python实现的简单文本类游戏实例

本文实例讲述了python实现的简单文本类游戏实现方法。分享给大家供大家参考。具体实现方法如下:

############################################################
# – 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)

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论