2

I am attempting to make a ball bounce simulation using the Pygame module in Python 3.7.3. The class I have displays the balls but doesn't work with movement. The error is "local variable x referenced before assignment." I think this means that it is local but needs to be global, however to have the number of balls as a variable (so i can say how many to generate) i don't know how to fix this.

I've tried reading other questions but none have solved my issue. I am able to get a single ball bouncing around the screen with border collision working but not when i make it object oriented. I have also played around with self variables to refer to each individual ball but that didn't work.

class BallEntity():
    def __init__(self, radius):
        x = random.randint(radius, win_width - radius)
        y = random.randint(radius, win_height - radius)
        pos = x, y
        pygame.draw.circle(win, (248, 24, 148), pos, radius)
        dx = random.randint(-5, 5)
        dy = random.randint(-5, 5)
        BallEntity.movement(self)

    def movement(self):
        if x <= r1 or x >= win_width - r1:
            dx = -dx
        elif x > r1 and x < win_width -r1:
            x += dx

        if y <= r1 or y >= win_height - r1:
            dy = -dy
        elif self.y > r1 and self.y < win_height -r1:
            y += dy


numbBalls = 8
r1 = 8
for i in range(numbBalls):
    BallEntity.__init__(i, r1)

I expect the balls to print and move with collision working, but instead I get the error "local variable x referenced before assignment."

1 Answers1

-1
  • global win,x,y let us remove x not defined`
  • Also, x, y is used in internal class instead global variable to various circles point.

import random

import pygame, sys
import time
from pygame.locals import *

pygame.init()

win_width = 500
win_height = 400
radius = 90

win = pygame.display.set_mode((win_width, win_height), 0, 32)

class BallEntity():
    win = None

    x = 0
    y = 0

    dx = 1
    dy = 1

    radius = 0
    # preious position(used for delete previous circle after movement)
    prePos = 0, 0
    pos = 0, 0
    def __init__(self, i, radius):
        self.radius = radius

        # random position (x, y)
        self.x = random.randint(radius, win_width - radius)
        self.y = random.randint(radius, win_height - radius)

        self.dx = random.randint(-5, 5)
        self.dy = random.randint(-5, 5)

    def movement(self):
        global win

        if self.x <= r1 or self.x >= win_width - r1:
            self.dx = -self.dx
        elif self.x > r1 and self.x < win_width -r1:
            self.x += self.dx

        if self.y <= r1 or self.y >= win_height - r1:
            self.dy = -self.dy
        elif self.y > r1 and self.y < win_height -r1:
            self.y += self.dy

        self.pos = self.x, self.y
        # draw background color to delete previous position
        pygame.draw.circle(win, (0, 0, 0), self.prePos, self.radius)
        # draw circle
        pygame.draw.circle(win, (248, 24, 148), self.pos, self.radius)

        self.prePos = self.pos

numbBalls = 5
r1 = 8


balls = []



# Create balls and store list
for i in range(numbBalls):
    e = BallEntity(i, r1)
    balls.append(e)

while True:
    # update circle position via movement()
    for i in range(numbBalls):
        balls[i].movement()

    pygame.display.update()
    # delay for animation
    time.sleep(0.1)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
  • code without explanation is not prefered answer. -1 – furas Apr 20 '19 at 04:44
  • @furas how about now? – Wai Ha Lee Apr 20 '19 at 08:50
  • @WaiHaLee explanation explains nothing. There is no `global x,y` in code - so why it is mentioned? Code use `self.x, self.y` but there is no explanation for this. – furas Apr 20 '19 at 09:13
  • @furas - hmm: valid points. I mis-clicked and came across this answer. Saw an edit had been applied after your comment and thought it *might* be enough to justify removing your downvote. I don't care either way, but thought you might want your 1 rep back. – Wai Ha Lee Apr 20 '19 at 09:19