0

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?

  • 1
    Using classes would be indeed helpful. – Valentino Jun 06 '19 at 21:04
  • @valentino would you be able to help me with a basis on where to start with my buttons and questions and answers situation? – CyanDeathReaper Jun 06 '19 at 21:10
  • Could you add a sample of quizfile.csv? I'm trying to reproduce your code. – Valentino Jun 06 '19 at 21:22
  • 1
    CyanDeathReaper: I think you need to make the code in your question runnable befor asking how to enhance it. @Valentino: Even with a sample csv file, the `trivQs()` function is broken. – martineau Jun 06 '19 at 21:31
  • @martineau it think so too, I cannot understand how that function works. The `return` inside the nested loop does not make sense to me. – Valentino Jun 06 '19 at 21:35
  • the return gives the text box in the gameevent loop the text and box information so the button will appear. I do not know why I put it in the nested loop as it will only work once. i was trying to make it so it generates a random question and it's pairing answer, then displays it as a box – CyanDeathReaper Jun 06 '19 at 21:39
  • the csv file would look like: What colour is the sky, blue – CyanDeathReaper Jun 06 '19 at 21:41
  • @valentino please excuse my bad formatting in the comments as I am on mobile – CyanDeathReaper Jun 06 '19 at 21:44
  • 1
    But can you run your code? Because even with that csv, I get a `ValueError: too many values to unpack (expected 3)` when `trivQs` is called. – Valentino Jun 06 '19 at 21:48
  • ah yes add y to the b2 one like I have done in b, I must have missed it. it would be b2,TextSurf,TextRect,y = trivQs() – CyanDeathReaper Jun 06 '19 at 21:51
  • here is a photo of my flowchart diagram/psuedocode of what I am trying to achieve. I need to implement the topic buttons in my code but those will take a short time.https://photos.app.goo.gl/ZZTTkg3nDFzTGwsY6 – CyanDeathReaper Jun 06 '19 at 21:55
  • Please use the [edit](https://stackoverflow.com/posts/56485004/edit) button to fix your code in the question, otherwise will be difficult to follow – Valentino Jun 06 '19 at 22:05
  • ok sorry @valentino, I have done it – CyanDeathReaper Jun 06 '19 at 22:07
  • CyanDeathReaper: `return`ing in the middle of the nested loop like that in `trivQs()` terminates the loop (as well as the entire function's execution) at that point. This means that all it will ever do is create a single randomly-selected button. – martineau Jun 06 '19 at 22:46
  • @martineau how would I fix it then? – CyanDeathReaper Jun 07 '19 at 05:59
  • CyanDeathReaper: You need to create and store all of them in some kind of container like a list or dictionary. Creating a `Button` class would facilitate doing so, although that's not a strict requirement. The rest of the program could then be written in terms of using and manipulating that (or those) data structures. – martineau Jun 07 '19 at 06:28
  • So the class would look something like: ``` class Button: def __init__(self, question, answer,topic,position): self.question = question self.answer = answer``` – CyanDeathReaper Jun 07 '19 at 07:13
  • And when I read from the csv file, would I replace the return in the nested loop with Button(x,y,z,400)? What would I write in my game event loop to make it so all the buttons are displayed and then when I click on one of them, it uses the question function with that question in the parameters? – CyanDeathReaper Jun 07 '19 at 07:17

0 Answers0