Python Pygame过渡/按钮未创建持久更改

Python Pygame过渡/按钮未创建持久更改,python,pygame,Python,Pygame,我正在尝试创建一个游戏,在你点击正确答案后,会出现一个动画,并出现“恭喜”或“再试一次”字样。但是,动画不起作用,文字只在我按下按钮时出现。我怎样才能解决这个问题 我试着将所有代码放在一个while循环中,但这导致游戏窗口根本没有响应 #Import Packages import pygame, sys from pygame.locals import * #Initialize Package pygame.init() win = pygame.display.set_mode ((6

我正在尝试创建一个游戏,在你点击正确答案后,会出现一个动画,并出现“恭喜”或“再试一次”字样。但是,动画不起作用,文字只在我按下按钮时出现。我怎样才能解决这个问题

我试着将所有代码放在一个while循环中,但这导致游戏窗口根本没有响应

#Import Packages
import pygame, sys
from pygame.locals import *

#Initialize Package
pygame.init()
win = pygame.display.set_mode ((640,600))
pygame.display.set_caption("EduCAUTION")

#Colours init
crushed = (142, 71, 71)
white = (250, 250, 250)

#Images
math1BG = pygame.image.load('mathlvl1BG.jpg')
building = pygame.image.load('building.png')
skybox = pygame.image.load('skybox.png')

#Text init
pygame.font.init()
myfont = pygame.font.SysFont('Arial', 50)
subtitle = pygame.font.SysFont('Arial', 25)
normal = pygame.font.SysFont('comicsans', 20)

#Division Game text
divtitle = myfont.render('CAUTION with Division', False, (crushed))
divrules = subtitle.render('Click on the right answer to help the worker come down the building', False, (75,75,75))
divQ1 = subtitle.render('Divide 42 by me and you will get 7. I am..', False, (crushed))
divQ2 = subtitle.render('If you divide 30 by me, the answer is 3 doubled. I am..', False, (crushed))
congrats = myfont.render('Congrats!! Thats correct!', False, (white))
again = myfont.render('Try again!', False, (white))
opt1 = myfont.render('6', False, (75,75,75))
opt2 = myfont.render('5', False, (75,75,75))
opt3 = myfont.render('1', False, (75,75,75))

def mathGame2():
    xbox = 140
    ybox = 240
    win.blit(math1BG, (0,0))
    win.blit(divtitle, (80, 15))
    win.blit(divrules, (20,80))
    win.blit(building, (-220,80))
    win.blit(skybox, (xbox, ybox))
    game2 = 0
    #Level 1 ##########################################################
    if game2 == 0:
        win.blit(divQ1, (200, 200))
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        woah = 10
        #Choice 1
        if 200+100 > mouse[0] > 200 and 270+50 > mouse[1] > 270:
            pygame.draw.rect(win, white,(200,270,100,50))
            win.blit(opt2, (210, 270))
            if click[0] == 1 and click != None:
                woah = 0

        else:
            pygame.draw.rect(win, crushed,(200,270,100,50))
            win.blit(opt2, (210, 270))
        #Choice 2
        if 350+100 > mouse[0] > 350 and 270+50 > mouse[1] > 270:
            pygame.draw.rect(win, white,(350,270,100,50))
            win.blit(opt1, (360, 270))
            if click[0] == 1 and click != None:
                woah = 1

        else:
            pygame.draw.rect(win, crushed,(350,270,100,50))
            win.blit(opt1, (360, 270))
        #Choice 3
        if 500+100 > mouse[0] > 500 and 270+50 > mouse[1] > 270:
            pygame.draw.rect(win, white,(500,270,100,50))
            win.blit(opt3, (510, 270))
            if click[0] == 1 and click != None:
                woah = 0

        else:
            pygame.draw.rect(win, crushed,(500,270,100,50))
            win.blit(opt3, (510, 270))
        pygame.display.update()

        if woah == 1:
            win.blit(congrats, (200, 500))
            while ybox > 400:
                ybox += 10
                win.blit(skybox, (xbox, ybox))
            game2 += game2
        if woah == 0:
            win.blit(again, (200, 500))

#Main game Loop
run = True
while run:
    #Quit game
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    mathGame2()

#End Game
pygame.quit()

根据变量
woah
绘制结束符号。您可以在游戏的每次迭代中设置
woah=10

if game2 == 0:
    win.blit(divQ1, (200, 200))
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    woah = 10  # Here!

因此,当您不按下按钮时,
woah
将重置,因此不会绘制结束符号。您必须在游戏开始之前设置
woah
,而不是在循环中。

要完成@Rockybilly的答案,当然变量
woah
必须在主循环之前初始化。使用访问函数
mathGame2
中的变量

但同样重要的是,在将文本绘制到显示器上后,执行
pygame.display.update()

def mathGame2():
全球woah
# [...]
如果game2==0:
# [...]

#非常感谢!这彻底解决了我的问题。