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

本文提供两个Python编程示例,包括一款猜数字游戏和一个模拟文件下载的游戏。玩家需在限定次数内猜出随机生成的三位数以避免锁定,以及在两个文件中选择正确的进行下载,游戏涉及随机数生成、用户输入处理和时间模拟等编程概念。
摘要由CSDN通过智能技术生成

############################################################

# - 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程序设计有所帮助。

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值