Python文字PK小游戏项目
明确项目目标
- 人机PK小游戏
- 这个游戏中,会随机生成玩家和敌人的属性,同时互相攻击,直至一方血量小于零。
- 战斗会持续三局,采取三局两胜制,最后输出战斗结果,公布获胜方。
示意
项目练习
- 延时函数time.sleep(secs)
import time #调用time模块
#使用time模块下面的sleep()函数,括号里填的是间隔的秒数(seconds,简称secs)
#time.sleep(1.5)就表示停留1.5秒再运行后续代码
- 随机模块random
import random
#调用random模块,与
a = random.randint(1,100)
# 随机生成1-100范围内(含1和100)的一个整数,并赋值给变量a
print(a)
- 目标1:1.定义两个变量,来存储玩家血量和玩家攻击力的数值 2.血量是100-150的随机数,攻击力是30-50的随机数 3.将两个变量打印出来
- 目标2:显示玩家和敌人属性
- 目标3:自动战斗
- 双方的血量和攻击是随机生成,不是固定的。所以我们不知道具体要战斗多少回合才能分出胜负,也就是循环次数不明确,那自然要用while循环。
- 循环的条件是双方血量都要大于0
import random,time
playerwin = 0
enemywin = 0
for i in range(2):
#print('\n' +'++++++++现在开始第' + str(i + 1) + '局游戏+++++++++')
print('\n++++++++现在开始第 %s 局游戏+++++++++' % (i + 1))
playerlife = random.randint(100,150)
playerattack = random.randint(30,50)
enemylife = random.randint(100,150)
enemyattack = random.randint(30,50)
round = 0
time.sleep(1.5)
print('-------------------------------------------')
#print('玩家的血量是[' + str(playerlife) + '],攻击力是[' + str(playerattack) + ']')
print('玩家的血量是[%s],攻击力是[%s]' % (playerlife,playerattack))
time.sleep(1)
#print('敌人的血量是[' + str(enemylife) + '],攻击力是[' + str(enemyattack) + ']')
print('敌人的血量是[%s],攻击力是[%s]' % (enemylife,enemyattack))
print('--------------------------------------------\n')
time.sleep(1)
print('+++++++++++游戏即将开始++++++++++')
time.sleep(1.5)
# and两边的条件分别用括号括起,是一种习惯,方便阅读
while (playerlife > 0) and (enemylife > 0):
round += 1
time.sleep(1)
#print('\n' + '第' + str(round) + '轮攻击')
print('\n第%s轮攻击' % round)
print('~~~~~~~~~~~~~~~~~~~~~~~~~')
enemylife = enemylife - playerattack
time.sleep(1)
if enemylife <= 0:
#print('敌人剩余血量' + str(enemylife) + '死亡,玩家胜利')
print('敌人剩余血量%s死亡,玩家胜利' % enemylife)
playerwin += 1
print('-------------------------------------------')
continue
else:
#print('玩家攻击,敌人剩余血量为' + str(enemylife))
print('玩家攻击,敌人剩余血量为%s' % enemylife)
playerlife = playerlife - enemyattack
time.sleep(1)
if playerlife <= 0:
#print('玩家剩余血量' + str(playerlife) + '死亡,敌人胜利')
print('玩家剩余血量%s死亡,敌人胜利' % playerlife)
enemywin += 1
continue
else:
#print('敌人攻击,玩家剩余血量为' + str(playerlife))
print('敌人攻击,玩家剩余血量为%s' % playerlife)
print('--------------------------------------------')
if playerwin == 2:
print('玩家赢下2局,玩家胜')
elif enemywin == 2:
print('敌人赢下2局,敌人胜')
else:
pass
格式化字符串
print('血量:'+str(player_life)+' 攻击:'+str(player_attack))
print('血量:%s 攻击:%s' % (player_life,player_attack))
把%想象成:图书馆里用来占位的一本书。先占一个位置,之后再填上实际的变量
格式符和类型码
- %后面的类型码用什么,取决于你希望这个%占住的这个位置的数据以什么类型展示出来,如果你希望它以字符串形式展示,那就写%s,如果你希望它以整数形式展示,那就写%d
文字游戏正确参考代码
import time
import random
player_victory = 0
enemy_victory = 0
for i in range(1,4):
time.sleep(1.5)
print(' \n——————现在是第 %s 局——————' % i)
#对比之前:(' \n——————现在是第'+str(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【最终结果:平局!】')
再来一局
- 两种方案
# 方案1
while True:
a1 = input('要继续游戏吗,请输入n退出,输入其他继续:')
if a1 == 'n':
break
# 方案2
again = True
while again:
a2 = input('要继续游戏吗,请输入y继续,输入其他退出:')
if a2 == 'y':
again = True
else:
again = False
import random,time
playerwin = 0
enemywin = 0
while True:
for i in range(2):
#print('\n' +'++++++++现在开始第' + str(i + 1) + '局游戏+++++++++')
print('\n++++++++现在开始第 %s 局游戏+++++++++' % (i + 1))
playerlife = random.randint(100,150)
playerattack = random.randint(30,50)
enemylife = random.randint(100,150)
enemyattack = random.randint(30,50)
round = 0
time.sleep(1.5)
print('-------------------------------------------')
#print('玩家的血量是[' + str(playerlife) + '],攻击力是[' + str(playerattack) + ']')
print('玩家的血量是[%s],攻击力是[%s]' % (playerlife,playerattack))
time.sleep(1)
#print('敌人的血量是[' + str(enemylife) + '],攻击力是[' + str(enemyattack) + ']')
print('敌人的血量是[%s],攻击力是[%s]' % (enemylife,enemyattack))
print('--------------------------------------------\n')
time.sleep(1)
print('+++++++++++游戏即将开始++++++++++')
time.sleep(1.5)
# and两边的条件分别用括号括起,是一种习惯,方便阅读
while (playerlife > 0) and (enemylife > 0):
round += 1
time.sleep(1)
#print('\n' + '第' + str(round) + '轮攻击')
print('\n第%s轮攻击' % round)
print('~~~~~~~~~~~~~~~~~~~~~~~~~')
enemylife = enemylife - playerattack
time.sleep(1)
if enemylife <= 0:
#print('敌人剩余血量' + str(enemylife) + '死亡,玩家胜利')
print('敌人剩余血量%s死亡,玩家胜利' % enemylife)
playerwin += 1
print('-------------------------------------------')
continue
else:
#print('玩家攻击,敌人剩余血量为' + str(enemylife))
print('玩家攻击,敌人剩余血量为%s' % enemylife)
playerlife = playerlife - enemyattack
time.sleep(1)
if playerlife <= 0:
#print('玩家剩余血量' + str(playerlife) + '死亡,敌人胜利')
print('玩家剩余血量%s死亡,敌人胜利' % playerlife)
enemywin += 1
continue
else:
#print('敌人攻击,玩家剩余血量为' + str(playerlife))
print('敌人攻击,玩家剩余血量为%s' % playerlife)
print('--------------------------------------------')
if playerwin >= 2:
print('==================')
print('玩家赢下2局,玩家胜')
elif enemywin >= 2:
print('==================')
print('敌人赢下2局,敌人胜')
else:
print('平局')
again = input('是否要再来一局?Y or N')
if again == 'Y':
continue
else:
break
格式化字符串:format()函数
# % 格式化:str % ()
print('%s%d'%('数字:',0))
print('%d,%d'%(0,1))
print('%d,%d,%d'%(0,1,0))
name1 = 'Python'
print('I am learning %s'% name1) # 注:当只跟一个数据时,%后可不加括号,format()一定要有。
# format()格式化函数:str.format()
print('\n{}{}'.format('数字:',0)) # 优势1:不用担心用错类型码。
print('{},{}'.format(0,1)) # 不设置指定位置时,默认按顺序对应。
print('{1},{0}'.format(0,1)) # 优势2:当设置指定位置时,按指定的对应。
print('{0},{1},{0}'.format(0,1)) # 优势3:可多次调用format后的数据。
name2 = 'Python基础语法'
print('我正在学{}'.format(name2)) # format()函数也接受通过参数传入数据。