A ball that bounces

Now and in the next three examples, we add rules of engagement that are increasingly complex. The overall objective is to introduce behaviors and interactions into our artificial world to make it behave more like the real world. We use numbers, calculations, and graphical drawings to represent aspects of the real world as we know it.

The first new behavior is that our colored disks will bounce elastically off the walls of the container that is the Tkinter canvas.

How to do it...

The code has purposely been kept as similar as possible to the previous four examples so that we feel we are still in familiar territory as the world we create gets increasingly more complicated. If we did not do this, we would get lost and bewildered. The whole secret in successfully constructing complex computer programs is to build it up gradually and systematically piece-by-piece. It is not a planned journey along a well-mapped road but rather a strenuous exploration through uncharted jungle.

# bounce_ball.py
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
import time
root = Tk()
root.title("The bouncer")
cw = 200 # canvas width
ch = 120 # canvas height
chart_1 = Canvas(root, width=cw, height=ch, background="white")
chart_1.grid(row=0, column=0)
cycle_period = 50 # time between new positions of the ball
# (milliseconds).
# The parameters determining the dimensions of the ball and its position.
posn_x = 1 # x position of box containing the ball (bottom).
posn_y = 1 # x position of box containing the ball (left edge).
shift_x = 1 # amount of x-movement each cycle of the 'for' loop.
shift_y = 1 # amount of y-movement each cycle of the 'for' loop.
ball_width = 12 # size of ball - width (x-dimension).
ball_height = 12 # size of ball - height (y-dimension).
color = "firebrick" # color of the ball
# Here is a function that detects collisions with the walls of the # container
# and then reverses the direction of movement if a collision is # detected.
def detect_wall_collision():
global posn_x, posn_y, shift_x, shift_y, cw, cy
if posn_x > cw : # Collision with right-hand container wall.
shift_x = -shift_x # reverse direction.
if posn_x < 0 : # Collision with left-hand wall.
shift_x = -shift_x
if posn_y > ch : # Collision with floor.
ballbouncingshift_y = -shift_y
if posn_y < 0 : # Collision with ceiling.
shift_y = -shift_y
for i in range(1,1000): # end the program after1000 position shifts.
posn_x += shift_x
posn_y += shift_y
chart_1.create_oval(posn_x, posn_y, posn_x + ball_width,
posn_y + ball_height, fill=color)
detect_wall_collision() # Has the ball collided with # any container wall?
chart_1.update() # This refreshes the drawing on the canvas.
chart_1.after(cycle_period) # This makes execution pause # for 200 milliseconds.
chart_1.delete(ALL) # This erases everything on the canvas.
root.mainloop()
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

The new feature here is the function detect_Wall_Collision(). Whenever it is called, it checks whether the position of the ball has moved outside the boundary of the canvas. If it has, the direction of the ball is reversed. This method is crude because it does not compensate for the size of the ball. Consequently the ball pops out of existence.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.211.106