4 Replies - 1531 Views - Last Post: 30 May 2015 - 07:26 PM Rate Topic: -----

#1 slayerodd   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 79
  • Joined: 10-May 15

local variable 'ballx' referenced before assignment

Posted 28 May 2015 - 11:15 AM

I am trying to make a pong game and i get an error. Can someone explain what i have done wrong and also give me some tips? i feel rusty and clumsy while writing code.

This is the error message:
Traceback (most recent call last):
File "C:/Anton/python/pong/pong_game.py", line 92, in <module>
game_loop()
File "C:/Anton/python/pong/pong_game.py", line 74, in game_loop
ballx += ballxChange
UnboundLocalError: local variable 'ballx' referenced before assignment

and this is my code:

import pygame

pygame.init()

display_width = 800
display_height = 600

black =(0,0,0)
white =(255,255,255)


gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('pong')
clock = pygame.time.Clock()

racket1Img = pygame.image.load('racket.png')
racket1_width = 9
racket1_height = 64
racket2Img = pygame.image.load('racket.png')
racket2_width = 9
racket2_height = 64

ballImg = pygame.image.load('ball.png')
ball_width = 15
ball_height = 15

def ball(x,y):
    gameDisplay.blit(ballImg,(ballx,bally))

def racket1(x,y):
    gameDisplay.blit(racket1Img,(racket1x,racket1y))
def racket2(x,y):
    gameDisplay.blit(racket2Img,(racket2x,racket2y))

def quitgame():
    pygame.quit()
    quit()

def game_loop():
    racket2x = (display_width *0.8)
    racket2y = (display_height *0.5)
    racket1x = (display_width *0.8)
    racket1y = (display_height *0.5)
    

    ballxChange = 0
    ballyChange = 0
    
    racket1yChange = 0
    racket2yChange = 0

    bouncing = True

    while bouncing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quitgame()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    racket2yChange = -5
                if event.key == pygame.K_DOWN:
                    racket2yChange = 5
                if event.key == pygame.K_W:
                    racket1yChange = -5
                if event.key == pygame.K_S:
                    racket1yChange = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    racket2yChange = 0
                if event.key == pygame.K_W or event.key == pygame.K_S:
                    racket1yChange = 0

        ballx += ballxChange
        bally += ballyChange
        racket2y += racket2yChange
        racket1y += racket1yChange
        gameDisplay.fill(white)

        pygame.display.update()
        clock.tick(60)
        



            





game_loop()
pygame.quit()
quit()
                    
                    
    
    



Is This A Good Question/Topic? 0
  • +

Replies To: local variable 'ballx' referenced before assignment

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,025
  • Joined: 13-June 14

Re: local variable 'ballx' referenced before assignment

Posted 28 May 2015 - 11:20 AM

Exactly what it says - you're trying to modify a variable (in your case, you're trying to increment ballx) before you've declared it and given it an initial value (of presumably 0, or whatever is appropriate).

Here's a simple example:

x = 0
...

x += 1 # You can do this, because x has previously been defined. In your case, the variable hasn't been defined before you try and use it.


This post has been edited by ndc85430: 28 May 2015 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 slayerodd   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 79
  • Joined: 10-May 15

Re: local variable 'ballx' referenced before assignment

Posted 28 May 2015 - 11:23 AM

View Postndc85430, on 28 May 2015 - 11:20 AM, said:

Exactly what it says - you're trying to modify a variable (in your case, you're trying to increment ballx) before you've declared it and given it an initial value (of presumably 0, or whatever is appropriate).

Here's a simple example:

x = 0
...

x += 1 # You can do this, because x has previously been defined. In your case, the variable hasn't been defined before you try and use it.



how could i be so stupid... thanks!
Was This Post Helpful? 0
  • +
  • -

#4 slayerodd   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 79
  • Joined: 10-May 15

Re: local variable 'ballx' referenced before assignment

Posted 28 May 2015 - 11:31 AM

This is probably a simple and sloppy error but here we go.
I am trying to make a pong game and i have got an error

The error:
Traceback (most recent call last):
File "C:/Anton/python/pong/pong_game.py", line 97, in <module>
game_loop()
File "C:/Anton/python/pong/pong_game.py", line 83, in game_loop
ball(ballx,bally)
File "C:/Anton/python/pong/pong_game.py", line 28, in ball
gameDisplay.blit(ballImg,(ballx,bally))
NameError: name 'ballx' is not defined

The code:
import pygame

pygame.init()

display_width = 800
display_height = 600

black =(0,0,0)
white =(255,255,255)


gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('pong')
clock = pygame.time.Clock()

racket1Img = pygame.image.load('racket.png')
racket1_width = 9
racket1_height = 64
racket2Img = pygame.image.load('racket.png')
racket2_width = 9
racket2_height = 64

ballImg = pygame.image.load('ball.png')
ball_width = 15
ball_height = 15

def ball(x,y):
    gameDisplay.blit(ballImg,(ballx,bally))

def racket1(x,y):
    gameDisplay.blit(racket1Img,(racket1x,racket1y))
def racket2(x,y):
    gameDisplay.blit(racket2Img,(racket2x,racket2y))

def quitgame():
    pygame.quit()
    quit()

def game_loop():
    ballx = (display_width *0.5)
    bally = (display_height *0.5)
    
    racket2x = (display_width *0.8)
    racket2y = (display_height *0.5)
    racket1x = (display_width *0.8)
    racket1y = (display_height *0.5)
    

    ballxChange = 0
    ballyChange = 0
    
    racket1yChange = 0
    racket2yChange = 0

    bouncing = True

    while bouncing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quitgame()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    racket2yChange = -10
                if event.key == pygame.K_DOWN:
                    racket2yChange = 10
                if event.key == pygame.K_W:
                    racket1yChange = -10
                if event.key == pygame.K_S:
                    racket1yChange = 10
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    racket2yChange = 0
                if event.key == pygame.K_W or event.key == pygame.K_S:
                    racket1yChange = 0

        ballx += ballxChange
        bally += ballyChange
        racket2y += racket2yChange
        racket1y += racket1yChange
        gameDisplay.fill(white)

        ball(ballx,bally)

        pygame.display.update()
        clock.tick(60)
        



            





game_loop()
pygame.quit()
quit()
                    
                    
    
    


Was This Post Helpful? 0
  • +
  • -

#5 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: local variable 'ballx' referenced before assignment

Posted 30 May 2015 - 07:26 PM

def ball(x,y):
    gameDisplay.blit(ballImg,(ballx,bally))



There is no value called ballx or bally. However, there are values called x and y. Try
def ball(x,y):
    gameDisplay.blit(ballImg,(x,y))

You make the same type of mistake numerous times, so you'll need to make a lot of changes to fix it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1