Python文字PK小游戲項(xiàng)目
明確項(xiàng)目目標(biāo)
- 人機(jī)PK小游戲
- 這個(gè)游戲中,會(huì)隨機(jī)生成玩家和敵人的屬性捎稚,同時(shí)互相攻擊,直至一方血量小于零。
- 戰(zhàn)斗會(huì)持續(xù)三局今野,采取三局兩勝制葡公,最后輸出戰(zhàn)斗結(jié)果,公布獲勝方条霜。
項(xiàng)目練習(xí)
- 延時(shí)函數(shù)time.sleep(secs)
import time #調(diào)用time模塊
#使用time模塊下面的sleep()函數(shù)催什,括號(hào)里填的是間隔的秒數(shù)(seconds,簡(jiǎn)稱secs)
#time.sleep(1.5)就表示停留1.5秒再運(yùn)行后續(xù)代碼
- 隨機(jī)模塊random
import random
#調(diào)用random模塊宰睡,與
a = random.randint(1,100)
# 隨機(jī)生成1-100范圍內(nèi)(含1和100)的一個(gè)整數(shù)蒲凶,并賦值給變量a
print(a)
- 目標(biāo)1:1.定義兩個(gè)變量,來存儲(chǔ)玩家血量和玩家攻擊力的數(shù)值 2.血量是100-150的隨機(jī)數(shù)拆内,攻擊力是30-50的隨機(jī)數(shù) 3.將兩個(gè)變量打印出來
- 目標(biāo)2:顯示玩家和敵人屬性
- 目標(biāo)3:自動(dòng)戰(zhàn)斗
- 雙方的血量和攻擊是隨機(jī)生成豹爹,不是固定的。所以我們不知道具體要戰(zhàn)斗多少回合才能分出勝負(fù)矛纹,也就是循環(huán)次數(shù)不明確,那自然要用while循環(huán)光稼。
- 循環(huán)的條件是雙方血量都要大于0
import random,time
playerwin = 0
enemywin = 0
for i in range(2):
#print('\n' +'++++++++現(xiàn)在開始第' + str(i + 1) + '局游戲+++++++++')
print('\n++++++++現(xià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兩邊的條件分別用括號(hào)括起或南,是一種習(xí)慣,方便閱讀
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))
把%想象成:圖書館里用來占位的一本書董济。先占一個(gè)位置,之后再填上實(shí)際的變量
- %后面的類型碼用什么要门,取決于你希望這個(gè)%占住的這個(gè)位置的數(shù)據(jù)以什么類型展示出來虏肾,如果你希望它以字符串形式展示,那就寫%s欢搜,如果你希望它以整數(shù)形式展示封豪,那就寫%d
文字游戲正確參考代碼
import time
import random
player_victory = 0
enemy_victory = 0
for i in range(1,4):
time.sleep(1.5)
print(' \n——————現(xiàn)在是第 %s 局——————' % i)
#對(duì)比之前:(' \n——————現(xià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('你發(fā)起了攻擊,【敵人】剩余血量%s' % enemy_life)
print('敵人向你發(fā)起了攻擊炒瘟,【玩家】的血量剩余%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【最終結(jié)果:你贏了胯杭!】')
elif enemy_victory > player_victory:
print('\n【最終結(jié)果:你輸了驯杜!】')
else:
print('\n【最終結(jié)果:平局!】')
再來一局
- 兩種方案
# 方案1
while True:
a1 = input('要繼續(xù)游戲嗎做个,請(qǐng)輸入n退出鸽心,輸入其他繼續(xù):')
if a1 == 'n':
break
# 方案2
again = True
while again:
a2 = input('要繼續(xù)游戲嗎,請(qǐng)輸入y繼續(xù)居暖,輸入其他退出:')
if a2 == 'y':
again = True
else:
again = False
import random,time
playerwin = 0
enemywin = 0
while True:
for i in range(2):
#print('\n' +'++++++++現(xiàn)在開始第' + str(i + 1) + '局游戲+++++++++')
print('\n++++++++現(xià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兩邊的條件分別用括號(hào)括起顽频,是一種習(xí)慣,方便閱讀
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()函數(shù)
# % 格式化:str % ()
print('%s%d'%('數(shù)字:',0))
print('%d,%d'%(0,1))
print('%d兰吟,%d通惫,%d'%(0,1,0))
name1 = 'Python'
print('I am learning %s'% name1) # 注:當(dāng)只跟一個(gè)數(shù)據(jù)時(shí),%后可不加括號(hào)揽祥,format()一定要有讽膏。
# format()格式化函數(shù):str.format()
print('\n{}{}'.format('數(shù)字:',0)) # 優(yōu)勢(shì)1:不用擔(dān)心用錯(cuò)類型碼。
print('{}拄丰,{}'.format(0,1)) # 不設(shè)置指定位置時(shí)府树,默認(rèn)按順序?qū)?yīng)。
print('{1}料按,{0}'.format(0,1)) # 優(yōu)勢(shì)2:當(dāng)設(shè)置指定位置時(shí)奄侠,按指定的對(duì)應(yīng)。
print('{0}载矿,{1}垄潮,{0}'.format(0,1)) # 優(yōu)勢(shì)3:可多次調(diào)用format后的數(shù)據(jù)烹卒。
name2 = 'Python基礎(chǔ)語法'
print('我正在學(xué){}'.format(name2)) # format()函數(shù)也接受通過參數(shù)傳入數(shù)據(jù)。