More than one moving object

We want to be able to develop programs where more than one independent graphic object co-exists and interacts according to some rules. This is how most computer games work. Pilot training simulators and serious engineering design models are designed on the same principles. We start this process simply by working up to an application that ends up with two balls bouncing off the walls and each other under the influence of gravity and energy loss.

How to do it...

The following code is very similar to that in the previous recipe, except that two similar objects are created. They are independent of each other and do not interact in any way.

# two_balls_moving_1.py
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title("Two balls")
cw = 200 # canvas width
ch = 130 # canvas height
chart_1 = Canvas(root, width=cw, height=ch, background="white")
chart_1.grid(row=0, column=0)
cycle_period = 100 # time between new positions of the ball
# (milliseconds).
# The parameters defining ball no 1.
posn_x_1 = 1 # x position of box containing the ball (bottom).
posn_y_1 = 1 # y position of box containing the ball (left edge).
shift_x_1 = 1 # amount of x-movement each cycle of the 'for' loop.
shift_y_1 = 1 # amount of y-movement each cycle of the 'for' loop.
ball_width_1 = 12 # size of ball - width (x-dimension).
ball_height_1 = 12 # size of ball - height (y-dimension).
color_1 = "blue" # color of ball #1
# The parameters defining ball no 2.
posn_x_2 = 180 # x position of box containing the ball (bottom).
multiple objectsmovingposn_y_2 = 180 # x position of box containing the ball (left # edge).
shift_x_2 = -2 # amount of x-movement each cycle of the 'for' # loop.
shift_y_2 = -2 # amount of y-movement each cycle of the 'for' # loop.
ball_width_2 = 8 # size of ball - width (x-dimension).
ball_height_2 = 8 # size of ball - height (y-dimension).
color_2 = "green" # color of ball #2.
for i in range(1,100): # end the program after 50 position shifts.
posn_x_1 += shift_x_1
posn_y_1 += shift_y_1
posn_x_2 += shift_x_2
posn_y_2 += shift_y_2
chart_1.create_oval(posn_x_1, posn_y_1, posn_x_1 + ball_width_1,
posn_y_1 + ball_height_1, fill=color_1)
chart_1.create_oval(posn_x_2, posn_y_2, posn_x_2 + ball_width_2,
posn_y_2 + ball_height_2, fill=color_2)
chart_1.update() # This refreshes the drawing on the canvas.
chart_1.after(cycle_period) # This makes execution pause for 100 # milliseconds.
chart_1.delete(ALL) # This erases everything on the canvas
root.mainloop()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

The main point to note is that these programs, and many others in this book, are divided into five parts:

  1. Creating the environment where objects will exist.
  2. Defining the individual objects and their attributes.
  3. Defining the rules of engagement between objects.
  4. Creating the objects.
  5. Using a loop to simulate the march of time by changing properties such as position at rates that mimic real-time motion.
  6. Controlling the environment inside which the objects exist.

The environment in most of our examples is the Tkinter canvas. The objects that are going to exist inside the canvas environment in this example are two colored balls. The rules of engagement are that they will not have any effect on each other at all and they will not be affected by the edges of the canvas. Another rule of engagement is how their positions will shift each time the for loop is executed.

Finally the environment is controlled by the time regulated canvas.update() and canvas.delete(ALL) methods.

There's more...

The principle idea demonstrated in this recipe is that we can create more than one similar, but different objects exist and react independently. This gives rise to the idea of object-oriented programming.

Python offers more than one way to use the ideas of object-oriented programming. In this book, we use three ways of making objects: lists, dictionaries, and classes.

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

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