gpt4 book ai didi

if 语句中无法识别 Python 变量

转载 作者:行者123 更新时间:2023-12-01 04:57:10 24 4
gpt4 key购买 nike

这里是所有代码,但在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com