Python3基礎實戰練習,三個簡單練手遊戲

學Python3之前我們先來幾個簡單的小遊戲練練手,這三個小遊戲一個比一個複雜,建議新手慢慢來:

Python3基礎實戰練習,三個簡單練手遊戲

1.猜拳:

<code>import random  #導入隨機模塊

num = 1
yin_num = 0
shu_num = 0
while num <= 3:
if shu_num == 2 or yin_num == 2:
break
user = int(input('請出拳 0(石頭) 1(剪刀) 2(布)'))
if user > 2:
print('不能出大於2的值')
else:
data = ['石頭', '剪刀', '布']
com = random.randint(0, 2)
print("您出的是{},電腦出的是{}".format(data[user], data[com]))
if user == com:
print('平局')
continue
elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):
print('你贏了')
yin_num += 1
else:
print('你輸了')
shu_num += 1
num += 1/<code>
Python3基礎實戰練習,三個簡單練手遊戲

2.數字炸彈:

<code>import random
import time

bomb = random.randint(1, 99)
print(bomb)
start = 0
end = 99
while 1 == 1:

people = int(input('請輸入{}到{}之間的數:'.format(start, end)))
if people > bomb:
print('大了')
end = people
elif people < bomb:
print('小了')
start = people
else:
print('BOOM!!!')
break
print('等待電腦了輸入{}到{}之間的數:'.format(start, end))
time.sleep(1)
com = random.randint(start + 1, end - 1)
print('電腦輸入:{}'.format(com))
if com > bomb:
print('大了')
end = com
elif com < bomb:
print('小了')
start = com
else:
print('BOOM!!!')
break/<code>

3.賭大小:

<code>import time
import random
# 讓用戶註冊

name = input('請填寫用戶名:')
age = input("{}您好,請輸入您的年齡 : ".format(name))
user_info = {'name': name, 'age': int(age)} # 用戶信息
user_properties = ['X 1-5'] # 用於存放用戶道具 默認道具
properties = ['X3 (250G)', 'X1-5 (300G)'] # 道具列表 顯示用

# 根據用戶年齡 給與不同的初始金幣
if 10 < user_info['age'] < 18:
glod = 1000
elif 18 <= user_info['age'] <= 30:
glod = 1500
else:
glod = 500
user_info['glod'] = glod

# 輸出相關提示信息
print("{}您好,歡迎遊玩本遊戲,您的初始金幣為:{}".format(user_info['name'], user_info['glod']))
print("\\n")
time.sleep(1)
print('遊戲說明'.center(50, '*'))
print('*'.ljust(53), '*')
print('*', end='')
print("電腦每次投擲三枚骰子,總點數>=10為大,否則為小".center(32), end='')
print('*')
print('*'.ljust(53), '*')
print('*' * 54)
print("\\n")

# 開始遊戲
result = input('是否開始遊戲 yes or no : ')
go = True
if (result.lower() == 'yes'):
while go:
dices = []
# 開始投擲
for i in range(0, 3):
dices.append(random.randint(1, 6))
total = sum(dices) # 計算總和

user_input = input('請輸入big OR small : ') # 等待用戶輸入
u_input = user_input.strip().lower()
time.sleep(1)
# 判斷用戶輸入
print('骰子點數為:{}'.format(dices), end=' ')
if (total >= 10 and u_input == 'big') or (total < 10 and u_input == 'small'):
print('您贏了!!!')
multi = 1 # 倍數
if len(user_properties) > 0: # 如果用戶有道具 選擇是否使用道具
use_pro = input('是否使用道具: ')
if use_pro.lower() == 'yes':
use_pro = int(input('請選擇使用第幾個道具{} :'.format(user_properties)))
use_pro -= 1
# 判斷道具類型
if user_properties[use_pro] == 'X 3':
multi = 3
print('獎金翻3倍')
elif user_properties[use_pro] == 'X 1-5':
multi = random.randint(1, 5)
print('獎金翻{}倍'.format(multi))
user_properties.remove(user_properties[use_pro]) # 刪除道具

user_info['glod'] += 100 * multi; # 金額增加
else:
print('您輸了!')
user_info['glod'] -= 100; # 錯誤 用戶金幣減 100

# 判斷用戶金幣 是否夠下次玩 不夠則退出程序
if (user_info['glod'] <= 0):
print('您的金幣已經用完,感謝您的遊玩')
break

if user_info['glod'] % 1000 == 0: # 用戶金幣 是1000的倍數是 可購買道具
shop = input('您現在有金幣:{},是否購買道具 yes or no: '.format(user_info['glod']))
if shop.lower() == 'yes':

good_num = int(input('請選擇要購買第幾個道具 {}'.format(properties)))
if good_num == 1:
user_properties.append('X 3') # 給用戶添加道具
user_info['glod'] -= 250
print('購買成功!消耗金幣250')
elif good_num == 2:
user_properties.append('X 1-5') # 給用戶添加道具
user_info['glod'] -= 300 # 用戶金幣減 300
print('購買成功!消耗金幣300')
else:
print('沒有該道具,您失去了這次機會')
else:
# 一直提示 太煩
# conti = input('您現在有金幣:{},是否繼續遊玩,yes or no: '.format(user_info['glod']))
print('您現在有金幣:{} '.format(user_info['glod']))
else:
print('歡迎下次遊玩,再見!')/<code>

最後小編幫助大家整理了一套python教程,下面展示了部分,希望也能幫助對編程感興趣,想做數據分析,人工智能、爬蟲或者希望從事編程開發的小夥伴,畢竟python工資也還可以,如果能幫到你請點贊、點贊、點贊。

Python3基礎實戰練習,三個簡單練手遊戲

Python3基礎實戰練習,三個簡單練手遊戲

Python3基礎實戰練習,三個簡單練手遊戲

Python3基礎實戰練習,三個簡單練手遊戲

Python3基礎實戰練習,三個簡單練手遊戲

PS:如果你喜歡python,並覺得這篇文章對你有益的話,麻煩多多點贊關注支持!!!!


分享到:


相關文章: