《Pygame游戏编程入门》学习——第3章 I/O、数据和字体:Trivia游戏
第3章 挑战1
问题1. 修改Trivia游戏,使用已有的代码来扩展你的背景,加入自己的用户输入和问题。
思路:增加背景颜色(RGB.py)和中文字体。系统中已有的中文字体名称:stfangsong, stcaiyun, stheiti, stxinwei, stsong, stzhongsong, sthupo, stliti, stkaiti, stxingkai, stxihei
问题2. 修改Trivia游戏中包含问题的trivia_data.txt数据文件,添加几个新的航天学问题。作为替代方案,可以创建你自己选择的其他学科领域的问题。
思路:由于使用excel表格能更有条理的准备数据,所以将网上的10道天文学选择题2录入excel中,然后使用xlrd模块3直接从excel中读取数据。
提前准备好excel表格:
需要注意的是,xlrd模块只能识别Microsoft Excel 97-2003文件(×.xls),所以需要把以上表格另存为.xls文件。
问题3. 修改Trivia游戏,使得用户回答完最后一个问题之后,提示用户是想要再玩一次还是退出,而不是从头再开始回答。
思路:Trivia
类增加一个布尔变量end
,用于判断用户是否已答完所有问题。在show_question()
方法中,如果end
为True
,则打印提示:“再玩一次(1),或退出(enter)”。handle_input()
方法用于响应用户数字1-4的输入,next_question()
方法用于响应用户回车的输入,故在两个方法中分别实现再玩一次和退出的逻辑即可。
3.6_Trivia.py
代码如下:
import sys, pygame, RGB, xlrd
from pygame.locals import *
class Trivia():
def __init__( self ):
self.data = {
}
self.current = 1
self.total = 0
self.correct = 0
self.score = 0
self.scored = False
self.failed = False
self.wronganswer = 0
self.colors = [ RGB.White, RGB.White, RGB.White, RGB.White ]
self.end = False
def load_data( self ):
workbook = xlrd.open_workbook( r'data.xls' )
data_sheet = workbook.sheet_by_name( 'data' )
self.total = data_sheet.nrows - 1
for row in range( 1, data_sheet.nrows ):
name = data_sheet.cell_value( row, 0 )
self.data[ name ] = {
}
for col in range( 1, data_sheet.ncols ):
attr = data_sheet.cell_value( 0, col )
value = data_sheet.cell_value( row, col )
self.data[ name ][ attr ] = value
def show_question( self ):
if self.end:
print_text( font1, 110, 5, '再玩一次(1),或退出(enter)' )
return
print_text( font1, 210, 5, 'TRIVIA游戏' )
print_text( font2, 190, 500-40, "按1-4回答问题", RGB.Purple )
print_text( font2, 530, 15, "得分", RGB.Purple )
print_text( font2, 550, 45, str( self.score ), RGB.Purple )
self.correct = int(self.data[ self.current ][ '正确答案' ] )
question = self.current
print_text( font1, 5, 80, "问题" + str( question ) )
print_text( font2, 5, 130, self.data[ self.current ][ '问题' ], RGB.Yellow )
if self.scored:
self.colors = [ RGB.White, RGB.White, RGB.White, RGB.White ]
self.colors[ self.correct - 1 ] = RGB.Green
print_text( font1, 230, 380, "正确!", RGB.Green )
print_text( font2, 170, 420, "输入回车前往下一题", RGB.Green )
elif self.failed:
self.colors = [ RGB.White, RGB.White, RGB.White, RGB.White ]
self.colors[ self.wronganswer - 1 ] = RGB.Red
self.colors[ self.correct - 1 ] = RGB.Green
print_text( font1, 230, 380, "错误!", RGB.Red )
print_text( font2, 170, 420, "输入回车前往下一题", RGB.Red )
print_text( font1, 5, 160, "答案" )
print_text( font2, 20, 210, '1 - ' + self.data[ self.current ][ 'A' ], self.colors[ 0 ] )
print_text( font2, 20, 240, '2 - ' + self.data[ self.current ][ 'B' ], self.colors[ 1 ] )
print_text( font2, 20, 270, '3 - ' + self.data[ self.current ][ 'C' ], self.colors[ 2 ] )
print_text( font2, 20, 300, '4 - ' + self.data[ self.current ][ 'D' ], self.colors[ 3 ] )
def handle_input( self, number ):
if self.end:
self.end = False
self.current = 1
return
if not self.scored and not self.failed:
if number == self.correct:
self.scored = True
self.score += 1
else:
self.failed = True
self.wronganswer = number
def next_question( self ):
if self.end:
sys.exit( 0 )
if self.scored or self.failed:
self.scored = False
self.failed = False
self.correct = 0
self.colors = [ RGB.White, RGB.White, RGB.White, RGB.White ]
self.current += 1
if self.current >= self.total:
self.end = True
def print_text( font, x, y, text, color=RGB.White, shadow=True ):
if shadow:
imgText = font.render( text, True, RGB.Black )
screen.blit( imgText, ( x - 2, y - 2 ) )
imgText = font.render( text, True, color )
screen.blit( imgText, ( x, y ) )
pygame.init()
screen = pygame.display.set_mode( ( 600, 500 ) )
pygame.display.set_caption( 'Trivia游戏' )
font1 = pygame.font.SysFont( 'stheiti', 36 )
font2 = pygame.font.SysFont( 'stheiti', 24 )
trivia = Trivia()
trivia.load_data()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
trivia.handle_input( 1 )
elif event.key == pygame.K_2:
trivia.handle_input( 2 )
elif event.key == pygame.K_3:
trivia.handle_input( 3 )
elif event.key == pygame.K_4:
trivia.handle_input( 4 )
elif event.key == pygame.K_RETURN:
trivia.next_question()
screen.fill( RGB.Black )
trivia.show_question()
pygame.display.update()
游戏模块RGB.py的部分代码如下:
White = ( 255, 255, 255 )
Purple = ( 160, 32, 240 )
Yellow = ( 255, 255, 0 )
Green = ( 0, 255, 0 )
Red = ( 255, 0, 0 )
Black = ( 0, 0, 0 )
以上是本游戏用到的颜色,全部RGB的颜色代码可以查RGB颜色表。
运行结果:
[美] Jonathan S. Harbour 著,李强 译,Python游戏编程入门,人民邮电出版社,2015.1 ↩︎