【问题标题】:Pygame Loop IssuesPygame 循环问题
【发布时间】:2017-08-18 10:11:15
【问题描述】:

我目前正在做一个学校项目,在这个项目中你要回答不同难度的数学问题才能获胜。我正在使用 Pygame 与 Python 一起创建它,但遇到了一个我没有发现问题的问题,我想知道是否有人可以阐明一下!

问题在于我正在使用的游戏循环。游戏开始时会加载标题屏幕,屏幕上有音乐和一组按钮。如果这些按钮悬停在上面,它们会变成不同的颜色来表示这一点。目前我正在搞乱播放按钮,并希望它带你进入一个新屏幕。目前我已经设置好了,这样它就可以让你脱离标题屏幕循环,让你进入一个简单的蓝绿色背景,没有别的东西。然而,虽然按钮显然被注册为单击,但游戏似乎保持 Intro 循环完好无损,并没有切换到应该满足的新循环,“Play Clicked”

我的代码是这样的:

import pygame, sys
from pygame.locals import *
pygame.init()

#Initialises the music mixer and then loads the music
#file from the directory of the executable, sets the volume
#and sets it to infinite loop
pygame.mixer.init()
pygame.mixer.music.load("Wepa.mp3")
pygame.mixer.music.set_volume(0.4)
pygame.mixer.music.play(-1,0.0)
#-1 Causes an infinite loop.
#The track starts at the beginning, 0.0.

#Sets the cursor to be visible
pygame.mouse.set_visible

#Sets the FPS
FPS = 60 
fpsClock = pygame.time.Clock()

#Creates a display surface, the main window for the game, and sets a title.
DISPLAYSURF = pygame.display.set_mode((800, 600))
#Resolution of 800x600.
pygame.display.set_caption("Maths Mania")

#Creates a background colour, I used teal simply to test if it's working
#as the base colour of the window is black anyway.
TEAL = (0, 128, 128)

#Gets the title screen image from the same directory as the executable
#and sets the coordinates of the top left corner, in this case, the top left
#of DISPLAYSURF, therefore 0,0.
TitleScreen = pygame.image.load('TitleScreenButtonless.png')
Titlex=0
Titley=0

#Creates sprites for the individual buttons on the title screen and sets their top left
#corner.
PlayButton=pygame.image.load('PlayButton.png')
PlayButtonx=284
PlayButtony=235

PlayButtonHovered=pygame.image.load('PlayButtonHovered.png')
PlayButtonHoveredx=284
PlayButtonHoveredy=235

OptionsButton=pygame.image.load('OptionsButton.png')
OptionsButtonx=284
OptionsButtony=329

OptionsButtonHovered=pygame.image.load('OptionsButtonHovered.png')
OptionsButtonHoveredx=284
OptionsButtonHoveredy=329

QuitButton=pygame.image.load('QuitButton.png')
QuitButtonx=284
QuitButtony=425

QuitButtonHovered=pygame.image.load('QuitButtonHovered.png')
QuitButtonHoveredx=284
QuitButtonHoveredy=425

Intro=True
PlayClicked=False

def PlayButtonClicker(PlayButtonx,PlayButtony,width,height,action=None):
    cursor=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if (PlayButtonx+width)>cursor[0]>PlayButtonx and (PlayButtony+height)>cursor[1]>PlayButtony:
        DISPLAYSURF.blit(PlayButtonHovered, (PlayButtonx, PlayButtony))
        if click[0]==1 and action!=None:
            if action=="quit":
                pygame.quit()
                quit()

            elif action=="Playbutton":
                print("1")
                PlayClicked==True
                Intro==False

def OptionsButtonClicker(OptionsButtonx,OptionsButtony,width,height,action=None):
    cursor=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if (OptionsButtonx+width)>cursor[0]>OptionsButtonx and (OptionsButtony+height)>cursor[1]>OptionsButtony:
        DISPLAYSURF.blit(OptionsButtonHovered, (OptionsButtonx, OptionsButtony))
        if click[0]==1 and action!=None:
            if action=="quit":
                pygame.quit()
                quit()

def QuitButtonClicker(QuitButtonx,QuitButtony,width,height,action=None):
    cursor=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if (QuitButtonx+width)>cursor[0]>QuitButtonx and (QuitButtony+height)>cursor[1]>QuitButtony:
        DISPLAYSURF.blit(QuitButtonHovered, (QuitButtonx, QuitButtony))
        if click[0]==1 and action!=None:
            if action=="quit":
                pygame.quit()
                quit()

while Intro==True: #THIS IS THE MAIN GAME LOOP, EVERYTHING IN THIS LOOP IS THE GAME    

    DISPLAYSURF.fill(TEAL)
    #Fills the display window with the background colour
    DISPLAYSURF.blit(TitleScreen, (Titlex, Titley))
    #Fills the display window with the TitleScreen image and tells it
    #where to place the top left corner of said image.

    #Places the buttons on the title screen
    DISPLAYSURF.blit(PlayButton, (PlayButtonx, PlayButtony))
    DISPLAYSURF.blit(OptionsButton, (OptionsButtonx, OptionsButtony))
    DISPLAYSURF.blit(QuitButton, (QuitButtonx, QuitButtony))

    PlayButtonClicker(284,235,231,64,action='Playbutton')
    OptionsButtonClicker(284,329,231,64,action='Optionsbutton')
    QuitButtonClicker(284,425,231,64,action='Quitbutton')




#The following lines check each event that happens in the game. If any of those
#events should be to quit, the game exits.

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

#These couple lines update the display and the FPS clock.
    pygame.display.update()
    fpsClock.tick(FPS)


while PlayClicked==True:

    DISPLAYSURF.fill(TEAL)
    Nonsense=pygame.image.load('Nonsense.png')
    Nonsensex=0
    Nonsensey=0
    DISPLAYSURF.blit(Nonsene, (Nonsensex, Nonsensey))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.display.update()
    fpsClock.tick(FPS)

【问题讨论】:

  • 请将您在此处发布的代码缩减为minimum,并用您填充一些颜色的pygame.Surfaces 替换图像,例如image = pygame.Surface((30, 50)),然后是image.fill((30, 90, 150))

标签: python-3.x pygame


【解决方案1】:
        elif action=="Playbutton":
            print("1")
            PlayClicked==True
            Intro==False

更改此设置,以便在 PlayClicked 和 Intro 中使用单个 = 符号而不是 ==。大概您正在尝试设置这些变量的值而不是检查是否相等:P

【讨论】:

    【解决方案2】:

    您需要将 PlayClickedIntro 声明为全局变量,然后按照 Astrolab 告诉您的操作将双精度 == 更改为 =

    但是,通常应该避免使用全局变量,因为它们通常会使代码更难理解并且更容易出错,因此最好重构程序。

    def PlayButtonClicker(PlayButtonx,PlayButtony,width,height,action=None):
        global PlayClicked
        global Intro
        cursor=pygame.mouse.get_pos()
        click=pygame.mouse.get_pressed()
        if (PlayButtonx+width)>cursor[0]>PlayButtonx and (PlayButtony+height)>cursor[1]>PlayButtony:
            DISPLAYSURF.blit(PlayButtonHovered, (PlayButtonx, PlayButtony))
            if click[0]==1 and action!=None:
                if action=="quit":
                    pygame.quit()
                    sys.exit()
                elif action=="Playbutton":
                    print("1")
                    PlayClicked = True
                    Intro = False
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2014-05-23
    • 1970-01-01
    • 2018-11-05
    • 2011-09-13
    • 2013-10-06
    相关资源
    最近更新 更多