2014-11-25 34 views
0

当我运行这段代码时,它进入一个while循环并检查每一回合是否on_title_screen == True。如果这是真的,程序将继续检查输入,但如果它是假的,循环将刷新屏幕并开始游戏。然而,当点击开始时,on_title_screen = False,游戏仍然捕获鼠标输入,并且不显示它应该的鸟。Python基本控制流错误

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 Kid") 
#Game Presets 
vY=0 
xPos,yPos=200,100 
score=0 

on_title_screen=True 

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

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() 
     screen.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 refresh_display(self): 
     #updates the display 
     screen.blit(self.background_image,[xPos,yPos],self.birdHitBox) 
     falling_loop() 
     screen.blit(self.bird_image,[xPos,yPos]) 

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") 
        on_title_screen=False 
       elif self.quit_rect.collidepoint(x,y): 
        print("quit") 
        sys.exit() 

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 on_title_screen==False: 
      graphicsC.refresh_display() 
     elif on_title_screen==True: 
      titleC.get_click() 
     pygame.display.flip() 

main() 
+0

第一个猜测:你有一个范围界定错误,你在两个不同的地方得到两个叫'on_title_screen'的东西,它们有不同的值。 Google Python范围,'globals'关键字和实例变量... – TessellatingHeckler 2014-11-25 01:55:40

回答

1

我觉得@TessellatingHeckler是对的,on_title_screen是一个影子变量,不一样。

在这段代码中,没有任何地方可以将on_title_screen(global)设置为False。

一个更强大的答案是解释如何找到问题。我强烈建议使用pdb或ipdb。在这种情况下,我会在while循环中放置一个,并确保这些变量是我认为应该是的。