所以我试图在pygame上制作一个游戏,它会显示一个词汇作为一个问题和三个答案选择。如果用户按下正确的答案,他们的分数将上升一,游戏将继续下一个词汇问题。
我将我的问题存储在一个名为questions []的二维数组中,在数组的每个元素中,它将每个问题的问题和答案保存为[问题,正确的答案选择,答案选择,答案选择]。所以正确答案总是在索引位置[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()
答案 0 :(得分:0)
你可以在循环中为问题设置一个无限循环,当在答案框上单击鼠标时,退出无限循环的中断条件。
示例强>:
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
答案 1 :(得分:0)
是的,您需要重新构建程序并更好地将绘图与事件处理和游戏逻辑分开。如果按下鼠标按钮,它应该只进入下一个问题,因此请检查事件循环if event.type == pygame.MOUSEBUTTONDOWN:
(每次单击只生成一个MOUSEBUTTONDOWN
事件),然后查看是否单击了一个rect,增量得分,最后呈现下一个问题和选择。在事件循环之外显示问题和选择文本。
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.