我正在为我的孩子创建一个flashcard游戏。它关于迪诺斯。我无法制作"恭喜,你做对了"出现在屏幕上。我把代码移到了所有地方,但没有运气。有人可以帮帮我吗。
要清楚,我想要发生的是当用户按下键盘上的数字1,2,3时,如果该键是与问题相关的正确答案,则消息"恭喜,你做对了!"应出现在屏幕上。
我知道keydown事件现在是返回键,但我这样做仅用于测试目的。对于testtext变量,这也是相同的。我正在使用该变量来查看是否可以打印" Hello WOrld"到了屏幕。
我确实感觉它与正在运行的循环有关。我的猜测是,它确实出现了几分之一秒,但在任何人都能看到它之前消失了。
import pygame, random
pygame.font.init()
pygame.init()
font = pygame.font.Font(None, 48)
#Created the window display
size = width, height = 800,800
screen = pygame.display.set_mode(size)
#Loads the images of the starting game Trex
#t_rex = pygame.image.load('trex1.png')
##Places the image on the screen
#screen.blit(t_rex,(150,50))
count = 0
score = 0
active = False
testtext = font.render("Hello WOrld", True, (250, 250, 250))
#The below code keeps the display window open until user decides to quie app
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
screen.blit(testtext, (200,699))
while count < 2:
screen.fill(0)
dinoQuestions = ["Does a t-rex eat meat?\n","Does a trycerotopes have 3 horns?\n"]
dinoAnswer = ["Yes\n", "No\n","Maybe\n"]
wordnum = random.randint(0, len(dinoQuestions)-1)
mainpic = pygame.image.load("trex1.png")
screen.blit(mainpic, (150, 20))
options = [random.randint(0, len(dinoAnswer)-1),random.randint(0, len(dinoAnswer)-1)]
options[random.randint(0,1)] = wordnum
question_display = font.render(dinoQuestions[wordnum].rstrip('\n'),True, (255, 255, 255))
text1 = font.render('1 - ' + dinoAnswer[options[0]].rstrip('\n'),True, (255, 255, 255))
text2 = font.render('2 - ' + dinoAnswer[options[1]].rstrip('\n'),True, (255, 255, 255))
#the below code is for testing purposes only
screen.blit(question_display,(200, 590))
screen.blit(text1, (200, 640))
screen.blit(text2, (200, 690))
count = count + 1
pygame.display.flip()
答案 0 :(得分:1)
当您稍后致电screen.fill(0)
时,处理返回按键事件时您执行的屏幕表面的blit会被覆盖。
我已经重新安排了一些代码并添加了在适当的按键上显示结果。
import pygame
import random
pygame.init()
pygame.font.init()
font = pygame.font.Font(None, 48)
size = width, height = 800,800
screen = pygame.display.set_mode(size) #Created the window display
count = 0
score = 0
active = False
white = pygame.color.Color("white")
black = pygame.color.Color("black")
green = pygame.color.Color("green")
# load/create static resources once
mainpic = pygame.image.load("trex1.png")
testtext = font.render("Hello World", True, (250, 250, 250))
correct_text = font.render("Correct! Well Done!", True, green)
clock = pygame.time.Clock() # for limiting FPS
dinoQuestions = ["Does a t-rex eat meat?","Does a triceratops have 3 horns?"]
dinoAnswer = ["Yes", "No","Maybe"]
# initialise state
show_hello = False
show_correct = False
update_questions = True # need to update questions on the first iteration
finished = False
while not finished:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
show_hello = not show_hello # toggle flag for later display
elif event.key == pygame.K_SPACE:
update_questions = True
elif event.key in [pygame.K_1, pygame.K_2]:
# an answer has been selected
# pygame.K_1 is 0, pygame.K_2 is 1
if dinoAnswer[event.key - pygame.K_1] == "Yes":
show_correct = True
count += 1
else:
show_correct = False
screen.fill(black)
screen.blit(mainpic, (150, 20))
if show_hello:
screen.blit(testtext, (200,199))
if show_correct:
screen.blit(correct_text, (200, 300))
if update_questions:
random.shuffle(dinoQuestions)
random.shuffle(dinoAnswer)
question_display = font.render(dinoQuestions[0],True, white)
text1 = font.render('1 - ' + dinoAnswer[0],True, white)
text2 = font.render('2 - ' + dinoAnswer[1],True, white)
update_questions = False
show_correct = False
# Display the Question
screen.blit(question_display,(200, 590))
screen.blit(text1, (200, 640))
screen.blit(text2, (200, 690))
# count = count + 1
pygame.display.flip()
clock.tick(60)
希望这足以让你扩展一个框架。
如果您对代码的任何部分有任何疑问,请与我们联系。
答案 1 :(得分:0)
我对你的确切问题感到有点困惑,所以我将尝试回答。你说你想要单词&#34;恭喜,你说对了!&#34;,所以我可以帮助你解决问题所在。在为屏幕着色之前,你会对测试文本进行blit,因此每次循环循环时,它都会显示testtext,但几乎可以立即用screen.fill(0)覆盖它。为了使它更好,你应该在屏幕着色后放置文本的blitting。执行此操作的最佳方法是在循环开始时将其放置,或者在屏幕的当前位置之后创建另一个事件检测器。填写代码。 此外,我将摆脱堆叠的while循环,而是用if语句替换它,因为它已经在while循环中。 这是你在找什么?