Python variable not recognized in if-statement
Question: Here is all the code, but in main(), a while loop checks if on_title_screen is
true, and if it is, displays the title screen, but if not, displays the game.
However, after starting the program, running the game, and returning to the
title screen, attempting to press the start button makes both the code for if
on_title_screen==True and elif on_title_screen==False run when only the first
bit should run.

    
    
    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()
    


Answer: You have to use `self.xxx` to make class attributes. You also need to make
sure those values are defined when initializing the class using the `__init__`
method. So use:

    
    
    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
    

However, making 'global' variables this way probably isn't the best way, as
[Cyber mentions in the
comments](http://stackoverflow.com/questions/27129680/python-variable-not-
recognized-in-if-statement#comment42758526_27129680).

