【发布时间】:2017-11-04 08:30:24
【问题描述】:
所以我正在尝试在 pygame 上制作一个游戏,它将一个词汇表显示为一个问题和三个答案选项。如果用户按正确的答案,他们的分数会上升一,游戏将进入下一个词汇问题。
我将我的问题存储在一个名为 questions[] 的二维数组中,在数组的每个元素中,它将每个问题的问题和答案保存为 [问题、正确答案选择、答案选择、答案选择]。所以正确的答案总是在索引位置[i][1]。我确实随机化了稍后显示答案选择的顺序。
现在我的问题是我的游戏在不等待用户输入的情况下通过问题。关键是它会等待用户点击。当用户点击时,它会检查用户点击的位置。用户鼠标的位置将决定用户按下哪个“答案框”。让我们假设用户按下了第一个框。游戏然后比较存储在该框中的文本是否正确(即文本与 questions[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()
【问题讨论】:
-
您好,欢迎来到 StackOverflow。请花一分钟时间到take the tour。 StackOverflow 不是用于编写代码的服务,而是一种协作故障排除的手段。这意味着,在这种情况下,重要的是描述您之前尝试过的内容和/或发布您当前代码的副本。