Pygame过渡/按钮无法创造持久的变化

时间:2019-06-09 01:18:46

标签: python pygame

我正在尝试创建一个游戏,在其中单击正确的答案后,会出现动画,并出现“恭喜”或“重试”字样。但是,动画不起作用,并且仅当我按下按钮时才会显示文字。我该如何解决?

我试图将所有代码放入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()

2 个答案:

答案 0 :(得分:1)

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

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

def mathGame2():

    global woah

    # [...]

    if game2 == 0:

       # [...]

       #woah = 10 <----- delete 

       # [...]

       # pygame.display.update() <---- delete

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

        pygame.display.update()
woah = 10
run = True
while run:
    #Quit game
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    mathGame2()

答案 1 :(得分:0)

您可以根据变量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