3個(gè)適合新手練習(xí)的python小游戲
學(xué)Python之前我們先來幾個(gè)簡單的小游戲練練手,這三個(gè)小游戲一個(gè)比一個(gè)復(fù)雜,建議新手慢慢來:
1.猜拳
import random ?#導(dǎo)入隨機(jī)模塊 ? 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'] ?# 用于存放用戶道具 默認(rèn)道具 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 ? # 輸出相關(guān)提示信息 print("{}您好,歡迎游玩本游戲,您的初始金幣為:{}".format(user_info['name'], user_info['glod'])) print("\n") time.sleep(1) print('游戲說明'.center(50, '*')) print('*'.ljust(53), '*') print('*', end='') print("電腦每次投擲三枚骰子,總點(diǎn)數(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) ?# 計(jì)算總和 ? ? ? ? user_input = input('請輸入big OR small : ') ?# 等待用戶輸入 ? ? ? ? u_input = user_input.strip().lower() ? ? ? ? time.sleep(1) ? ? ? ? # 判斷用戶輸入 ? ? ? ? print('骰子點(diǎn)數(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('請選擇使用第幾個(gè)道具{} :'.format(user_properties))) ? ? ? ? ? ? ? ? ? ? use_pro -= 1 ? ? ? ? ? ? ? ? ? ? # 判斷道具類型 ? ? ? ? ? ? ? ? ? ? if user_properties[use_pro] == 'X 3': ? ? ? ? ? ? ? ? ? ? ? ? multi = 3 ? ? ? ? ? ? ? ? ? ? ? ? print('獎(jiǎng)金翻3倍') ? ? ? ? ? ? ? ? ? ? elif user_properties[use_pro] == 'X 1-5': ? ? ? ? ? ? ? ? ? ? ? ? multi = random.randint(1, 5) ? ? ? ? ? ? ? ? ? ? ? ? print('獎(jiǎng)金翻{}倍'.format(multi)) ? ? ? ? ? ? ? ? ? ? user_properties.remove(user_properties[use_pro]) ?# 刪除道具 ? ? ? ? ? ? ? user_info['glod'] += 100 * multi; ?# 金額增加 ? ? ? ? else: ? ? ? ? ? ? print('您輸了!') ? ? ? ? ? ? user_info['glod'] -= 100; ?# 錯(cuò)誤 用戶金幣減 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('請選擇要購買第幾個(gè)道具 {}'.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('沒有該道具,您失去了這次機(jī)會(huì)') ? ? ? ? else: ? ? ? ? ? ? # ?一直提示 太煩 ? ? ? ? ? ? # conti = input('您現(xiàn)在有金幣:{},是否繼續(xù)游玩,yes or no: '.format(user_info['glod'])) ? ? ? ? ? ? print('您現(xiàn)在有金幣:{} '.format(user_info['glod'])) else: ? ? print('歡迎下次游玩,再見!')
到此這篇關(guān)于3個(gè)適合新手練習(xí)的python小游戲的文章就介紹到這了,更多相關(guān)python小游戲練習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中反向生成models.py的實(shí)例講解
今天小編就為大家分享一篇Django中反向生成models.py的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python異常處理機(jī)制結(jié)構(gòu)實(shí)例解析
這篇文章主要介紹了Python異常處理機(jī)制結(jié)構(gòu)實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python數(shù)據(jù)分析庫pandas基本操作方法
下面小編就為大家分享一篇Python數(shù)據(jù)分析庫pandas基本操作方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python golang中g(shù)rpc 使用示例代碼詳解
這篇文章主要介紹了python golang中g(shù)rpc 使用,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06將Python項(xiàng)目打包成exe并附帶下載功能的操作流程
這篇文章主要為大家詳細(xì)介紹了將Python項(xiàng)目打包成exe并附帶下載功能的操作流程,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下2023-12-12tensorboard 可視化之localhost:6006不顯示的解決方案
這篇文章主要介紹了tensorboard 可視化之localhost:6006不顯示的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python讀取并顯示圖片的三種方法(opencv、matplotlib、PIL庫)
這篇文章主要給大家介紹了關(guān)于python讀取并顯示圖片的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04