python人机PK小游戏

  • A+
所属分类:编程

小游戏代码

python人机PK小游戏

  1. import time
  2. import random
  3. play_victory = 0
  4. enemy_victory = 0
  5. while True:
  6.     for i in range(1,4):
  7.         time.sleep(1.5)
  8.         print('\n------------现在是第{}局,ready go'.format(i))
  9.          #print('\n------------现在是第%s局,ready go' %i)
  10.         #print('\n——————现在是第'+str(i)+'局,ready go!——————')
  11.         player_life = random.randint(100,150)
  12.         player_attack = random.randint(30,50)
  13.         enemy_life = random.randint(100,150)
  14.         enemy_attack = random.randint(30,50)
  15.         print('【玩家】\n血量:{}\n攻击:{}' .format(player_life, player_attack))
  16.          #print('【玩家】\n血量:%s\n攻击:%s'%(player_life,player_attack))
  17.         #print('【玩家】\n'+'血量:'+str(player_life)+'\n攻击:'+str(player_attack))
  18.         print('--------------')
  19.         time.sleep(1)
  20.         print('【敌人】\n血量:{}\n攻击:{}' .format(enemy_life, enemy_attack))
  21.          #print('【敌人】\n血量:%s\n攻击:%s' %(enemy_life,enemy_attack))
  22.         #print('【敌人】\n'+'血量:'+str(enemy_life)+'\n攻击:'+str(enemy_attack))
  23.         print('------------------------')
  24.         while (player_life>0) and (enemy_life>0):
  25.             player_life = player_life - enemy_attack
  26.             enemy_life = enemy_life - player_attack
  27.             print('你发起了攻击,【玩家】剩余血量{}' .format(player_life))
  28.              #print('你发起了攻击,【玩家】剩余血量%s'%player_life)
  29.             #print('你发起了攻击,【玩家】剩余血量' + str(player_life))
  30.             # player_life是整数,所以拼接时要先用str()转换
  31.             print('敌人向你发起了攻击,【敌人】剩余血量{}' .format(enemy_life))
  32.             #print('敌人向你发起了攻击,【敌人】剩余血量%s'%enemy_life)
  33.             #print('敌人向你发起了攻击,【敌人】剩余血量' + str(enemy_life))
  34.             print('------------------------')
  35.             time.sleep(1.5)
  36.         if player_life > 0 and enemy_life <=0:
  37.             play_victory +=1
  38.             print('敌人死翘翘了,你赢了')
  39.         elif player_life <=0 and enemy_life >0:
  40.             enemy_victory +=1
  41.             print('悲催,敌人把你干掉了!')
  42.         else:
  43.             print('哎呀,你和敌人同归于尽了!')
  44.     if play_victory > enemy_victory:
  45.         time.sleep(1)
  46.         print('【最终结果:你赢了!】')
  47.     elif enemy_victory > play_victory:
  48.         time.sleep(1)
  49.         print('【最终结果:你输了!】')
  50.     else:
  51.         print('【最终结果:平局】')
  52.     a1 = input('要继续游戏吗,输入n退出,其他继续')
  53.     if a1 == 'n':
  54.         break

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: