。
关于TRivia游戏的加载
这是源代码:
# coding=UTF-8
import pygame
import struct
import sys
from import *
class Trivia(object):
def __init__(self, filename):
= []
self.current = 0
= 0
self.correct = 0
= 0
= False
= False
self.wronganswer = 0
= [white, white, white, white]
# read trivia data from file
f = open('trivia_data.txt', "r")
trivia_data = f.readline()
f.close()
# count and clean up trivia data
for text_line in trivia_data:
self.data.append(text_line.strip())
+= 1
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, 550, 25, str(self.score), purple)
#get correct answer out of data(first)
self.correct = int(self.data[self.current+5])
#Display question
question_text(font1, 5, 80, "QUESTION" + str(question))
question_text(font2, 20, 120, self.data[self.current], yellow)
#respond to correct
if self.scored:
= [white, 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:
= [white, white, white, white, white]
self.colors[self.correct-1] = green
print_text(font1, 220, 380, "INCORRECT!", red)
print_text(font2, 170,420, "Press Enter For NEXT QUESTION", red)
#display answer
print_text(font1, 5, 170, "ANSWER")
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 and not self.failed:
if number == self.correct:
= True
+= 1
else:
= True
self.wronganswer = number
def next_question(self):
if or self.failed:
= False
= False
self.correct = 0
= [white, white, white, white, white]
self.current += 6
if self.current >= self.total:
self.current = 0
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 = pyagme.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("")
# repeating loop
while True:
for event in pygame.event.get():
if == QUIT:
sys.exit()
elif == pygame.K_1:
trivia.handle_input(1)
elif == pygame.K_2:
trivia.handle_input(2)
elif == pygame.K_3:
trivia.handle_input(3)
elif == pygame.K_4:
trivia.handle_input(4)
elif == pygame.K_RETURN:
trivia.next_input()
# clear the screen
screen.fill((0, 0, 200))
# display trivia data
trivia.show_question()
# update the display
pygame.display.update()
帮我分析解决一下
- 下载图片
- 复制图片
2024-03-26
浏览571
技能问答
登录后评论
1
评论
分享