解题思路
1.明确项目目标
2.分析过程,拆解项目
3.逐步执行,代码实现
明确项目目标
# 你想制作一个人机PK小游戏,最后想达到这样的效果
image.png
分析项目,拆解过程
# 对于新手来说,层层递进,难度不断增加是个不错的选择
# 拆解项目,从最简单过程开始,不断突破
image.png
逐步执行,代码实现
版本1.0:自定属性,人工PK
# 用最基础的思路将项目搭起来
# 新知识:time模块
import time
print('【玩家】\n血量:100\n攻击:50')
print('------------------------')
time.sleep(1.5)
print('【敌人】\n血量:100\n攻击:30')
print('------------------------')
time.sleep(1.5)
print('你发起了攻击,【敌人】剩余血量50')
print('敌人向你发起了攻击,【玩家】剩余血量70')
print('------------------------')
time.sleep(1.5)
print('你发起了攻击,【敌人】剩余血量0')
print('敌人向你发起了攻击,【玩家】剩余血量40')
print('-----------------------')
time.sleep(1.5)
print('敌人死翘翘了,你赢了!')
版本2.0:随机属性,自动PK
# 为达到版本2.0需要的知识通过网络搜索来完成
# time、random模块
import time
import random
enemy_lelf = random.randint(100,150)
player_lelf = random.randint(100,150)
enemy_attack = random.randint(30,50)
player_attack = random.randint(30,50)
print('【玩家】\n血量:'+str(enemy_lelf)+'\n攻击:'+str(enemy_attack))
print('------------------------')
time.sleep(1.5)
print('【玩家】\n血量:'+str(player_lelf)+'\n攻击:'+str(player_attack))
print('------------------------')
time.sleep(1.5)
while enemy_lelf >= 0 and player_lelf >= 0:
enemy_lelf = enemy_lelf - player_attack
player_lelf = player_lelf - enemy_attack
print('你发起了攻击,【敌人】剩余血量'+str(enemy_lelf))
print('敌人向你发起了攻击,【玩家】剩余血量'+str(player_lelf))
print('------------------------')
time.sleep(1.5)
if enemy_lelf <= 0 and player_lelf > 0:
print('玩家赢得胜利')
elif enemy_lelf > 0 and player_lelf <= 0:
print('敌人赢得胜利')
else:
print('打成平局')
版本3:打印成果,三局两胜
# 知识点:time模块、random模块、for循环、while循环、格式化字符串、if……ellf……else
# 最重要的是对于工作的拆解,从易到难进行
# 新知识点:格式化字符串,%可以被看成图书馆占位置的书,%s %f %d
import time,random
for i in range(3):
enemy_number = 0
player_number = 0
time.sleep(1.5)
print('这是第 %s 局' % (i+1))
time.sleep(1.5)
enemy_lelf = random.randint(100,150)
player_lelf = random.randint(100,150)
enemy_attack = random.randint(30,50)
player_attack = random.randint(30,50)
print('【玩家】\n血量:%s \n攻击:%s'%(player_lelf,player_attack))
print('------------------------')
time.sleep(1)
print('【敌人】\n血量:%s \n攻击:%s'%(enemy_lelf,enemy_attack))
print('------------------------')
time.sleep(1)
while enemy_lelf >= 0 and player_lelf >= 0:
enemy_lelf = enemy_lelf - player_attack
player_lelf = player_lelf - enemy_attack
print('你发起了攻击,【敌人】剩余血量 %s' % (enemy_lelf))
print('敌人向你发起了攻击,【玩家】剩余血量 %s' % (player_lelf))
print('------------------------')
time.sleep(1.5)
if enemy_lelf <= 0 and player_lelf > 0:
print('玩家赢得胜利')
player_number += 1
elif enemy_lelf > 0 and player_lelf <= 0:
print('敌人赢得胜利')
enemy_number += 1
else:
print('打成平局')
if player_number > enemy_number:
print('经过3局PK,最终玩家取得胜利,获胜局数是 %s' % (player_number))
elif player_number < enemy_number:
print('经过3局PK,最终敌人取得胜利,获胜局数是 %s' % (enemy_number))
else:
print('经过3局PK,玩家和敌人打成平局')
网友评论