I am trying to make a trivia game where the home screen has three rows of 8 questions, each question random. When you press on the question, all the other buttons will disappear and that question will appear bigger with an input text below. You have to answer it correctly and if you do, you go back to the home screen with that question removed.
I am trying to make it so it automatically creates the buttons and loads in the questions onto the buttons and that button will automatically, when pressed, show that question large on screen and be able to have the answer from the csv file.
Is there any way I could make it so when I am loading in the questions and answers with csv that the button stores the answer so it can be used to check if they got it correct?
If you do not understand please say, I will try my best!
This is on pygame 3.6
what i have so far:
# 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()
Would I have to use classes?