让pygame在继续之前等待用户单击/输入(python问答游戏)

让pygame在继续之前等待用户单击/输入(python问答游戏),python,pygame,Python,Pygame,所以我尝试在pygame上做一个游戏,它会将一个词汇表显示为一个问题和三个答案选项。如果用户按下正确答案,他们的分数将上升1,游戏将进入下一个词汇问题 我将我的问题存储在一个名为questions[]的2D数组中,在数组的每个元素中,它将每个问题的问题和答案保存为[question,correct answer choice,answer choice,answer choice]。所以正确答案总是在索引位置[i][1]。我会随机化答案选择的显示顺序 现在我的问题是,我的游戏在不等待用户输入的情

所以我尝试在pygame上做一个游戏,它会将一个词汇表显示为一个问题和三个答案选项。如果用户按下正确答案,他们的分数将上升1,游戏将进入下一个词汇问题

我将我的问题存储在一个名为questions[]的2D数组中,在数组的每个元素中,它将每个问题的问题和答案保存为[question,correct answer choice,answer choice,answer choice]。所以正确答案总是在索引位置[i][1]。我会随机化答案选择的显示顺序

现在我的问题是,我的游戏在不等待用户输入的情况下完成问题。关键是它将等待用户单击。当用户单击时,它会检查用户单击的位置。用户鼠标的位置将决定用户按下的“应答框”。让我们假设用户按下了第一个框。然后,游戏比较存储在该框中的文本是否正确(即文本与问题[i][1]相同)。它会在一瞬间显示每个问题,然后转到下一个问题和下一个问题

但它不会等待用户先单击。甚至没有,它甚至没有显示足够长的时间让用户阅读问题。是否有一种方法可以构建循环或添加某些条件,以便程序显示每个问题,直到用户选择答案,然后添加分数并继续下一个问题

代码如下:

import pygame
from random import randint
from pygame import *

pygame.init()
pygame.font.match_font('Courier New.ttf')


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREEN = (71, 212, 15)
BLUE = (42, 250, 246)
PINK = (255,102, 196)
YELLOW = (255, 255, 0)
i = 0

size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Spanish Space Quiz") 

done = False

questions = [["Hola", "Hello", "Goodbye", "Cow"],["Amigo", "Friend", "Cat", "Dog"],["Si", "Yes", "No", "Maybe"]]
answerboxes = [[30,300,190,150,BLUE,WHITE,7],[255,300,190,150,YELLOW,WHITE,7],[480,300,190,150,PINK,WHITE,7]]
score = 0
choices = []

def textObject (text, font):
    textWord = font.render(text, True, WHITE)
    return textWord, textWord.get_rect()

def answerbutton(drawbox):
    mouse = pygame.mouse.get_pos()

    if drawbox[0]+drawbox[2] > mouse[0] > drawbox[0] and drawbox[1]+drawbox[3] > mouse[1] > drawbox[1]:
        pygame.draw.rect(screen, drawbox[5],(drawbox[0],drawbox[1],drawbox[2],drawbox[3]),drawbox[6])           
    else:
        pygame.draw.rect(screen, drawbox[4],(drawbox[0],drawbox[1],drawbox[2],drawbox[3]),drawbox[6])

    answerTextFont = pygame.font.SysFont("Courier New",60)
    textWord, textBox = textObject(drawbox[7], answerTextFont) #the text & the "Text box"
    textBox.center = ( (drawbox[0]+(drawbox[2]/2)), (drawbox[1]+(drawbox[3]/2)) )
    screen.blit(textWord, textBox)

def questionbutton(message,x,y,w,h,color):
    mouse = pygame.mouse.get_pos()
    pygame.draw.rect(screen,color,(x,y,w,h))

    answerTextFont = pygame.font.SysFont("Courier New",60)
    textWord, textBox = textObject(message, answerTextFont) #the text & the "Text box"
    textBox.center = ( (x+(w/2)), (y+(h/2)) )
    screen.blit(textWord, textBox)

while not done:
    screen.blit (backgroundImage, [0,0])
    font = pygame.font.SysFont('Courier', 30, True, False)
    text = font.render("SPACE VOCBULARY QUIZ",True,WHITE)
    screen.blit(text, [30, 30])
    font = pygame.font.SysFont('Courier', 30, False, False)
    text = font.render("SCORE: ", True, WHITE)
    screen.blit(text, [500, 30])

    for event in pygame.event.get():
        if i == (len(questions)): #if user clicks close then done becomes true and game quits
            done = True
            event.type == pygame.QUIT
        for c in range (len(questions)):
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            questionbutton((questions[c][0]),30,150,640,100,GREEN)
            for n in range(3):
                choices.append(questions[c][n+1])
            for r in range(3):
                randomPointer = randint(0, (len(choices)-1))
                answerboxes[r].append(choices[randomPointer])
                choices.remove(choices[randomPointer])
                answerbutton(answerboxes[r][0:8])
            if click[0] == 1: 
                for a in range(3):
                    if answerboxes[a][0]+answerboxes[a][2] > mouse[0] > answerboxes[a][0] and answerboxes[a][1]+answerboxes[a][3] > mouse[1] > answerboxes[a][1]:
                        if answerboxes[a][7] == questions[i][1]:
                            score = score + 1
                            print (score)
            for g in range (3):
                answerboxes[g].pop()           
            i = i+1

    pygame.display.update()

pygame.quit()

您可以在问题的循环中放置一个无限循环,中断条件是当鼠标单击答案框时退出无限循环

示例

for c in len(range(questions)):
    clicked_on_answer = False
    while True:
        # your code

        if click[0] == 1: 
            for a in range(3):
                if answerboxes[a][0]+answerboxes[a][2] > mouse[0] > answerboxes[a][0] and answerboxes[a][1]+answerboxes[a][3] > mouse[1] > answerboxes[a][1]:
                    clicked_on_answer = True
                    if answerboxes[a][7] == questions[i][1]:
                        score = score + 1
                        print (score)
        if clicked_on_answer:
            break

您可以在问题的循环中放置一个无限循环,中断条件是当鼠标单击答案框时退出无限循环

示例

for c in len(range(questions)):
    clicked_on_answer = False
    while True:
        # your code

        if click[0] == 1: 
            for a in range(3):
                if answerboxes[a][0]+answerboxes[a][2] > mouse[0] > answerboxes[a][0] and answerboxes[a][1]+answerboxes[a][3] > mouse[1] > answerboxes[a][1]:
                    clicked_on_answer = True
                    if answerboxes[a][7] == questions[i][1]:
                        score = score + 1
                        print (score)
        if clicked_on_answer:
            break

是的,您需要重新构造程序,更好地将绘图与事件处理和游戏逻辑分开。如果按下鼠标按钮,它应该只进入下一个问题,因此检查事件循环
if event.type==pygame.MOUSEBUTTONDOWN:
(每次单击仅产生一个
MOUSEBUTTONDOWN
事件),然后查看是否单击了矩形,增加分数,最后呈现下一个问题和选项。将问题和选择文本置于事件循环之外

clock = pygame.time.Clock()  # A clock to limit the frame rate.
# Define the fonts outside of the main loop.
font = pygame.font.SysFont('Courier', 30, False, False)

# Render question and choice text surfaces.
# Create the rects for the choices and set their positions.

while not done:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Check if event.pos collides with the correct rect.
            for index, rect in enumerate(rects):
                if rect.collidepoint(event.pos) and choices[index] == correct_answer:
                    score += 1
            # Get next question and choices, render them and update the rects.

    # Draw everything.
    # Blit the question and choice text surfaces at their rects.

    pygame.display.update()
    clock.tick(30)  # Limit frame rate to 30 fps.

是的,您需要重新构造程序,更好地将绘图与事件处理和游戏逻辑分开。如果按下鼠标按钮,它应该只进入下一个问题,因此检查事件循环
if event.type==pygame.MOUSEBUTTONDOWN:
(每次单击仅产生一个
MOUSEBUTTONDOWN
事件),然后查看是否单击了矩形,增加分数,最后呈现下一个问题和选项。将问题和选择文本置于事件循环之外

clock = pygame.time.Clock()  # A clock to limit the frame rate.
# Define the fonts outside of the main loop.
font = pygame.font.SysFont('Courier', 30, False, False)

# Render question and choice text surfaces.
# Create the rects for the choices and set their positions.

while not done:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Check if event.pos collides with the correct rect.
            for index, rect in enumerate(rects):
                if rect.collidepoint(event.pos) and choices[index] == correct_answer:
                    score += 1
            # Get next question and choices, render them and update the rects.

    # Draw everything.
    # Blit the question and choice text surfaces at their rects.

    pygame.display.update()
    clock.tick(30)  # Limit frame rate to 30 fps.

嗨,谢谢你的回答。不幸的是,当我运行代码时,屏幕上没有显示任何内容,并且有一个无限旋转的轮子。我不知道为什么/嗨,谢谢你的回答。不幸的是,当我运行代码时,屏幕上没有显示任何内容,并且有一个无限旋转的轮子。我不知道为什么/大家好,欢迎来到StackOverflow。请稍等片刻。StackOverflow不是一个编写代码的服务,而是一种协作故障排除的方法。这意味着,在本例中,描述您以前尝试过的内容和/或发布当前代码的副本非常重要。您好,欢迎来到StackOverflow。请稍等片刻。StackOverflow不是一个编写代码的服务,而是一种协作故障排除的方法。这意味着,在这种情况下,描述您以前尝试过的内容和/或发布当前代码的副本非常重要。