python實(shí)現(xiàn)的簡(jiǎn)單文本類(lèi)游戲?qū)嵗?/h1>
更新時(shí)間:2015年04月28日 15:01:48 作者:feiwen
這篇文章主要介紹了python實(shí)現(xiàn)的簡(jiǎn)單文本類(lèi)游戲,以?xún)蓚€(gè)實(shí)例形式分析了python操作文本與字符串的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了python實(shí)現(xiàn)的簡(jiǎn)單文本類(lèi)游戲?qū)崿F(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
############################################################
# - My version on the game "Dragon Realm".
# - taken from the book "invent with python" by Al Sweigart.
# - thanks for a great book Mr Sweigart.
# - this code takes advantage of python 3.
############################################################
#files.py
import random
import time
print('\n\n[--system--] one file is bad the other is good ..guess the right one.\n')
print('\n\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nconnection established')
def displayIntro():
print('------------')
print('SYSTEM FILES')
print('------------\n')
print('1.) file.')
print('2.) file.\n')
def chooseOption():
option = ''
while option != '1' and option != '2':
print('which file to download? 1 or 2')
option = input('user:> ')
return option
def checkOption(chosenOption):
print('\nintialising download....')
time.sleep(1)
print('accessing file....')
time.sleep(1)
print('downloading....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
goodfile = random.randint(1, 2)
if chosenOption == str(goodfile):
print('\ndownload complete.')
print('\nGAME OVER')
else:
print('\nfile corrupt')
print('system infected.')
print('\nGAME OVER')
playAgain = 'yes'
while playAgain == 'yes':
displayIntro()
optionNumber = chooseOption()
checkOption(optionNumber)
print('\ndownload again? .... (yes or no)')
playAgain = input('user:> ')
############################################################
# - My version of the game "guess the number".
# - taken from the book "invent with python" by Al Sweigart.
# - thanks for a great book Mr Sweigart.
# - this code takes advantage of python 3.
############################################################
# -NOTE - this program will crash if a number is not typed.
#digitcode.py
import random
import time
guessesTaken = 0
print('\n\n\n\n\n[--system--] enter code in 15 trys to avoid lockout\n')
print('\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('connection established\n')
print('---------------------')
print(' MAINFRAME - LOGIN ')
print('---------------------')
print('\nenter 3 digit access code..')
number = random.randint(000, 999)
while guessesTaken < 15:
print()
guess = input('user:> ')
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('\nACCESS - DENIED -code to low')
if guess > number:
print('\nACCESS - DENIED -code to high')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('\nverifying ....')
time.sleep(1)
print('\nauthenticating ....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nACCESS - GRANTED')
print('\nGAME OVER\n')
exit(0)
if guess != number:
number = str(number)
print('\n....')
time.sleep(1)
print('\n....')
time.sleep(1)
print('\nSYSTEM LOCKED -the code was ' + number)
print()
exit(0)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
相關(guān)文章
-
關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python函數(shù)的相關(guān)知識(shí),文章圍繞著python中readlines函數(shù)的參數(shù)hint展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下 2021-06-06
-
20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具
本文主要介紹了Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧 2022-07-07
-
Django使用channels + websocket打造在線(xiàn)聊天室
本文將教你如何使用channels + websocket打造個(gè)在線(xiàn)聊天室。一共只有四步,你可以輕松上手并學(xué)會(huì)。項(xiàng)目中大部分代碼是基于channels的官方文檔的,加入了些自己的理解,以便新手學(xué)習(xí)使用。 2021-05-05
-
基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn)
這篇文章主要介紹了基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下 2020-07-07
-
Python腳本實(shí)現(xiàn)抓取指定網(wǎng)站上的所有圖片
對(duì)于開(kāi)發(fā)者、數(shù)據(jù)分析師以及研究人員而言,從網(wǎng)頁(yè)中提取有價(jià)值的信息是一項(xiàng)至關(guān)重要的技能,本文將詳細(xì)介紹如何使用Python編寫(xiě)一個(gè)腳本來(lái)自動(dòng)抓取指定網(wǎng)站上的所有圖片,需要的可以參考下 2024-10-10
-
Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱(chēng)的方法示例
這篇文章主要介紹了Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱(chēng)的方法,涉及Python使用psutil對(duì)系統(tǒng)進(jìn)程進(jìn)行操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下 2018-05-05
-
基于python編寫(xiě)的五個(gè)拿來(lái)就能用的炫酷表白代碼分享
七夕快到了,所以本文小編將給給大家介紹五種拿來(lái)就能用的炫酷表白代碼,無(wú)限彈窗表白,愛(ài)心發(fā)射,心動(dòng)表白,玫瑰花等表白代碼,需要的小伙伴快來(lái)試試吧 2023-08-08
-
python獲取文件版本信息、公司名和產(chǎn)品名的方法
這篇文章主要介紹了python獲取文件版本信息、公司名和產(chǎn)品名的方法,是Python程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下 2014-10-10
最新評(píng)論
本文實(shí)例講述了python實(shí)現(xiàn)的簡(jiǎn)單文本類(lèi)游戲?qū)崿F(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
############################################################
# - My version on the game "Dragon Realm".
# - taken from the book "invent with python" by Al Sweigart.
# - thanks for a great book Mr Sweigart.
# - this code takes advantage of python 3.
############################################################
#files.py
import random
import time
print('\n\n[--system--] one file is bad the other is good ..guess the right one.\n')
print('\n\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nconnection established')
def displayIntro():
print('------------')
print('SYSTEM FILES')
print('------------\n')
print('1.) file.')
print('2.) file.\n')
def chooseOption():
option = ''
while option != '1' and option != '2':
print('which file to download? 1 or 2')
option = input('user:> ')
return option
def checkOption(chosenOption):
print('\nintialising download....')
time.sleep(1)
print('accessing file....')
time.sleep(1)
print('downloading....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
goodfile = random.randint(1, 2)
if chosenOption == str(goodfile):
print('\ndownload complete.')
print('\nGAME OVER')
else:
print('\nfile corrupt')
print('system infected.')
print('\nGAME OVER')
playAgain = 'yes'
while playAgain == 'yes':
displayIntro()
optionNumber = chooseOption()
checkOption(optionNumber)
print('\ndownload again? .... (yes or no)')
playAgain = input('user:> ')
############################################################
# - My version of the game "guess the number".
# - taken from the book "invent with python" by Al Sweigart.
# - thanks for a great book Mr Sweigart.
# - this code takes advantage of python 3.
############################################################
# -NOTE - this program will crash if a number is not typed.
#digitcode.py
import random
import time
guessesTaken = 0
print('\n\n\n\n\n[--system--] enter code in 15 trys to avoid lockout\n')
print('\nconnecting....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('connection established\n')
print('---------------------')
print(' MAINFRAME - LOGIN ')
print('---------------------')
print('\nenter 3 digit access code..')
number = random.randint(000, 999)
while guessesTaken < 15:
print()
guess = input('user:> ')
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('\nACCESS - DENIED -code to low')
if guess > number:
print('\nACCESS - DENIED -code to high')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('\nverifying ....')
time.sleep(1)
print('\nauthenticating ....')
time.sleep(1)
print('....')
time.sleep(1)
print('....')
time.sleep(1)
print('\nACCESS - GRANTED')
print('\nGAME OVER\n')
exit(0)
if guess != number:
number = str(number)
print('\n....')
time.sleep(1)
print('\n....')
time.sleep(1)
print('\nSYSTEM LOCKED -the code was ' + number)
print()
exit(0)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python函數(shù)的相關(guān)知識(shí),文章圍繞著python中readlines函數(shù)的參數(shù)hint展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具
本文主要介紹了Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Django使用channels + websocket打造在線(xiàn)聊天室
本文將教你如何使用channels + websocket打造個(gè)在線(xiàn)聊天室。一共只有四步,你可以輕松上手并學(xué)會(huì)。項(xiàng)目中大部分代碼是基于channels的官方文檔的,加入了些自己的理解,以便新手學(xué)習(xí)使用。2021-05-05
基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn)
這篇文章主要介紹了基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python腳本實(shí)現(xiàn)抓取指定網(wǎng)站上的所有圖片
對(duì)于開(kāi)發(fā)者、數(shù)據(jù)分析師以及研究人員而言,從網(wǎng)頁(yè)中提取有價(jià)值的信息是一項(xiàng)至關(guān)重要的技能,本文將詳細(xì)介紹如何使用Python編寫(xiě)一個(gè)腳本來(lái)自動(dòng)抓取指定網(wǎng)站上的所有圖片,需要的可以參考下2024-10-10
Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱(chēng)的方法示例
這篇文章主要介紹了Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱(chēng)的方法,涉及Python使用psutil對(duì)系統(tǒng)進(jìn)程進(jìn)行操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05
基于python編寫(xiě)的五個(gè)拿來(lái)就能用的炫酷表白代碼分享
七夕快到了,所以本文小編將給給大家介紹五種拿來(lái)就能用的炫酷表白代碼,無(wú)限彈窗表白,愛(ài)心發(fā)射,心動(dòng)表白,玫瑰花等表白代碼,需要的小伙伴快來(lái)試試吧2023-08-08
python獲取文件版本信息、公司名和產(chǎn)品名的方法
這篇文章主要介紹了python獲取文件版本信息、公司名和產(chǎn)品名的方法,是Python程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10

