Python development _tkinter_ the ball perfectly elastic collision download games

Complete this ball game inspired by the perfect elastic collision:

The following is a small I spent a week time write perfectly elastic collision game:

Game initialization state:

Mobile speed below the cursor and modify the ball

===================================================

Source:

===================================================

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
  7     Judge
  8     Two balls
  9     {
 10         Center: A (x1, Y1) radius: R X speed: Vax Y shaft speed: Vay
 11         Center: B (X2, Y2) radius: R X speed: Vbx Y shaft speed: Vby
 12     }
 13     Collision condition: 
 14     1 the two ball of center distance of not more than two ball radius and (r+R), i.e.: 
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
 18     The collision of 2 small ball, the number of exchange of two balls, i.e.: 
 19     {
 20         tempVax = Vax
 21         tempVay = Vay
 22         Vax = Vbx
 23         Vay = Vby
 24         Vbx = tempVax
 25         Vby = tempVay
 26         Or: 
 27         Vax = Vax + Vbx
 28         Vbx = Vax - Vbx
 29         Vax = Vax - Vbx
 30         Vay = Vay + Vby
 31         Vby = Vay - Vby
 32         Vay = Vay - Vby
 33     }
 34 
 35     The rules of the game: 
 36     Five small ball movement on the canvas, the collision between them, and of course the ball and up and down all around collision will produce
 37     After the collision, the ball will return to change direction
 38     The following is used to move cursor speed adjusting ball, the cursor is in the range of[-100, 100]
 39     
 40     Defect or BUG: 
 41     1 changes in the cursor data so as to change the ball moving speed, moving the ball distance is not timely update
 42     Cause the ball may escape from the canvas
 43     2 balls in athletic process, sometimes may also escape the canvas
 44 
 45     Summary: 
 46     Finish this game, spent a week's time. In this process, not only to learn the knowledge of mathematics in high school, 
 47     Knowledge of physics, a lot of things are forgotten, but soon learn to. 
 48     The game actually is a lot of mathematical problems. 
 49     
 50     Defects or BUG also exist in the game, hope that like-minded can work together to improve. 
 51 '''
 52 
 53 __author__ = {'author' : 'Hongten',
 54               'Email' : '',
 55               'Blog' : 'http://www.cnblogs.com/hongten/',
 56               'Created' : '2013-09-28',
 57               'Version' : '1.0'}
 58 
 59 class Pong(Frame):
 60     def createWidgets(self):
 61         ## Canvas
 62         self.draw = Canvas(self, width="5i", height="5i", bg='white')
 63 
 64         ## The cursor (control the ball moving speed, range: [-100, 100])
 65         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
 66                            from_=-100, to=100)
 67 
 68         self.speed.pack(side=BOTTOM, fill=X)
 69 
 70         #The scope of small ball collision walls
 71         self.scaling_right = 4.8
 72         self.scaling_left = 0.2
 73         #The ball diameter
 74         self.ball_d = 0.4
 75         #The cursor.
 76         self.scale_value = self.speed.get()
 77         #Discharge rate
 78         self.scaling = 100.0
 79 
 80         #Storage ball array
 81         self.balls = []
 82         #Storage of X ball array
 83         self.ball_x = []
 84         #Storage of Y ball array
 85         self.ball_y = []
 86         #Storage of X axis speed ball array
 87         self.ball_v_x = []
 88         #Storage of Y axis speed ball array
 89         self.ball_v_y = []
 90 
 91         # Five balls
 92         self.ball = self.draw.create_oval("0.10i", "0.10i", "0.50i", "0.50i",
 93                                           fill="red")
 94         self.second_ball = self.draw.create_oval("0.70i", "0.70i", "1.10i", "1.10i",
 95                                                  fill='black')
 96         self.three_ball = self.draw.create_oval("1.30i", "1.30i", "1.70i", "1.70i",
 97                                                  fill='brown')
 98         self.four_ball = self.draw.create_oval("2.0i", "2.0i", "2.40i", "2.40i",
 99                                                  fill='green')
100         self.five_ball = self.draw.create_oval("3.0i", "3.0i", "3.40i", "3.40i",
101                                                  fill='gray')
102 
103         #The five ball into an array
104         self.balls.append(self.ball)
105         self.balls.append(self.second_ball)
106         self.balls.append(self.three_ball)
107         self.balls.append(self.four_ball)
108         self.balls.append(self.five_ball)
109 
110         #The first ball, namely self.ball coordinates (self.x, self.y), here are shrinking, the purpose is to
111         #In the process of moving the ball more fluent in
112         self.x = 0.3        
113         self.y = 0.3
114         #The first ball velocity direction
115         self.velocity_x = -0.2
116         self.velocity_y = 0.5
117 
118         self.second_ball_x = 0.9
119         self.second_ball_y = 0.9
120         self.second_ball_v_x = 0.4
121         self.second_ball_v_y = -0.5
122 
123         self.three_ball_x = 1.5
124         self.three_ball_y = 1.5
125         self.three_ball_v_x = -0.3
126         self.three_ball_v_y = -0.5
127 
128         self.four_ball_x = 2.2
129         self.four_ball_y = 2.2
130         self.four_ball_v_x = 0.1
131         self.four_ball_v_y = -0.5
132 
133         self.five_ball_x = 3.2
134         self.five_ball_y = 3.2
135         self.five_ball_v_x = 0.3
136         self.five_ball_v_y = 0.5
137 
138         
139         #Coordinate updates the ball
140         self.update_ball_x_y()
141         self.draw.pack(side=LEFT)
142 
143     def update_ball_x_y(self, *args):
144         '''Coordinate to update the ball, that each ball center coordinates information and velocity information is stored in an array, 
145            Easy to use in the back of cyclic traversal. '''
146         #The first ball information
147         self.ball_x.append(self.x)
148         self.ball_y.append(self.y)
149         self.ball_v_x.append(self.velocity_x)
150         self.ball_v_y.append(self.velocity_y)
151 
152         self.ball_x.append(self.second_ball_x)
153         self.ball_y.append(self.second_ball_y)
154         self.ball_v_x.append(self.second_ball_v_x)
155         self.ball_v_y.append(self.second_ball_v_y)
156 
157         self.ball_x.append(self.three_ball_x)
158         self.ball_y.append(self.three_ball_y)
159         self.ball_v_x.append(self.three_ball_v_x)
160         self.ball_v_y.append(self.three_ball_v_y)
161 
162         self.ball_x.append(self.four_ball_x)
163         self.ball_y.append(self.four_ball_y)
164         self.ball_v_x.append(self.four_ball_v_x)
165         self.ball_v_y.append(self.four_ball_v_y)
166 
167         self.ball_x.append(self.five_ball_x)
168         self.ball_y.append(self.five_ball_y)
169         self.ball_v_x.append(self.five_ball_v_x)
170         self.ball_v_y.append(self.five_ball_v_y)
171     
172     def update_ball_velocity(self, index, *args):
173         '''Update each ball speed information, namely the collision ball around and another ball for updating speed information'''
174         #Cursor
175         self.scale_value = self.speed.get()
176         #Collision walls
177         if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
178             self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
179         if (self.ball_y[index] > self.scaling_right) or (self.ball_y[index] < self.scaling_left):
180             self.ball_v_y[index] = -1.0 *  self.ball_v_y[index]
181 
182         '''
183         #TEST:
184         for n in range(len(self.balls)):
185             #print((self.ball_x[index] - self.ball_x[n])**2)
186             #print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2))
187             print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2))
188         '''
189         for n in range(len(self.balls)):
190             #Small ball collision conditions, i.e.: (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
191             if (round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2)):
192                 #Two ball speed exchange
193                 temp_vx = self.ball_v_x[index]
194                 temp_vy = self.ball_v_y[index]
195                 self.ball_v_x[index] = self.ball_v_x[n]
196                 self.ball_v_y[index] = self.ball_v_y[n]
197                 self.ball_v_x[n] = temp_vx
198                 self.ball_v_y[n] = temp_vy
199         #print(self.ball_v_x, self.ball_v_y)
200                
201         '''
202         #WRONG:
203         for n in range(len(self.balls)):            
204             if (((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2) <= self.ball_d**2):
205                 #Two ball speed exchange
206                 self.ball_v_x[index] = self.ball_v_x[index] + self.ball_v_x[n]
207                 self.ball_v_x[n] = self.ball_v_x[0] - self.ball_v_x[n]
208                 self.ball_v_x[index] = self.ball_v_x[index] - self.ball_v_x[n]
209                 self.ball_v_y[index] = self.ball_v_y[index] + self.ball_v_y[n]
210                 self.ball_v_y[n] = self.ball_v_y[index] - self.ball_v_y[n]
211                 self.ball_v_y[index] = self.ball_v_y[index] - self.ball_v_y[n]
212         print(self.ball_v_x, self.ball_v_y)
213         '''
214         
215     def get_ball_deltax(self, index, *args):
216         '''The ball X axis moving distance and update the ball center coordinates X, return to the X axis required to move the distance'''
217         deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
218         self.ball_x[index] = self.ball_x[index] + deltax
219         return deltax
220 
221     def get_ball_deltay(self, index, *args):
222         '''The ball Y axis moving distance and update the ball center coordinates Y, return to the Y axis required to move the distance'''
223         deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
224         self.ball_y[index] = self.ball_y[index] + deltay
225         return deltay
226     
227     def moveBall(self, *args):
228         '''Move the first ball, number: 0, which is based on the array: self.balls determination. '''
229         self.update_ball_velocity(0)       
230         deltax = self.get_ball_deltax(0)
231         deltay = self.get_ball_deltay(0)
232         #The ball movement
233         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
234         self.after(10, self.moveBall)
235 
236     def move_second_ball(self, *args):
237         self.update_ball_velocity(1)       
238         deltax = self.get_ball_deltax(1)
239         deltay = self.get_ball_deltay(1)        
240         self.draw.move(self.second_ball,  "%ri" % deltax, "%ri" % deltay)
241         self.after(10, self.move_second_ball)
242 
243 
244     def move_three_ball(self, *args):
245         self.update_ball_velocity(2)       
246         deltax = self.get_ball_deltax(2)
247         deltay = self.get_ball_deltay(2)
248         self.draw.move(self.three_ball,  "%ri" % deltax, "%ri" % deltay)
249         self.after(10, self.move_three_ball)
250 
251     def move_four_ball(self, *args):
252         self.update_ball_velocity(3)       
253         deltax = self.get_ball_deltax(3)
254         deltay = self.get_ball_deltay(3)
255         self.draw.move(self.four_ball,  "%ri" % deltax, "%ri" % deltay)
256         self.after(10, self.move_four_ball)
257 
258     def move_five_ball(self, *args):
259         self.update_ball_velocity(4)       
260         deltax = self.get_ball_deltax(4)
261         deltay = self.get_ball_deltay(4)
262         self.draw.move(self.five_ball,  "%ri" % deltax, "%ri" % deltay)
263         self.after(10, self.move_five_ball)
264 
265             
266     def __init__(self, master=None):
267         '''The initialization function'''
268         Frame.__init__(self, master)
269         Pack.config(self)
270         self.createWidgets()
271         self.after(10, self.moveBall)
272         self.after(10, self.move_three_ball)
273         self.after(10, self.move_four_ball)
274         self.after(10, self.move_five_ball)
275         self.after(10, self.move_second_ball)
276         
277         
278 game = Pong()
279 
280 game.mainloop()

Download the source code: http://files.cnblogs.com/hongten/python_tkinter_pong.1.0.zip

===================================================

The second version version.1.1:  Edit by Hongten 2013-09-28 17:40

===================================================

Revision record:

1 adjust the canvas size

2 adjust the radius of the ball, the ball speed and initial value, small initial center coordinates

Range vernier was revised to 3: [-200, 200]

These changes are mainly for the above shortcomings of.

Advantage:

1 small ball movement process more intuitive

Movement speed by 2 ball is smaller, but the ball moving speed changes according to the cursor

3 interface more friendly than before

Effect of operation:

===================================================

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
  7     Judge
  8     Two balls
  9     {
 10         Center: A (x1, Y1) radius: R X speed: Vax Y shaft speed: Vay
 11         Center: B (X2, Y2) radius: R X speed: Vbx Y shaft speed: Vby
 12     }
 13     Collision condition: 
 14     1 the two ball of center distance of not more than two ball radius and (r+R), i.e.: 
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
 18     The collision of 2 small ball, the number of exchange of two balls, i.e.: 
 19     {
 20         tempVax = Vax
 21         tempVay = Vay
 22         Vax = Vbx
 23         Vay = Vby
 24         Vbx = tempVax
 25         Vby = tempVay
 26         Or: 
 27         Vax = Vax + Vbx
 28         Vbx = Vax - Vbx
 29         Vax = Vax - Vbx
 30         Vay = Vay + Vby
 31         Vby = Vay - Vby
 32         Vay = Vay - Vby
 33     }
 34 
 35     The rules of the game: 
 36     Five small ball movement on the canvas, the collision between them, and of course the ball and up and down all around collision will produce
 37     After the collision, the ball will return to change direction
 38     The following is used to move cursor speed adjusting ball, the cursor is in the range of[-100, 100]
 39     
 40     Defect or BUG: 
 41     1 changes in the cursor data so as to change the ball moving speed, moving the ball distance is not timely update
 42     Cause the ball may escape from the canvas
 43     2 balls in athletic process, sometimes may also escape the canvas
 44 
 45     Summary: 
 46     Finish this game, spent a week's time. In this process, not only to learn the knowledge of mathematics in high school, 
 47     Knowledge of physics, a lot of things are forgotten, but soon learn to. 
 48     The game actually is a lot of mathematical problems. 
 49     
 50     Defects or BUG also exist in the game, hope that like-minded can work together to improve. 
 51 
 52     Revision record: 
 53     1 adjust the canvas size
 54     2 adjust the radius of the ball, the ball speed and initial value, small initial center coordinates
 55     Range vernier was revised to 3: [-200, 200]
 56     These changes are mainly for the above shortcomings of. 
 57 
 58     Advantage: 
 59     1 small ball movement process more intuitive
 60     Movement speed by 2 ball is smaller, but the ball moving speed changes according to the cursor
 61     3 interface more friendly than before
 62 '''
 63 
 64 __author__ = {'author' : 'Hongten',
 65               'Email' : '',
 66               'Blog' : 'http://www.cnblogs.com/hongten/',
 67               'Created' : '2013-09-28',
 68               'Version' : '1.1'}
 69 
 70 class Pong(Frame):
 71     def createWidgets(self):
 72          #Discharge rate
 73         self.scaling = 100.0
 74         #The proportion of the canvas
 75         self.canvas_width = 10
 76         self.canvas_height = 5.6
 77         ## Canvas
 78         self.draw = Canvas(self, width=(self.canvas_width * self.scaling),
 79                            height=(self.canvas_height * self.scaling),
 80                            bg='white')
 81 
 82         ## The cursor (control the ball moving speed, range: [-100, 100])
 83         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
 84                            from_=-200, to=200)
 85         
 86         self.speed.pack(side=BOTTOM, fill=X)
 87 
 88         #The ball diameter
 89         self.ball_d = 1.0
 90         #The scope of small ball collision walls
 91         self.scaling_left = round(self.ball_d / 2, 1)
 92         self.scaling_right = self.canvas_width - self.scaling_left
 93         self.scaling_bottom = self.canvas_height - self.scaling_left
 94         self.scaling_top = self.scaling_left
 95        
 96         #The cursor.
 97         self.scale_value = self.speed.get() * 0.1
 98        
 99         #Storage ball array
100         self.balls = []
101         #Storage of X ball array
102         self.ball_x = []
103         #Storage of Y ball array
104         self.ball_y = []
105         #Storage of X axis speed ball array
106         self.ball_v_x = []
107         #Storage of Y axis speed ball array
108         self.ball_v_y = []
109 
110         # Five balls
111         self.ball = self.draw.create_oval("0.60i", "0.60i", "1.60i", "1.60i",
112                                           fill="red")
113         self.second_ball = self.draw.create_oval("2.0i", "2.0i", "3.0i", "3.0i",
114                                                  fill='black')
115         self.three_ball = self.draw.create_oval("4.0i", "4.0i", "5.0i", "5.0i",
116                                                  fill='brown')
117         self.four_ball = self.draw.create_oval("6.0i", "2.0i", "7.0i", "3.0i",
118                                                  fill='green')
119         self.five_ball = self.draw.create_oval("8.0i", "3.0i", "9.0i", "4.0i",
120                                                  fill='gray')
121 
122         #The five ball into an array
123         self.balls.append(self.ball)
124         self.balls.append(self.second_ball)
125         self.balls.append(self.three_ball)
126         self.balls.append(self.four_ball)
127         self.balls.append(self.five_ball)
128 
129         #The first ball, namely self.ball coordinates (self.x, self.y), here are shrinking, the purpose is to
130         #In the process of moving the ball more fluent in
131         self.x = 1.1        
132         self.y = 1.1
133         #The first ball velocity direction
134         self.velocity_x = -0.2
135         self.velocity_y = 0.1
136 
137         self.second_ball_x = 2.5
138         self.second_ball_y = 2.5
139         self.second_ball_v_x = 0.1
140         self.second_ball_v_y = -0.2
141 
142         self.three_ball_x = 4.5
143         self.three_ball_y = 4.5
144         self.three_ball_v_x = -0.1
145         self.three_ball_v_y = -0.2
146 
147         self.four_ball_x = 6.5
148         self.four_ball_y = 2.5
149         self.four_ball_v_x = 0.1
150         self.four_ball_v_y = -0.2
151 
152         self.five_ball_x = 8.5
153         self.five_ball_y = 3.5
154         self.five_ball_v_x = 0.1
155         self.five_ball_v_y = 0.2
156 
157         
158         #Coordinate updates the ball
159         self.update_ball_x_y()
160         self.draw.pack(side=LEFT)
161 
162     def update_ball_x_y(self, *args):
163         '''Coordinate to update the ball, that each ball center coordinates information and velocity information is stored in an array, 
164            Easy to use in the back of cyclic traversal. '''
165         #The first ball information
166         self.ball_x.append(self.x)
167         self.ball_y.append(self.y)
168         self.ball_v_x.append(self.velocity_x)
169         self.ball_v_y.append(self.velocity_y)
170 
171         self.ball_x.append(self.second_ball_x)
172         self.ball_y.append(self.second_ball_y)
173         self.ball_v_x.append(self.second_ball_v_x)
174         self.ball_v_y.append(self.second_ball_v_y)
175 
176         self.ball_x.append(self.three_ball_x)
177         self.ball_y.append(self.three_ball_y)
178         self.ball_v_x.append(self.three_ball_v_x)
179         self.ball_v_y.append(self.three_ball_v_y)
180 
181         self.ball_x.append(self.four_ball_x)
182         self.ball_y.append(self.four_ball_y)
183         self.ball_v_x.append(self.four_ball_v_x)
184         self.ball_v_y.append(self.four_ball_v_y)
185 
186         self.ball_x.append(self.five_ball_x)
187         self.ball_y.append(self.five_ball_y)
188         self.ball_v_x.append(self.five_ball_v_x)
189         self.ball_v_y.append(self.five_ball_v_y)
190     
191     def update_ball_velocity(self, index, *args):
192         '''Update each ball speed information, namely the collision ball around and another ball for updating speed information'''
193         #Cursor
194         self.scale_value = self.speed.get() * 0.1
195         #Collision walls
196         if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
197             self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
198         if (self.ball_y[index] > self.scaling_bottom) or (self.ball_y[index] < self.scaling_top):
199             self.ball_v_y[index] = -1.0 *  self.ball_v_y[index]
200 
201         '''
202         #TEST:
203         for n in range(len(self.balls)):
204             #print((self.ball_x[index] - self.ball_x[n])**2)
205             #print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2))
206             print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2))
207         '''
208         for n in range(len(self.balls)):
209             #Small ball collision conditions, i.e.: (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
210             if (round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2)):
211                 #Two ball speed exchange
212                 temp_vx = self.ball_v_x[index]
213                 temp_vy = self.ball_v_y[index]
214                 self.ball_v_x[index] = self.ball_v_x[n]
215                 self.ball_v_y[index] = self.ball_v_y[n]
216                 self.ball_v_x[n] = temp_vx
217                 self.ball_v_y[n] = temp_vy
218         #print(self.ball_v_x, self.ball_v_y)
219                
220         '''
221         #WRONG:
222         for n in range(len(self.balls)):            
223             if (((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2) <= self.ball_d**2):
224                 #Two ball speed exchange
225                 self.ball_v_x[index] = self.ball_v_x[index] + self.ball_v_x[n]
226                 self.ball_v_x[n] = self.ball_v_x[0] - self.ball_v_x[n]
227                 self.ball_v_x[index] = self.ball_v_x[index] - self.ball_v_x[n]
228                 self.ball_v_y[index] = self.ball_v_y[index] + self.ball_v_y[n]
229                 self.ball_v_y[n] = self.ball_v_y[index] - self.ball_v_y[n]
230                 self.ball_v_y[index] = self.ball_v_y[index] - self.ball_v_y[n]
231         print(self.ball_v_x, self.ball_v_y)
232         '''
233         
234     def get_ball_deltax(self, index, *args):
235         '''The ball X axis moving distance and update the ball center coordinates X, return to the X axis required to move the distance'''
236         deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
237         self.ball_x[index] = self.ball_x[index] + deltax
238         return deltax
239 
240     def get_ball_deltay(self, index, *args):
241         '''The ball Y axis moving distance and update the ball center coordinates Y, return to the Y axis required to move the distance'''
242         deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
243         self.ball_y[index] = self.ball_y[index] + deltay
244         return deltay
245     
246     def moveBall(self, *args):
247         '''Move the first ball, number: 0, which is based on the array: self.balls determination. '''
248         self.update_ball_velocity(0)       
249         deltax = self.get_ball_deltax(0)
250         deltay = self.get_ball_deltay(0)
251         #The ball movement
252         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
253         self.after(10, self.moveBall)
254 
255     def move_second_ball(self, *args):
256         self.update_ball_velocity(1)       
257         deltax = self.get_ball_deltax(1)
258         deltay = self.get_ball_deltay(1)        
259         self.draw.move(self.second_ball,  "%ri" % deltax, "%ri" % deltay)
260         self.after(10, self.move_second_ball)
261 
262 
263     def move_three_ball(self, *args):
264         self.update_ball_velocity(2)       
265         deltax = self.get_ball_deltax(2)
266         deltay = self.get_ball_deltay(2)
267         self.draw.move(self.three_ball,  "%ri" % deltax, "%ri" % deltay)
268         self.after(10, self.move_three_ball)
269 
270     def move_four_ball(self, *args):
271         self.update_ball_velocity(3)       
272         deltax = self.get_ball_deltax(3)
273         deltay = self.get_ball_deltay(3)
274         self.draw.move(self.four_ball,  "%ri" % deltax, "%ri" % deltay)
275         self.after(10, self.move_four_ball)
276 
277     def move_five_ball(self, *args):
278         self.update_ball_velocity(4)       
279         deltax = self.get_ball_deltax(4)
280         deltay = self.get_ball_deltay(4)
281         self.draw.move(self.five_ball,  "%ri" % deltax, "%ri" % deltay)
282         self.after(10, self.move_five_ball)
283 
284             
285     def __init__(self, master=None):
286         '''The initialization function'''
287         Frame.__init__(self, master)
288         Pack.config(self)
289         self.createWidgets()
290         self.after(10, self.moveBall)
291         self.after(10, self.move_three_ball)
292         self.after(10, self.move_four_ball)
293         self.after(10, self.move_five_ball)
294         self.after(10, self.move_second_ball)
295         
296         
297 game = Pong()
298 
299 game.mainloop()

Download the source code: http://files.cnblogs.com/hongten/python_tkinter_pong.1.1.zip

Posted by Marico at November 18, 2013 - 10:21 PM