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