-1

I am trying to make it so when you answer one of the questions on the screen, the four buttons on the main menu will change and display a new set of 4 questions from my quizfile.csv file.

I have tried loading the questions in again but they are the same, with the newQuestions function


z=0 #for positioning
t=0 #for loop
quiz = random.sample(quiz_qas, 4) #randomly select 4 questions
for q, a in quiz: #for every question and answer in the file
    questions[q] = a #define the dictionary of questions and answers
    for x, y in questions.items(): #for every answer and question in the dictionary, link them
        if t==0: #the sweet spots for getting a different question every time
            b = Button(x,y,200,200) #make button object
            z+=50

        elif t==5:
            b1 = Button(x,y,600,200)
            z+=50

        elif t==7:
            b2 = Button(x,y,600,400)
            z+=50
        elif t==9:
            b3 = Button(x,y,200,400)
            z+=50
        t+=1
questions = {}

def newQuestions(): #TRY TO RESET QUESTIONS
    questions = {}
    with open("quizfile.csv") as f: #load in questions
        reader = csv.reader(f)
        quiz_qas = list(reader) 

    z=0 #for positioning
    t=0 #for loop
    quiz = random.sample(quiz_qas, 4) #randomly select 4 questions
    for q, a in quiz: #for every question and answer in the file
        questions[q] = a #define the dictionary of questions and answers
        print(questions.items())
        for x, y in questions.items(): #for every answer and question in the dictionary, link them
            if t==0: #the sweet spots for getting a different question every time
                b = Button(x,y,200,200) #make button object
                z+=50

            elif t==5:
                b1 = Button(x,y,600,200)
                z+=50

            elif t==7:
                b2 = Button(x,y,600,400)
                z+=50
            elif t==9:
                b3 = Button(x,y,200,400)
                z+=50
            t+=1

b2on = False #for handling displaying the question
b3on = False
b4on = False
b5on = False
correct=False
wrong=False
paustimerevent = pygame.USEREVENT + 1
pausetimerevent = pygame.USEREVENT + 2
paused = False
pausedW = False

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
    screen.fill(white)
    events = pygame.event.get()

    if paused:
        question("well done")
        newQuestions()
    if pausedW:
        question("wrong")
        newQuestions()


    elif b2on==False and b3on==False and b4on==False and b5on==False and not paused and not pausedW:
        B2,TextSurf,TextRect = b.button() #draw buttons
        pygame.draw.rect(screen, [255, 0, 0], B2)
        screen.blit(TextSurf, TextRect)

        B3,TextSurf,TextRect = b1.button()
        pygame.draw.rect(screen, [255, 0, 0], B3)
        screen.blit(TextSurf, TextRect)

        B4,TextSurf,TextRect = b2.button()
        pygame.draw.rect(screen, [255, 0, 0], B4)
        screen.blit(TextSurf, TextRect)

        B5,TextSurf,TextRect = b3.button()
        pygame.draw.rect(screen, [255, 0, 0], B5)
        screen.blit(TextSurf, TextRect)


    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


    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()  


  • I've seen this exact question by some other user. Are you pair-programming? You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. You can remember which questions were asked and when getting new ones from your file do so until you have new ones checking with the remembered ones. – Patrick Artner Jun 08 '19 at 17:54
  • Learn about scopes - your `b,b1,b2,b3` inside `def newQuestions():` "die" when the function is done. You do not return them from it - you do not pack them into your gui. You also would not really need to create new buttons, changing the text should suffice: [how-to-remove-replace-text-in-pygame](https://stackoverflow.com/questions/10467863/how-to-remove-replace-text-in-pygame) – Patrick Artner Jun 08 '19 at 17:59
  • @Patrick Artner, I reposted with less code as I was guided to do and I thought this was sufficient enough for my problem.Sorry I did not realise it was still too much – CyanDeathReaper Jun 08 '19 at 18:26

1 Answers1

1

You will need to add return b,b1,b2,b3 to the end of the newQuestions function and then put b,b2,b3,b4 = newQuestions() replacing the existing newQuestions function call