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

Dani AI

Generated

Quick diagnosis — there are two separate issues visible in the thread. correctly flagged the undefined name used for threeRect (vl in the posted code is never created). correctly pointed out a scope problem: the display surface is created inside newscreen() so menu() can't see windowSurface. Those yield immediate NameError/AttributeError complaints at runtime; fixing them and using a single shared surface (or an object to hold it) will stop the crashes and make the code maintainable.

A simple, safe pattern is to initialize Pygame once and keep the main surface and fonts as attributes on an object. Example:

import pygame, sys

class Game:
    def __init__(self, size=(700,600)):
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont(None, 48)
        self.colors = [(255,0,0),(0,255,0),(255,180,0),(230,230,0)]

    def draw_menu(self):
        self.screen.fill((0,0,0))
        labels = ['1','2','3','4']
        for i, lbl in enumerate(labels):
            surf = self.font.render(lbl, True, (255,255,255), self.colors[i])
            rect = surf.get_rect(center=(self.screen.get_width()//2 + (i-1.5)*200,
                                         self.screen.get_height()//2 + (i-1.5)*150))
            self.screen.blit(surf, rect)
        pygame.display.flip()

    def run(self):
        self.draw_menu()
        while True:
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    pygame.quit(); sys.exit()
            self.clock.tick(30)

Game().run()

Quick checklist to finish debugging and avoid similar errors:

  • Read the traceback: it tells the exact line and missing name.
  • Search for typos (vl vs v1 vs three) and for variables used before assignment (text, end, fourendRect in your post).
  • Ensure you call .get_rect() on a Surface (rendered text or image), not on a font or an undefined name.
  • Prefer a single initialization of colors/fonts and one display surface (return it from an initializer or use a class attribute).
  • Use a linter (pyflakes/pylint) or an editor with static checking to catch undefined names before running.

Applying those fixes will eliminate the NameError problems and make the menu-drawing logic predictable and easy to extend.

Recommended Answers

All 2 Replies

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

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()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.