954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bugs help

Please help me with this bug. I have python 3.0 and I was using pygame and for some reason it isn't reconizing

windowSurface


here Is the code Im having problems with and thanks in advance.

import pygame, sys, random 
from pygame.locals import * 
#*******************************************SETUPVAR**************************************************
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0)
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 
ORANGE = (255,180,0)
YELLOW=(230,230,0)

def newscreen():
    pygame.init() 
    mainClock = pygame.time.Clock()
    windowSurface = pygame.display.set_mode((700,600), 0, 32) 
    pygame.display.set_caption('Test')
    BLACK = (0, 0, 0) 
    WHITE = (255, 255, 255) 
    RED = (255, 0, 0) 
    GREEN = (0, 255, 0) 
    BLUE = (0, 0, 255) 
    ORANGE = (255,180,0)
    YELLOW=(230,230,0)
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    windowSurface.fill(BLACK)
    text = basicFont.render ('Test',False,WHITE,BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx-100
    textRect.centery = windowSurface.get_rect().centery-250
    windowSurface.blit(text,textRect)
    pygame.display.update()

def menu():
    newscreen()
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    one = basicFont.render('1', False, WHITE, RED) 
    two = basicFont.render('2',False,WHITE,GREEN)
    three = basicFont.render('3',False,WHITE,ORANGE)
    four = basicFont.render('4',False,WHITE,YELLOW)
    threeRect = vl.get_rect()
    threeRect.centerx = windowSurface.get_rect().centerx+200
    threeRect.centery = windowSurface.get_rect().centery-10
    twoRect = one.get_rect()
    twoRect.centerx = windowSurface.get_rect().centerx-200
    twoRect.centery = windowSurface.get_rect().centery-100
    oneRect = text.get_rect() 
    oneRect.centerx = windowSurface.get_rect().centerx +200
    oneRect.centery = windowSurface.get_rect().centery+150
    oneRect.size = (100,100)
    fourRect = end.get_rect()
    fourendRect.centerx = windowSurface.get_rect().centerx-200
    fourRect.centery = windowSurface.get_rect().centery+150
    windowSurface.blit(one, oneRect) 
    windowSurface.blit(two,twoRect)
    windowSurface.blit(three,threeRect)
    windowSurface.blit(four,fourRect)
    pygame.display.update()

menu()


# run the game loop 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit()
wolfeater017
Light Poster
35 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

In
threeRect = vl.get_rect()
where does v1 come from?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

The variable windowSurface is not a global value and is local to the newscreen() method. You can make it that newscreen() returns windowSurface, but it will make your program much more future proof if you wrap these methods in a class, and declare windowSurface as self.windowSurface so that it can be called between methods without passing variables between methods.

There are also a few other problems with the code, but the below fix should get your program running so you can debug, but I would strongly recommend wrapping these functions in a class.

'''
Created on Jun 12, 2010

@author: DESKTOP
'''


import pygame, sys, random 
from pygame.locals import * 
#*******************************************SETUPVAR**************************************************
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0)
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 
ORANGE = (255,180,0)
YELLOW=(230,230,0)

def newscreen():
    pygame.init() 
    mainClock = pygame.time.Clock()
    windowSurface = pygame.display.set_mode((700,600), 0, 32) 
    pygame.display.set_caption('Test')
    BLACK = (0, 0, 0) 
    WHITE = (255, 255, 255) 
    RED = (255, 0, 0) 
    GREEN = (0, 255, 0) 
    BLUE = (0, 0, 255) 
    ORANGE = (255,180,0)
    YELLOW=(230,230,0)
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    windowSurface.fill(BLACK)
    text = basicFont.render ('Test',False,WHITE,BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx-100
    textRect.centery = windowSurface.get_rect().centery-250
    windowSurface.blit(text,textRect)
    pygame.display.update()
    return windowSurface    #FIXED

def menu():
    windowSurface = newscreen() #FIXED
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    one = basicFont.render('1', False, WHITE, RED) 
    two = basicFont.render('2',False,WHITE,GREEN)
    three = basicFont.render('3',False,WHITE,ORANGE)
    four = basicFont.render('4',False,WHITE,YELLOW)
    threeRect = vl.get_rect()
    threeRect.centerx = windowSurface.get_rect().centerx+200
    threeRect.centery = windowSurface.get_rect().centery-10
    twoRect = one.get_rect()
    twoRect.centerx = windowSurface.get_rect().centerx-200
    twoRect.centery = windowSurface.get_rect().centery-100
    oneRect = text.get_rect() 
    oneRect.centerx = windowSurface.get_rect().centerx +200
    oneRect.centery = windowSurface.get_rect().centery+150
    oneRect.size = (100,100)
    fourRect = end.get_rect()
    fourendRect.centerx = windowSurface.get_rect().centerx-200
    fourRect.centery = windowSurface.get_rect().centery+150
    windowSurface.blit(one, oneRect) 
    windowSurface.blit(two,twoRect)
    windowSurface.blit(three,threeRect)
    windowSurface.blit(four,fourRect)
    pygame.display.update()

menu()


# run the game loop 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit()
tbone2sk
Junior Poster in Training
64 posts since Nov 2009
Reputation Points: 24
Solved Threads: 22
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: