Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#MonthOfCode day 5 - bounce
import os
from time import sleep
WIDTH = 32;
def old_school_disp( x_pos, velocity):
os.system('clear')# replace by os.system('cls') on windows systems
pos = int(x_pos )
for i in xrange(WIDTH):
if(pos == i ):
print 'O',
else:
print '.',
print ;
print 'X position:',x_pos
print 'velocity:',velocity
sleep(1.0/velocity) # waits 1sec
def is_collision( x_pos, speed):
return( (x_pos+speed)<= 0. or (x_pos+speed)>=WIDTH )
x_speed = -1.0
x_pos = WIDTH/2
old_school_disp( x_pos, abs(x_speed) )
while( x_speed<WIDTH ):
if( is_collision(x_pos, x_speed) ):
x_speed *= -1 #invert direction
x_speed *= 1.20 #increase speed by 20% when it bounces
if( abs(0 -x_pos) < abs(WIDTH- x_pos)):
x_pos=0
else:
x_pos=WIDTH-1
old_school_disp( x_pos, abs(x_speed) )
continue
x_pos += x_speed
old_school_disp( x_pos, abs(x_speed) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment