一个简单的Trivia Game

xiaoxiao2021-02-28  148

根据《Python游戏编程入门》编写,稍加更改完成挑战

import sys, pygame 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.failde = False self.wronganswer = 0 self.colors = [white,white,white,white] self.flag = False #read trivia data from file f = open(filename, "r") 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 def show_question(self): print_text(font1, 210, 5, "TRIVIA GAME") if self.current < self.total: print_text(font2, 190, 500-20, "Press Keys (1-4) To Answer", purple) print_text(font2, 530, 5, "SCORE", purple) if self.current == 0: self.score = 0 print_text(font2, 550, 25, str(self.score), purple) #get correct answer out of data (first) if self.current < self.total: self.correct = int(self.data[self.current+5]) #尝试新加if语句########################################################### '''if self.flag == 1: self.flag = 0 self.current = 0 self.total = 0 self.failde = True''' if self.current >= self.total: #self.current -= 6 #self.correct = int(self.data[self.current+5]) #display question #question = self.current #print_text(font1, 5, 80, "QUESTION" + str(int(question/6+1))) #print_text(font2, 20, 120, self.data[self.current], yellow) #直接显示 print_text(font1, 120, 150, "请问是否要重新开始?", green) print_text(font2, 130 ,200, "Press Enter For Restart.", green) self.failde = True self.flag = True ##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]) ### else: #display question question = self.current print_text(font1, 5, 80, "QUESTION" + str(int(question/6+1))) print_text(font2, 20, 120, self.data[self.current], yellow) #respond to correct 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.failde: self.colors = [white,white,white,white] self.colors[self.wronganswer-1] = red self.colors[self.correct-1] = green print_text(font1, 230, 380, "INCORRECT!", red) print_text(font2, 170 ,420, "Press Enter For Next Question", red) #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.failde: if number == self.correct: self.scored = True self.score += 1 else: self.failde = True self.wronganswer = number def next_question(self): if self.scored or self.failde: self.scored = False self.failde = False self.correct = 0 self.colors = [white,white,white,white] self.current += 6 #if self.current >= self.total and self.flag == True: if self.flag: self.current = 0 self.flag = False 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.SysFont('SimHei', 40) font2 = pygame.font.SysFont('SimHei', 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("E:\Sublime Text3\My py\\trivia_data.txt") #repeating loop 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() #clear the screen screen.fill((132,133,135)) #display trivia data trivia.show_question() #update the display pygame.display.update()

转载请注明原文地址: https://www.6miu.com/read-31115.html

最新回复(0)