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.
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() # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The main point to note is that these programs, and many others in this book, are divided into five parts:
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.
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.
13.59.32.1