在pygame中基于测验的游戏

时间:2013-11-30 09:36:59

标签: pygame

好的,所以我在pygame中制作一个数学问题游戏。我有8个单独的选项,所以我将它们分成功能。现在我将所有问题和答案都放在txt文件中,我正在读入数组。我想要做的是弹出第一个问题,我选择我的答案,计算机告诉我是对还是错,然后弹出一个按钮转到下一个问题。出于某种原因,它不希望循环向右,随机函数alls生成数字。我做错了什么?

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

1 个答案:

答案 0 :(得分:0)

在主循环中,您调用tester_Function(),以便在开始时启动每个循环。您读取文件的每个循环,将i设置为零,创建按钮等。某些函数应在主循环之前完成。