Python學習筆記 練習 和電腦對戰的小遊戲

2021-10-02 15:42:20 字數 4821 閱讀 9263

學習python的練習題,稍微做了點改動和優化

#人機大戰

#給雙方命名

#隨機分配血量和攻擊力給雙方(一定範圍)

#比大小決定誰先出手

#列印每一回合的戰況

#列印最終結果

#增加隨機性

import time,random

players = [,

]print('**********歡迎來到劍魂世界!**********')

time.sleep(1)

#給玩家起名

players[0]['player_name'] = '》' + input('在遊戲開始前,請告訴我你的名字:\n') + '《'

time.sleep(1)

#給電腦起名

players[1]['player_name'] = '》' + input('很好,'+ players[0]['player_name'] +',這名字聽起來很霸氣!接下來請告訴我你對手的名字:\n') + '《'

time.sleep(1)

print('你的對手叫' + players[1]['player_name'] +'嗎?也很不錯!\n'+ '————————————-———————————————————————')

time.sleep(1)

print('對戰馬上就要開始啦!')

time.sleep(1)

print('3')

time.sleep(1)

print('2')

time.sleep(1)

print('1')

time.sleep(2)

#接下去是三個迴圈巢狀

goon = 1

while goon:

#定義局數、回合數和玩家勝場

game = 0

player_win = 0

players[0]['player_win'] = 0

players[1]['player_win'] = 0

while game <3:

game += 1

round = 1

print('***************第 ' + str(game) + ' 局***************')

players[0]['player_hp'] = random.randint(180,220)

players[0]['player_atk'] = random.randint(25,35)

players[0]['player_def'] = random.randint(3,5)

players[1]['player_hp'] = random.randint(180,220)

players[1]['player_atk'] = random.randint(25,35)

players[1]['player_def'] = random.randint(3,5)

time.sleep(1)

print('這是你們本局各自的屬性值:')

time.sleep(1)

print('%s 血量:%d 攻擊力:%d 防禦力:%d'%(players[0]['player_name'],players[0]['player_hp'],players[0]['player_atk'],players[0]['player_def']))

time.sleep(1)

print(players[1]['player_name']+'血量:' + str(players[1]['player_hp'] )+ ' 攻擊力:' +str(players[1]['player_atk'])

+ ' 防禦力:' + str(players[1]['player_def']))

time.sleep(1)

print('———————————————————————-————————————')

time.sleep(1)

#定義先手和後手和勝者

first = random.randint(0,1)

secend = 1-first

winner = 0

print(players[first]['player_name'] + '獲得了先機,率先發起進攻!')

time.sleep(1)

print('—————————————————————-——————————————')

time.sleep(1)

while players[0]['player_hp'] > 0 and players[1]['player_hp'] > 0:

print('第' + str(round) + '回合')

#計算傷害(新增隨機性)

damage1 = players[first]['player_atk'] - players[secend]['player_def'] + random.randint(-3,3)

damage2 = players[secend]['player_atk'] - players[first]['player_def'] + random.randint(-3,3)

#回合數+1

round += 1

players[secend]['player_hp'] -= damage1

#判斷是否死亡

if players[secend]['player_hp'] < 0:

players[secend]['player_hp'] = 0

time.sleep(1)

print(players[first]['player_name'] + '對' + players[secend]['player_name'] + '造成了' +

str(damage1) + '點傷害,' + players[secend]['player_name'] + '還剩下' +

str(players[secend]['player_hp']) + '點血。')

if players[secend]['player_hp'] == 0:

winner = first

print(players[secend]['player_name'] + '倒下了,戰鬥結束')

break

players[first]['player_hp'] -= damage2

if players[first]['player_hp'] < 0:

players[first]['player_hp'] = 0

time.sleep(1)

print(players[secend]['player_name'] + '對' + players[first]['player_name'] + '造成了' +

str(damage2) + '點傷害,' + players[first]['player_name'] + '還剩下' +

str(players[first]['player_hp']) + '點血。')

if players[first]['player_hp'] == 0:

winner = secend

print(players[first]['player_name'] + '倒下了,戰鬥結束')

break

time.sleep(1)

print('———————————————————-————————————————')

time.sleep(1)

print('———————————————————-————————————————')

time.sleep(1)

print('精彩!')

time.sleep(1)

print('真是太精彩了!!')

time.sleep(1)

print(str(players[winner]['player_name']) + '贏得了第' + str(game) +'局戰鬥,本局持續了' + str(round) + '回合。')

players[winner]['player_win'] += 1

time.sleep(1)

print('現在的比分是 ' + players[0]['player_name'] + ' ' + str(players[0]['player_win']) +

':' + str(players[1]['player_win']) + ' ' + players[1]['player_name'])

if players[0]['player_win'] > players[1]['player_win']:

finalwinner = 0

print('目前' + players[0]['player_name'] + '領先!')

elif players[0]['player_win'] < players[1]['player_win']:

finalwinner = 1

print('目前' + players[1]['player_name'] + '領先!')

else:

print('目前情況膠著,平局!勝負將在下一局揭曉!!')

if players[0]['player_win'] == 2 or players[1]['player_win'] == 2:

break

time.sleep(2)

print('激動人心的時刻到來了!!勝負已分!!' + players[finalwinner]['player_name'] + '獲得了勝利,恭喜!!!')

goon = int(input('是否要再來一把?\n0:不來了\n1:再來一把\n'))

print('玩的很愉快,再見!')

筆記 Python 學習筆記 題目練習

max num 4 ls for i in range 1,max num 1 for j in range 1,max num 1 if i j new num i 10 j if new num in ls continue print 一共可以生成 i位互不相同且無重複數字的兩位數 len l...

python 學習筆記 基礎練習

今天抽空練習了兩道題,同時重點看了檔案和輸入輸出章節基礎知識。1.寫乙個函式判斷輸入日期為當年的第幾天?deal with special case 2015 01 30 2015.01.30 2015 01 30 defyear of date datestr if datestr.find 0 ...

python學習筆記 pandas練習(2)

這幾天遇到一些合併表的需求。需要將多個表進行橫向合併。查閱pandas的功能,提供了這個鏈結的作用。以下是讀取多個csv的資料下面看一下實驗過程。首先,我們將 設定為csv格式,每乙個 只包含2個字段id和price 2個字段。依次類推,建五個表,每乙個表分別是 這樣資料。1 20 2 30 3 4...