回答问题后如何生成新的随机按钮?

时间:2019-06-08 17:47:13

标签: python pygame

我正在努力做到这一点,所以当您回答屏幕上的一个问题时,主菜单上的四个按钮将更改,并显示来自quizfile.csv文件的一组新的4个问题。

我尝试再次加载问题,但是使用newQuestions函数是相同的


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


1 个答案:

答案 0 :(得分:1)

您将需要在b,b1,b2,b3函数的末尾添加return newQuestions,然后将b,b2,b3,b4 = newQuestions()替换为现有的newQuestions函数调用