#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Is there a better way of completing this? (bouncing)

edited August 2017

I have created a bouncing ball going back and forth (left to right). It works but I was wondering if there is a way to clean up some of the code to make it more efficient. The print statements are to help me out and the move is set to 50 so I didn't need to scroll through the console to see how it was doing. Move=5 if necessary.

``````diam=10
move=50

def setup():
size(500,500)
background(50,50,50)
startover()

def startover():
global placementY, x
x=width/2
placementY=int(random(10, 490))

def draw():
global x, move
background(50,50,50)
fill(255)
ellipse(x, placementY, diam, diam)
if x <=0:
print("value of X: ", x)
print("change over from left to right, reached ZERO")
move*=-1
x=x+move

elif (x < width):
print("value of X: ", x)
print("in between everything")
x=x+move

elif (x > 490):
print("made it here to 500: ", x)
move*=-1
x=x+move
``````
Tagged:

• You need to do the same for y so I suggest you write a function named keepinrange for this which changes move (which should be called moveX or so)

I also don't like *-1 and use -abs(move) OR abs(move) instead which is not likely to cause ball stuttering

• You need to do the same for y so I

1) Do you mean so that is can bounce on the ceiling and floor as well, along the y axis?

2) I was thinking about adding a way to make it go at an angle as well, but I haven't figured that out yet.

3) I used the -1 because I wanted it to move the opposite direction. If I use ABS, that will get rid of the negative, right? I am not sure how that would work...

• 1 yes

2 when you have something to add to y as well we have an angle

3 yeah abs ( move ) always positive, we want this on reflection on the left side

-abs always negative, we want this on the right side

• ... wondering if there is a way to clean up some of the code...

You can encapsulate both the data and the functions which act upon them inside a `class` structure:
http://py.Processing.org/reference/class.html

I've just converted a very old Java/JS Cross-Mode bounce sketch example to Python Mode. :bz

``````"""
' Clickable Spawning Balls (v4.4.3)
' by  Drov.fr  (2013/Jun)
' mod GoToLoop (2013/Dec) (2017/Aug/25)
'
' Forum.Processing.org/two/discussion/23916/
' is-there-a-better-way-of-completing-this#Item_4
'
' Forum.Processing.org/one/topic/nullpointerexeption-with-an-arraylist.html
' Forum.Processing.org/two/discussion/2171/effect-in-processing
'
"""

balls = []

def setup(SMOOTH=3, FPS=60):
size(600, 400, FX2D)
smooth(SMOOTH), frameRate(FPS), cursor(ARROW)
colorMode(RGB), blendMode(REPLACE), ellipseMode(CENTER)
fill(Ball.COLOUR), noStroke()

def draw(BG=0, TITLE='Balls: %02d  -  FPS: %02.f'):
background(BG)
for b in balls: b.script()
this.surface.title = TITLE % (len(balls), frameRate)

def mousePressed():
if mouseButton != CENTER: balls.append(Ball())
else: len(balls) and balls.pop()

def keyPressed():
if key == TAB: del balls[:]
elif key == BACKSPACE: len(balls) and balls.pop()
else: balls.append(Ball())

class Ball:
DIAM, MIN_SPD, MAX_SPD = 20, 1, 5 + 1
RAD, COLOUR = DIAM >> 1, 0xffFFFF00

def __init__(b):
b.x, b.y = mouseX, mouseY

b.vx = int(random(b.MIN_SPD, b.MAX_SPD))
if random(2) < 1: b.vx *= -1

b.vy = int(random(b.MIN_SPD, b.MAX_SPD))\
* (random(2) < 1 and -1 or 1)

def move(b): b.x += b.vx; b.y += b.vy

def bump(b):
if b.x < b.RAD or b.x > width - b.RAD: b.vx *= -1
b.vy *= -1 if b.y < b.RAD or b.y > height - b.RAD else 1

def show(b): ellipse(b.x, b.y, b.DIAM, b.DIAM)

def script(b): b.move(); b.bump(); b.show()
``````
• Um, if all the of your paths end with `x += move` then you can move that out of the condition.

And I second chrisir's directional stuff, don't just flip the direction, that's too prone to errors. (This must be a FAQ by now, surely)

• Koogs : great point. I have taken that out of the conditionals and placed it right after where the ellipse is created.

GotoLoop : I know I should be using OOP, but I am not there yet.

Chrisir : I am still trying to wrap my head around the abs stuff. I don't see any stuttering - maybe my monitor? could you be so kind as to show me the code for the abs stuff?

• move=abs(move)

move=-abs(move)

• Ok, here is the fixed version with the suggestions implemented from Chrisir and Koogs

``````diam=10
move=21

def setup():
size(500,500)
background(50,50,50)
startover()

def startover():
global placementY, x
x=width/2
placementY=int(random(10, 490))

def draw():
global x, move
background(50,50,50)
fill(255)
ellipse(x, placementY, diam, diam)
x=x+move
if x <=0:
print("value of X: ", x)
print("change over from left to right, reached ZERO")
move=abs(move)
#move*=-1

elif (x < width):
print("value of X: ", x)
print("in between everything")

elif (x > 490):
print("made it here to 500: ", x)
move=-abs(move)
#move*=-1
``````
• i would make a function for the collision

also, you don't take into account diam, which means that half of the ball is crossing the border at 0 and at width