我正在尝试制作一个琐事游戏,主屏幕上有3行,每行8个问题,每个问题都是随机的。当您按问题时,所有其他按钮将消失,并且该问题将显示在较大的位置,并在下面输入文本。您必须正确回答,否则,您将返回主屏幕,并删除了该问题。
我正在尝试使其自动创建按钮并将问题加载到按钮上,并且该按钮在按下时将自动在屏幕上显示该问题,并能够从csv文件中获得答案。
有什么办法可以使我在用csv加载问题和答案时,该按钮存储答案,以便可以用来检查他们是否正确?
如果您不明白,请说,我会尽力!
这是在pygame 3.6上
我到目前为止有什么:
# pygame template - Bourne Grammar 2016
#https://github.com/Nearoo/pygame-text-input
import pygame
import sys
import random
import csv
import pygame_textinput
textinput = pygame_textinput.TextInput()
pygame.init() # starts the game engine
clock = pygame.time.Clock() # creates clock to limit frames per second
FPS = 60 # sets max speed of main loop
SCREENSIZE = SCREENWIDTH, SCREENHEIGHT = 1080, 720
screen = pygame.display.set_mode(SCREENSIZE)
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
pointsA = 0
pointsB = 0
questions = {}
buttons = []
def button(x,y,text): #MAKE BUTTON
ltr = len(text)
w= 12.5*ltr
button = pygame.Rect(x,y,w,50)
largeText = pygame.font.Font('freesansbold.ttf',20)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((x+(w/2)),(y+25))
return button, TextSurf, TextRect
def text_objects(text,font):
textSurf = font.render(text, True, black)
return textSurf, textSurf.get_rect()
def question(text): #MAKE QUESTION BIG ON SCREEN
largeText = pygame.font.Font('freesansbold.ttf',120)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((SCREENWIDTH/2)),(SCREENHEIGHT/2)
screen.blit(TextSurf, TextRect)
def trivQs(): #WHERE THEY GET LOADED IN AND ASSIGNED BUTTONS
with open("quizfile.csv") as f:
reader = csv.reader(f)
quiz_qas = list(reader)
quiz = random.sample(quiz_qas, 1) #RANDOMLY SELECT ONE QUESTION
for q, a in quiz:
questions[q] = a
for x, y in questions.items():
b,TextSurf,TextRect = button(400,400,x) #MAKE BUTTON
return b, TextSurf, TextRect,y
gameState = "running" # controls which state the games is in
# game loop #################### runs 60 times a second!
while gameState != "exit": # game loop - note: everything in the mainloop is indented one tab
#events = pygame.event.get()
for event in pygame.event.get(): # get user interaction events
if event.type == pygame.QUIT: # tests if window's X (close) has been clicked
gameState = "exit" # causes exit of game loop
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos # gets mouse position
# checks if mouse position is over the button
if b.collidepoint(mouse_pos):
print("a")
#do whatever
if b2.collidepoint(mouse_pos):
print("b") #USE QUESTION FUNCTION WITH OWN QUESTION TEXT
#do whatever
screen.fill(white)
b,TextSurf,TextRect,y = trivQs()
pygame.draw.rect(screen, [255, 0, 0], b)
screen.blit(TextSurf, TextRect)
b2,TextSurf,TextRect,y = trivQs()
pygame.draw.rect(screen, [255, 0, 0], b2)
screen.blit(TextSurf, TextRect)
pygame.display.update()
pygame.display.flip() # transfers build screen to human visable screen
clock.tick(FPS) # limits game to frame per second, FPS value
# out of game loop ###############
print("The game has closed")
pygame.quit()
sys.exit()
我必须使用类吗?