網(wǎng)站首頁 編程語言 正文
學Python之前我們先來幾個簡單的小游戲練練手,這三個小游戲一個比一個復雜,建議新手慢慢來:
1.猜拳
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
2.數(shù)字炸彈
import random import time ? bomb = random.randint(1, 99) print(bomb) start = 0 end = 99 while 1 == 1: ? ? ? people = int(input('請輸入{}到{}之間的數(shù):'.format(start, end))) ? ? if people > bomb: ? ? ? ? print('大了') ? ? ? ? end = people ? ? elif people < bomb: ? ? ? ? print('小了') ? ? ? ? start = people ? ? else: ? ? ? ? print('BOOM!!!') ? ? ? ? break ? ? print('等待電腦了輸入{}到{}之間的數(shù):'.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
3.賭大小
import time import random # 讓用戶注冊 name = input('請?zhí)顚懹脩裘?) age = input("{}您好,請輸入您的年齡 : ".format(name)) user_info = {'name': name, 'age': int(age)} ?# 用戶信息 user_properties = ['X 1-5'] ?# 用于存放用戶道具 默認道具 properties = ['X3 (250G)', 'X1-5 (300G)'] ?# 道具列表 顯示用 ? # 根據(jù)用戶年齡 給與不同的初始金幣 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("電腦每次投擲三枚骰子,總點數(shù)>=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('骰子點數(shù)為:{}'.format(dices), end=' ') ? ? ? ? if (total >= 10 and u_input == 'big') or (total < 10 and u_input == 'small'): ? ? ? ? ? ? print('您贏了!!!') ? ? ? ? ? ? multi = 1 ?# 倍數(shù) ? ? ? ? ? ? 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('您的金幣已經(jīng)用完,感謝您的游玩') ? ? ? ? ? ? break ? ? ? ? ? if user_info['glod'] % 1000 == 0: ?# 用戶金幣 是1000的倍數(shù)是 可購買道具 ? ? ? ? ? ? shop = input('您現(xiàn)在有金幣:{},是否購買道具 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('您現(xiàn)在有金幣:{},是否繼續(xù)游玩,yes or no: '.format(user_info['glod'])) ? ? ? ? ? ? print('您現(xiàn)在有金幣:{} '.format(user_info['glod'])) else: ? ? print('歡迎下次游玩,再見!')
原文鏈接:https://blog.csdn.net/huang5333/article/details/111715129
相關推薦
- 2022-04-23 R語言兩組變量特征相關關系熱圖繪制畫法_R語言
- 2022-04-18 python列表推導式實現(xiàn)找出列表中長度大于5的名字_python
- 2022-07-30 react中受控組件與非受控組件
- 2022-11-27 Git基礎學習之文件刪除操作命令詳解_相關技巧
- 2022-09-08 深入了解C語言的動態(tài)內(nèi)存管理_C 語言
- 2022-03-29 ASP.NET?Core實現(xiàn)動態(tài)獲取文件并下載_實用技巧
- 2022-03-26 C語言中指針常量和常量指針的區(qū)別_C 語言
- 2022-11-05 Nginx配置文件中l(wèi)ocation配置的多種場景_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支