How to make my code better? (completely beginner)

Hello, I am new here and have just started learning Processing sketch from this month.
I wonder if the code I wrote below can be better.

Thank you for your guidelines

x=50
y=50
move=3
def setup():
size(600,600)
def draw():
global x,y,move
background(0)
noStroke()
fill(255)

x=x+move
ellipse(x,y,100,100)
if x>50 and y<=50:
    move=3
if x>width-50:
    x=width-50
    y=y+move
if y>height-50:
    y=height-50
    move=-3
if x<50:
    x=50
    y=y+move
    move=-3
if y<50:
    move=3

I don’t think that this line is necessary since you define all 3 before setup

Make more empty lines

Use ctrl-t for automatically format

Welcome, @Jane_Dau

Firstly, when posting on this forum, place all of your code inside triple backticks like this, so the formatting looks good –

```python
x = 50
y = 50
...

```

Instead of of drawing circles with ellipse() functions, use the circle() function –

circle(x, y, 100)

Employ +=, -=, etc. assignment operators where you can. So, instead of y = y + move, use –

y += move

The noStroke() and fill(255) don’t change, so arguably, you could move those to the setup() block.

You write the integer 50 in many places; perhaps this should be a variable. The same goes for 3.

4 Likes

I am not sure what you try to achieve

The movement of a ball is:

add moveX to x and moveY to y
(so two different move variables)

and then change the 2 move variables on collision

Hello @Jane_Dau,

You are using the global Python statement correctly.

Reference:
https://py.processing.org/reference/globals.html

Examples here of use of the global Python statement and formatting here:
https://py.processing.org/tutorials/interactivity/

Additional resources here:
https://py.processing.org/

For readability (my personal preference) I add spaces:

ellipse(x,y,100,100)

ellipse(x, y, 100, 100) # For readability

:)

5 Likes

thank you for you reply.

1 Like

thank you for your advise @tabreturn

1 Like

I try to move the ball along the edge of window, start from x=50 and y=50

1 Like

Hello @Jane_Dau ,

Consider this approach (hints only and not complete):

    ellipse(x, y, 50, 50)
    
    if y <= 50:
        y = 50
        xd = 1 // 1, 0, or -1 for direction and speed of 1
        yd = 0 // 1, 0, -1 for direction and speed of 1
    
   // Rest of code here for each corner condition with similar approach to above
    
    x = x + xd*3
    // Do the same for y

I enjoyed this exploration into Python!

:)

3 Likes

thank you for your advise @glv

2 Likes

This is correct. I’m still quite confused by finding best practices for globals in processing.py. I here that globals are bad practice but it seems pretty difficult to not use them with the way the setup() and draw() functions work. If you are inside a local scope (in your code - within your draw function) you can still access globally defined variables (the ones you declared in your global/module scope right above the setup loop).

The problem is - you cannot change the value of a global variable from within a local scope. If you try to do this, (I think) python will create a brand new variable within your function with the same name. The changes will not be applied outside the scope of your function. In order to be able to make changes to the variable ‘x’ declared outside of your draw loop you need to write ‘global x’.

But i think your code will still work and appear to do the same thing if you don’t write ‘global x’. afterall you only use the changing value of x within your draw loop.

1 Like

Hello @tdwheeler,

Give it a try with and without the global x, y, move statement and report back. :)

A clean formatted version of above.

x=50
y=50
move=3

def setup():
    size(600,600)

def draw():
    global x, y, move
    background(0)
    noStroke()
    fill(255)

    x=x+move
    
    ellipse(x,y,100,100)
    if x>50 and y<=50:
        move=3
    if x>width-50:
        x=width-50
        y=y+move
    if y>height-50:
        y=height-50
        move=-3
    if x<50:
        x=50
        y=y+move
        move=-3
    if y<50:
        move=3

Reference:
https://py.processing.org/reference/globals.html

:)