以下是所有代码,但在main()中,while循环检查on_title_screen是否为true,如果是,则显示标题屏幕,但如果不是,则显示游戏。但是,在启动程序,运行游戏并返回标题屏幕后,尝试按下开始按钮会使得当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()
答案 0 :(得分:2)
您必须使用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。