《Python游戏编程入门》3.5-3习题

题目:修改Trivia游戏,使得用户回答完最后一个问题之后,提示用户是想要在再玩一次还是退出,而不是从头再开始回答.

主要添加self.endflag进行判断,如果结束则显示提示信息,并且原来对题目的键盘输入不再有效,同时对用户的分数score进行清零。

import pygame
import sys
from pygame.locals import *
    
class Trivia(object):
  def __init__(self,filename):
    self.data=[]
    self.current = 0
    self.total=0
    self.correct=0
    self.score=0
    self.scored=False
    self.failed=False
    self.wronganswer=0
    self.colors=[white ,white,white,white]
    self.endflag=False#添加的标志符
  
    #read trivia data from file
    f=open(filename,"rt")
    trivia_data=f.readlines()
    f.close()

    #count and clean up trivia data
    for text_line in trivia_data:
      self.data.append(text_line.strip())
      self.total +=1
    print(self.data)

  def show_question(self):
    print_text(font1,210,5,"TRIVIA GAME")
    print_text(font2,190,500-20,"Press Keys (1-4) To Answer",purple)
    print_text(font2,530,5,"SCORE",purple)
    print_text(font2,530,25,str(self.score),purple)

    #get correct answer out of data(first)
    self.correct=int (self.data[self.current+5])

    #display question 
    question= self.current//6+1
    print_text(font1,5,80,"Question"+str(question))
    print_text(font2,20,120,self.data[self.current],yellow)

    #respon to current answer
    if self.scored:
     self.colors=[white,white,white,white]
     self.colors[self.correct-1]=green
     print_text(font1,230,380,"CORRECT!",green)
     print_text(font2,170,420,"Press Enter For Next Question",green)
    elif self.failed:
      self.colors=[white,white,white,white]
      self.colors[self.wronganswer-1]=red
      self.colors[self.correct-1]=green
      print_text(font1,220,380,"INCORRECT!",red)
      print_text(font2,170,420,"PRESS ENTER For Next Question",red)

    #相应结束判断
    if self.endflag :
        print_text(font1,150,280,"This is end,replay?",white)
        print_text(font2,180,320,"PRESS ENTER For Replay",white)
        print_text(font2,180,340,"PRESS ESCAPE For Quit",white)
        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_RETURN:
                    self.current=0
                    self.endflag=False
                    self.score=0


    #display answers
    print_text(font1,5,170,"Answers")
    print_text(font2,20,210,"1-"+self.data[self.current+1],self.colors[0])
    print_text(font2,20,240,"2-"+self.data[self.current+2],self.colors[1])
    print_text(font2,20,270,"3-"+self.data[self.current+3],self.colors[2])
    print_text(font2,20,300,"4-"+self.data[self.current+4],self.colors[3])

  #响应用户输入
  def handle_input (self,number):
    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.scored or self.failed:
      self.scored=False
      self.failed=False
      self.correct=0
      self.colors=[white,white,white,white]
      self.current+=6
      if self.current >=self.total:#判断已经读取所有,可以进行询问是否想要再玩一次
        self.endflag=True
        self.current -=6
        
        

#主代码
def print_text(font,x,y,text,color=(255,255,255),shadow=True):
  if shadow:
    imgText= font.render(text,True,(0,0,0))
    screen.blit(imgText,(x-2,y-2))
  imgText=font.render(text,True,color)
  screen.blit(imgText,(x,y))

#main program begins
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("The Trivia Game")
font1=pygame.font.Font(None,40)
font2=pygame.font.Font(None,24)
white=255,255,255
cyan=0,255,255
yellow=255,255,0
purple=255,0,255
green=0,255,0
red=255,0,0

#load the trivia data file
trivia =Trivia("trivia_data.txt")

#repeating loop
while True:
  for event in pygame.event.get():
    if event.type==QUIT:
      sys.exit()
    elif event.type==KEYUP and trivia.endflag ==False:
      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()
  
  #clean the screen
  screen.fill((0,0,200))

  #display trivia data
  trivia.show_question()

  #updata the display
  pygame.display.update()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值