# How to make my code better? (completely beginner)

**URL:** https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980
**Category:** Processing.py
**Created:** [March 28, 2022, 12:12pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980 "2022-03-28T12:12:10Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Jane\_Dau](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jane\_Dau](https://discourse.processing.org/u/Jane_Dau)
#### Post date: [March 28, 2022, 12:12pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/1 "2022-03-28T12:12:10Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 28, 2022, 4:13pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/2 "2022-03-28T16:13:51Z")

</div>

> [@Jane\_Dau](#):
>
> global x,y,move

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

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [March 28, 2022, 6:39pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/3 "2022-03-28T18:39:26Z")

</div>

Welcome, @Jane_Dau

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

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

```

````

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

```auto
circle(x, y, 100)

```

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

```auto
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`.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 28, 2022, 6:42pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/4 "2022-03-28T18:42:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 29, 2022, 10:52am UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/5 "2022-03-29T10:52:37Z")

</div>

> [@Chrisir](#):
>
> > [@Jane\_Dau](#):
> >
> > global x,y,move
> 
> I don’t think that this line is necessary since you define all 3 before setup

Hello @Jane_Dau,

You are using the _global Python statement_ correctly.

Reference:  
[https://py.processing.org/reference/globals.html](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/](https://py.processing.org/tutorials/interactivity/)

Additional resources here:  
[https://py.processing.org/](https://py.processing.org/)

For readability (my personal preference) I add spaces:

```auto
ellipse(x,y,100,100)

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

```

`:)`

---

<div class="post-metadata">

### Author: ![Jane\_Dau](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jane\_Dau](https://discourse.processing.org/u/Jane_Dau)
#### Post date: [March 30, 2022, 9:29pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/6 "2022-03-30T21:29:00Z")

</div>

thank you for you reply.

---

<div class="post-metadata">

### Author: ![Jane\_Dau](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jane\_Dau](https://discourse.processing.org/u/Jane_Dau)
#### Post date: [March 30, 2022, 9:30pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/7 "2022-03-30T21:30:31Z")

</div>

thank you for your advise @tabreturn

---

<div class="post-metadata">

### Author: ![Jane\_Dau](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jane\_Dau](https://discourse.processing.org/u/Jane_Dau)
#### Post date: [March 30, 2022, 9:32pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/8 "2022-03-30T21:32:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 30, 2022, 9:44pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/9 "2022-03-30T21:44:25Z")

</div>

Hello @Jane_Dau ,

Consider this approach (hints only and not complete):

```auto
    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!

`:)`

---

<div class="post-metadata">

### Author: ![Jane\_Dau](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jane\_Dau](https://discourse.processing.org/u/Jane_Dau)
#### Post date: [March 31, 2022, 12:17pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/10 "2022-03-31T12:17:31Z")

</div>

thank you for your advise @glv

---

<div class="post-metadata">

### Author: ![tdwheeler](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@tdwheeler](https://discourse.processing.org/u/tdwheeler)
#### Post date: [May 27, 2022, 2:32pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/11 "2022-05-27T14:32:03Z")

</div>

> [@Jane\_Dau](#):
>
> x,y,m

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.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [May 27, 2022, 3:20pm UTC](https://discourse.processing.org/t/how-to-make-my-code-better-completely-beginner/35980/12 "2022-05-27T15:20:12Z")

</div>

Hello @tdwheeler,

> [@tdwheeler](#):
>
> 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.

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

A clean formatted version of above.

```auto
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](https://py.processing.org/reference/globals.html)

`:)`
