pygame 中基于测验的游戏

问题描述 投票:0回答:1

我正在 pygame 中制作一个数学问题游戏。我有 8 个单独的选择,因此我将它们分成函数。现在我将所有问题和答案都保存在 txt 文件中,并将其读入数组。

我想做的是弹出第一个问题,我选择我的答案,计算机告诉我是对还是错,然后弹出一个按钮进入下一个问题。由于某种原因,它不想正确循环,随机函数都会生成数字。

我做错了什么?

import pygame, time, sys, pygbutton, random, time
from pygame.locals import *

pygame.init()
mainclock = pygame.time.Clock()
width = 1024
height = 768
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('Dividing Fractions')
background = pygame.image.load("tv2.png").convert()
background_poistion = [0,0]
screen.blit(background,background_poistion)
pygame.display.flip()

######tester
def tester_Function():
    i = 0
    
    questionUNanswered = 1
    Section1Q = [0]*5
    Section1Ques = open('questions1.txt','r')
    with open('questions1.txt','r') as file:
        Section1Q = file.readlines()
        
    Section1A = [0]*5
    Section1Ans = open('answer1.txt','r')
    with open('answer1.txt','r') as file:
        Section1A = file.readlines()
    num1 = str(random.randint(0,5))
    num2 = str(random.randint(0,5))
    question = pygbutton.PygButton((100,40,200,30),Section1Q[i])
    tester2 = pygbutton.PygButton((100,70,200,30),'Clicl Here')
    Ranswer = pygbutton.PygButton((100,110,200,30),Section1A[i])
    Wanswer1 = pygbutton.PygButton((100,150,200,30),num1)
    Wanswer2 = pygbutton.PygButton((100,190,200,30),num2)
    winner = pygbutton.PygButton((500,190,200,30),'')
    
    question.draw(screen)
    tester2.draw(screen)
    Ranswer.draw(screen)
    Wanswer1.draw(screen)
    Wanswer2.draw(screen)
    winner.draw(screen)
    buttonEvent = Ranswer.handleEvent(event)
    buttonEvent2= Wanswer1.handleEvent(event)
    buttonEvent3= Wanswer2.handleEvent(event)
    
    
                    
    if 'click' in buttonEvent:                
            winner = pygbutton.PygButton((500,190,200,30),'Winner')
            winner.draw(screen)
            i = i+1
    if 'click' in buttonEvent2 :                                                
            winner = pygbutton.PygButton((500,190,200,30),'Lose')
            winner.draw(screen)
            i = i+1
    if 'click' in buttonEvent3 :
            winner = pygbutton.PygButton((500,190,200,30),'Lose')
            winner.draw(screen)
            i= i +1
                       
            
    question.draw(screen)
    tester2.draw(screen)
    Ranswer.draw(screen)
    Wanswer1.draw(screen)
    Wanswer2.draw(screen)
    winner.draw(screen)              
                        
    
while True: # main game loop
    for event in pygame.event.get(): # event handling loop
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()
        
    tester_Function()
    
    pygame.display.update()
python pygame
1个回答
0
投票

在主循环中,您调用

tester_Function()
,因此每个循环都从头开始。每次循环读取文件,将
i
设置为零,创建按钮等。一些功能应该在主循环之前完成。

© www.soinside.com 2019 - 2024. All rights reserved.