Здравствуйте, я пытаюсь сделать простую викторину с несколькими вариантами ответов в pygame. Это код, который я написал, и он работает так, как задумано.
from random import choice
qUsed=[]
qEasy = [
{
"question": "What year is it",
"choices": {"a": "2009", "b": "2016", "c": "2010"},
"answer": "b"
},
{
"question": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
"answer": "a"
},
{
"question": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
"answer": "a"
},
{
"question": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
"answer": "a"
},
{
"question": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
"answer": "a"
}
]
def Quiz(qLevel):
global qUsed
if qLevel == []: # if qLevel becomes empty
qLevel = qUsed # dump everything from qUsed to qLevel
qUsed = [] # reset qUsed to an empty list
x = choice(qLevel) # assign x to a random question in qLevel
print(x.get('question')) # print the question
answer = input(x.get('choices')).lower() # print the choices
if answer == x.get('answer'): # if answer is correct
print("You got it")
else:
print("Wrong")
qLevel.remove(x) # remove used question from list
qUsed.append(x) # add it to an empty unused list (qUsed)
Quiz(qEasy)
Quiz(qEasy)
Проблема возникает здесь, когда я написал скрипт pygame, чтобы открыть окно с вопросом в качестве заголовка и тремя возможными ответами, случайным образом размеченными на трех черных прямоугольниках. Что я хочу сделать, так это случайным образом выбрать вопрос из списка, вывести его на экран, где написано «Вставьте вопрос сюда», и случайным образом назначить ответы трем прямоугольникам. Если вы нажмете на правый прямоугольник, то прямоугольник изменит цвет на зеленый, в противном случае он станет красным. Но я не могу понять, как это сделать.
def Screen():
mouse.set_visible(1)
clock = time.Clock()
# The button is just a rect.
A1 = Rect(30, 100, 175, 250)
A2 = Rect(230, 100, 175, 250)
A3 = Rect(430, 100, 175, 250)
while True:
screen.fill(WHITE)
draw.rect(screen, BLACK, A1)
draw.rect(screen, BLACK, A2)
draw.rect(screen, BLACK, A3)
Question_surf = FONT.render(("Insert Question Here"), True, BLACK)
A1_surf = FONT.render(("Answer 1"), True, WHITE)
A2_surf = FONT.render(("Answer 2"), True, WHITE)
A3_surf = FONT.render(("Answer 3"), True, WHITE)
A1_rect = A1_surf.get_rect(center=(120, 225))
A2_rect = A2_surf.get_rect(center=(320,225))
A3_rect = A3_surf.get_rect(center=(520,225))
Question_rect = Question_surf.get_rect(center=(320, 50))
screen.blit(A1_surf, A1_rect)
screen.blit(A2_surf, A2_rect)
screen.blit(A3_surf, A3_rect)
screen.blit(Question_surf, Question_rect)
for e in event.get():
if e.type == QUIT:
exit("Exit")
# This block is executed once for each MOUSEBUTTONDOWN event.
elif e.type == MOUSEBUTTONDOWN:
# 1 is the left mouse button, 2 is middle, 3 is right.
if e.button == 1:
# `event.pos` is the mouse position.
if A1.collidepoint(e.pos):
pass
elif A2.collidepoint(e.pos):
pass
elif A3.collidepoint(e.pos):
pass
display.update()
clock.tick(60)
if __name__ == "__main__":
Screen()