Where should my size(640, 480) go in my py.processing code?

Hello, I am having some confusion about where my size() code will go.

`

Balls = []
class Ball:
    def __init__(self, x, y, xvel, yvel, radius):
        global Balls
        self.x = x
        self.y = y
        self.xvel = xvel
        self.yvel = yvel
        self.radius = radius
        m = radius*.1
    
        self.ball = self
        fill(250)
        self.drawing = ellipse(x, y, radius, radius)
    
        Balls.append(self)
    
def update(self):
    self.x += xvel
    self.y += yvel
    
def checkWall(self):
    if self.x > 640 - self.r:
        self.xvel = -self.xvel
    if self.x < 0 + self.r:
        self.xvel = -self.xvel
    if self.y > 480 - self.r:
        self.yvel = -self.yvel
    if self.y < 0 + self.r:
        self.yvel = -self.yvel
        
        
    draw()
        
    def display(self):
        noStroke()
        fill(255)
        ellipse(self.x, self.y, radius, radius)
        
def draw():
    background(255)

if len(Balls) > 0:

    for b in Balls:
        b.update()
        b.display()
        b.checkWall()
    
def settings():
    size(640, 480)

settings()

Ball(100, 100, 20, 10, 50)
Ball(500, 300, 10, 3, 200)`

I currently get the error: ’size() cannot be used here.’

Any place size() could go?

does that help?
https://py.processing.org/reference/size.html

1 Like

You are using animation, so you’ll have to include a setup() function (not settings). The size() is placed within this. However, there are several issues with your script. I’ve added the setup() function, but had to make a few other adjustments to get it working:

Balls = []

class Ball:
    def __init__(self, x, y, xvel, yvel, radius):
        global Balls
        self.x = x
        self.y = y
        self.xvel = xvel
        self.yvel = yvel
        self.radius = radius
        self.r = self.radius/2
        m = radius*.1
    
        self.ball = self
        fill(250)
        self.drawing = ellipse(x, y, radius, radius)
    
        Balls.append(self)

    def update(self):
        self.x += self.xvel
        self.y += self.yvel
        
    def checkWall(self):
        if self.x > 640 - self.r:
            self.xvel = -self.xvel
        if self.x < 0 + self.r:
            self.xvel = -self.xvel
        if self.y > 480 - self.r:
            self.yvel = -self.yvel
        if self.y < 0 + self.r:
            self.yvel = -self.yvel
            
    def display(self):
        fill(255)
        ellipse(self.x, self.y, self.radius, self.radius)

def draw():
    background(255)
    
    if len(Balls) > 0:
    
        for b in Balls:
            b.update()
            b.display()
            b.checkWall()

def setup():
    size(640, 480)
    global Balls
    Ball(100, 100, 20, 10, 50)
    Ball(500, 300, 10, 3, 200)

There’s still some redundant code in there. You might want to look at removing what you do not need.

1 Like

Thanks, mate! I’m still learning what all the functions are and how it works.

But thanks again for your help!

Thanks, mate! I’m still learning what all the functions are and how it works.

But thanks again for your help!

To learn those basics see the introduction tutorials, particularly the Overview, here

Interactive programs are drawn as a series of frames, which you can create by adding functions titled setup() and draw() as shown in the code below. These are built-in functions that are called automatically.

1 Like