Pygame转换/按钮不会产生持续变化

2024-04-16 05:02:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: andrectfalseifrenderpygamewinclick
2条回答

根据变量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。使用^{} 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()
^{pr2}$

相关问题 更多 >