在这个小游戏项目中,我们一起来完成一个文字版的人机对战小游戏吧,并且一步一步增加游戏的功能,让它变得更真实、更有意思吧~
第一个版本1.0:
import time
import random
player_victory = 0
enemy_victory = 0
for i in range(1,4):
time.sleep(1.5)
print(' \n——————现在是第 %s 局——————' % i)
player_life = random.randint(100,150)
player_attack = random.randint(30,50)
enemy_life = random.randint(100,150)
enemy_attack = random.randint(30,50)
print('【玩家】\n血量:%s\n攻击:%s' % (player_life,player_attack))
print('------------------------')
time.sleep(1)
print('【敌人】\n血量:%s\n攻击:%s' % (enemy_life,enemy_attack))
print('-----------------------')
time.sleep(1)
while player_life > 0 and enemy_life > 0:
player_life = player_life - enemy_attack
enemy_life = enemy_life - player_attack
print('你发起了攻击,【敌人】剩余血量%s' % enemy_life)
print('敌人向你发起了攻击,【玩家】的血量剩余%s' % player_life)
print('-----------------------')
time.sleep(1.2)
if player_life > 0 and enemy_life <= 0:
player_victory += 1
print('敌人死翘翘了,你赢了!')
elif player_life <= 0 and enemy_life > 0:
enemy_victory += 1
print('悲催,敌人把你干掉了!')
else:
print('哎呀,你和敌人同归于尽了!')
if player_victory > enemy_victory :
time.sleep(1)
print('\n【最终结果:你赢了!】')
elif enemy_victory > player_victory:
print('\n【最终结果:你输了!】')
else:
print('\n【最终结果:平局!】')
第二个版本2.0:
import time,random
# 需要的数据和变量放在开头
player_list = ['【狂血战士】','【森林箭手】','【光明骑士】','【独行剑客】','【格斗大师】','【枪弹专家】']
enemy_list = ['【暗黑战士】','【黑暗弩手】','【暗夜骑士】','【嗜血刀客】','【首席刺客】','【陷阱之王】']
players = random.sample(player_list,3)
enemies = random.sample(enemy_list,3)
player_info = {}
enemy_info = {}
# 随机生成角色的属性
def born_role():
life = random.randint(100,180)
attack = random.randint(30,50)
return life,attack
# 生成和展示角色信息
def show_role():
for i in range(3):
player_info[players[i]] = born_role()
enemy_info[enemies[i]] = born_role()
# 展