if 语句中无法识别 Python 变量

标签 python variables if-statement while-loop

这里是所有代码,但在 main() 中,一个 while 循环检查 on_title_screen 是否为 true,如果是,则显示标题屏幕,如果不是,则显示游戏。但是,在启动程序、运行游戏并返回到标题屏幕后,尝试按开始按钮会使 if on_title_screen==True 和 elif on_title_screen==False 的代码同时运行,而仅应运行第一位。

import random
import pygame
from pygame import *
import math
import sys

#Presets for window
size=width,height=500,500
Ag=-9.80665
clock = pygame.time.Clock()
white=(255,255,255)
blue=(0,0,255)
red=(255,0,0)
gray_bgColor=(190,193,212)

#Initialise pygame Surface as screen
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Bird Replica")


def falling_loop():
    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_UP:
                preset.vY=-10
    if preset.yPos>height-50:
        preset.on_title_screen=True
    preset.vY+=1
    preset.yPos+=preset.vY

class presets():
    #Holds all the "global" values for the game
    vY=0
    xPos,yPos=200,100
    on_title_screen=True

class graphics():
    #Holds the methods for loading/displaying graphics
    def load_images(self):
        #Loads the background and sprite images
        self.background_image=pygame.image.load("flappy_background.png").convert()
        self.bird_image=pygame.image.load("flappy_sprite.jpg").convert()
        self.bird_image.set_colorkey(white)
        self.birdHitBox=self.bird_image.get_rect()
    def show_background(self):
        #blits the background
        screen.blit(self.background_image,[0,0])
    def show_bird(self):
        #blits the bird onto screen at xPos, yPos
        screen.blit(self.bird_image,[preset.xPos,preset.yPos])
    def refresh_display(self):
        #updates the display
        screen.blit(self.background_image,[0,0])
        falling_loop()
        self.show_bird()

class titleScreen():
    #Holds the methods for the title screen/menu
    def title(self):
        #Sets up the title
        titleText="Flappy Game"
        titlePos=(0,0)
        currentFont=pygame.font.SysFont("arialms",30,bold=True,italic=True)
        renderTitle=currentFont.render(titleText,1,blue,gray_bgColor)
        self.titlex,self.titley=currentFont.size(titleText)
        screen.blit(renderTitle,titlePos)
    def start(self):
        #Sets up the start Button
        startText="Start Game"
        self.startPos=(0,self.titley)
        currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
        renderStart=currentFont.render(startText,1,blue,gray_bgColor)
        self.startx,self.starty=currentFont.size(startText)
        self.start_rect = pygame.Rect(self.startPos[0],self.titley,self.startx,self.starty)
        screen.blit(renderStart,self.startPos)
    def quit(self):
        #Sets up the quit button
        quitText="Quit"
        self.quitPos=(0,self.starty+self.titley)
        currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
        renderQuit=currentFont.render(quitText,1,red,gray_bgColor)
        self.quitx,self.quity=currentFont.size(quitText)
        self.quit_rect = pygame.Rect(self.quitPos[0],self.titley+self.starty,self.quitx,self.quity)
        screen.blit(renderQuit,self.quitPos)
    def get_click(self):
        #Gets mouse click and processes outcomes
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                x,y=pygame.mouse.get_pos()
                #Tests for start:
                if self.start_rect.collidepoint(x,y):
                    print("start")
                    preset.on_title_screen=False
                    graphicsC.show_background()
                elif self.quit_rect.collidepoint(x,y):
                    print("quit")
                    sys.exit()

#Assign objects to respective classes
preset=presets()        
titleC=titleScreen()
graphicsC=graphics()

def setupTitle():
    #bundles all title_screen functions
    titleC.title()
    titleC.start()
    titleC.quit()

def main():
    graphicsC.load_images()
    graphicsC.show_background()
    setupTitle()
    while True:
        clock.tick(30)
        if preset.on_title_screen==False:
            graphicsC.refresh_display()
            print("working...")
        elif preset.on_title_screen==True:
            setupTitle()
            titleC.get_click()
        pygame.display.flip()

main()

最佳答案

您必须使用self.xxx来创建类属性。您还需要确保在使用 __init__ 方法初始化类时定义了这些值。所以使用:

class presets():
    #Holds all the "global" values for the game
    def __init__(self):
        self.vY=0
        self.xPos,self.yPos=200,100
        self.on_title_screen=True

但是,以这种方式创建“全局”变量可能不是最好的方法,如 Cyber mentions in the comments .

关于if 语句中无法识别 Python 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27129680/

相关文章:

python - 如何将腌制数据帧从 GCS 加载到 App Engine

java - 如何创建一个由用户输入定义长度的静态数组?

c# - 在 C# 中通过名称访问类变量

java - && boolean 运算符的评估

java - Monty Hall 让我们做一笔交易 用 java 模拟(10000 次迭代)

JavaScript "if"语句无法运行

python - 替换字符串中的子字符串

python - 如何使用 python Schedule 创建一个作业任务,仅在白天早上 6 点到晚上 10 点期间每 3 分钟运行一次

python - 如何在python中映射具有多个参数的函数

jQuery click(function()) 在第一次单击时不使用变量