按下 return 键后如何使字符串出现

How do you make the string appear after hitting the return key

我正在为我的孩子制作抽认卡游戏。它是关于恐龙的。我无法让 "Congrats, You got it right" 出现在屏幕上。我已经把我的代码移到了各处,但没有运气。有人可以帮帮我吗?

明确地说,我想要发生的是当用户按下键盘上的数字 1、2、3 时,如果该键是与问题相关的正确答案,则消息 "Congrats, You got it right!" 应该出现在屏幕上。

我知道现在的 keydown 事件是 return 键,但我这样做只是为了测试目的。这对于 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()

我对你的确切问题是什么感到有点困惑,所以我会尝试回答。你说你想要 "Congrats, , you got it right!" 这个词,所以我可以帮助你解决问题。你在给屏幕上色之前 blit 测试文本,所以每次循环循环时,它都会显示测试文本,但几乎立即用 screen.fill(0) 覆盖它。为了使它更好,您应该在屏幕着色后放置文本块。最好的方法是将它放在循环的开头,或者在代码中 screen.fill 的当前位置之后创建另一个事件检测器。 此外,我将摆脱堆叠的 while 循环,而是将其替换为 if 语句,因为它已经在 while 循环中。 这是您要找的吗?

您在处理 Return 按键事件时对屏幕表面执行的 blit 在您稍后调用 screen.fill(0).[=12= 时被覆盖]

我稍微重新安排了您的代码并添加了在适当的按键时显示结果。

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)

希望这个框架足以供您扩展。

如果您对代码的任何部分有任何疑问,请告诉我。