python开发_tkinter_小球完全弹性碰撞游戏_源码下载

完成这个小球的完全弹性碰撞游戏灵感来自于:

下面是我花了一周下班时间所编写的一个小球完全弹性碰撞游戏:

游戏初始化状态:

最下面的游标和修改小球的移动速度

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

源码部分:

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

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
  7     判断
  8     两个小球
  9     {
 10         圆心:A(x1,y1)  半径:r  X轴速度:Vax  Y轴速度:Vay
 11         圆心:B(x2,y2)  半径:R  X轴速度:Vbx  Y轴速度:Vby
 12     }
 13     碰撞的条件是:
 14     1.两个小球的圆心距离不大于两小球半径之和(r+R),即:
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
 18     2.小球碰撞后,两小球的数度交换,即:
 19     {
 20         tempVax = Vax
 21         tempVay = Vay
 22         Vax = Vbx
 23         Vay = Vby
 24         Vbx = tempVax
 25         Vby = tempVay
 26         或:
 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     游戏规则:
 36     五小球在画布中移动,他们之间会产生碰撞,当然小球和上下左右都会产生碰撞
 37     碰撞后,小球会改变方向返回
 38     而最下面的游标则用于调节小球的移动速度,游标的范围是[-100, 100]
 39     
 40     缺陷或BUG:
 41     1.在修改游标数据从而改变小球移动速度的时候,小球移动的距离得不到及时的更新
 42     导致小球可能会逃离画布
 43     2.小球在运动的过程中,有时候也有可能逃离画布
 44 
 45     总结:
 46     完成这个游戏,花了一个星期的下班时间。在这个过程中不仅回去学习了高中的数学知识,
 47     物理知识,很多东西都忘得差不多了,不过很快又学返回来了。
 48     游戏其实很多就是数学问题。
 49     
 50     游戏中还存在缺陷或BUG,希望志同道合者可以共同完善。
 51 '''
 52 
 53 __author__ = {'author' : 'Hongten',
 54               'Email' : 'hongtenzone@foxmail.com',
 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         ## 画布
 62         self.draw = Canvas(self, width="5i", height="5i", bg='white')
 63 
 64         ## 游标(控制小球移动速度,范围:[-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         #小球碰撞墙壁的范围
 71         self.scaling_right = 4.8
 72         self.scaling_left = 0.2
 73         #小球直径
 74         self.ball_d = 0.4
 75         #游标度数
 76         self.scale_value = self.speed.get()
 77         #放缩率
 78         self.scaling = 100.0
 79 
 80         #存放小球数组
 81         self.balls = []
 82         #存放小球x坐标数组
 83         self.ball_x = []
 84         #存放小球y坐标数组
 85         self.ball_y = []
 86         #存放小球x轴方向速度数组
 87         self.ball_v_x = []
 88         #存放小球y轴方向速度数组
 89         self.ball_v_y = []
 90 
 91         # 五个小球
 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         #把五个小球放入数组
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         #第一个小球,即self.ball的圆心坐标(self.x, self.y),这里进行了放缩,目的是为了
111         #在小球移动的过程中更加流畅
112         self.x = 0.3        
113         self.y = 0.3
114         #第一个小球的速度方向
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         #更新小球的坐标
140         self.update_ball_x_y()
141         self.draw.pack(side=LEFT)
142 
143     def update_ball_x_y(self, *args):
144         '''更新小球的坐标,即把各个小球的圆心坐标信息以及速度信息存放到数组中,
145            便于在后面循环遍历的时候使用。'''
146         #第一个小球信息
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         '''更新各个小球速度信息,即小球碰撞到四周和另外的小球索要更新的速度信息'''
174         #游标值
175         self.scale_value = self.speed.get()
176         #碰撞墙壁
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             #小球碰撞条件,即:(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                 #两小球速度交换
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                 #两小球速度交换
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         '''获取小球X轴坐标移动距离并且更新小球的圆心X坐标,返回X轴所需移动距离'''
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         '''获取小球Y轴坐标移动距离并且更新小球的圆心Y坐标,返回Y轴所需移动距离'''
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         '''移动第一个小球,编号为:0,这是根据数组:self.balls确定的。'''
229         self.update_ball_velocity(0)       
230         deltax = self.get_ball_deltax(0)
231         deltay = self.get_ball_deltay(0)
232         #小球移动
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         '''初始化函数'''
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()

源码下载:https://files.cnblogs.com/hongten/python_tkinter_pong.1.0.zip

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

第二个版本 version.1.1:  Edit by Hongten 2013-09-28 17:40

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

修改记录:
1.调整画布大小
2.调整了小球的半径,以及小球的速度初始值,小球初始圆心坐标
3.游标的范围修改为:[-200, 200]
这些修改主要是针对上面的缺陷而进行的。

优点:
1.小球移动的过程更直观
2.小球的移动速度变小,但是可以根据游标来修改小球移动速度
3.界面比之前更加友好

运行效果:

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

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
  7     判断
  8     两个小球
  9     {
 10         圆心:A(x1,y1)  半径:r  X轴速度:Vax  Y轴速度:Vay
 11         圆心:B(x2,y2)  半径:R  X轴速度:Vbx  Y轴速度:Vby
 12     }
 13     碰撞的条件是:
 14     1.两个小球的圆心距离不大于两小球半径之和(r+R),即:
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
 18     2.小球碰撞后,两小球的数度交换,即:
 19     {
 20         tempVax = Vax
 21         tempVay = Vay
 22         Vax = Vbx
 23         Vay = Vby
 24         Vbx = tempVax
 25         Vby = tempVay
 26         或:
 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     游戏规则:
 36     五小球在画布中移动,他们之间会产生碰撞,当然小球和上下左右都会产生碰撞
 37     碰撞后,小球会改变方向返回
 38     而最下面的游标则用于调节小球的移动速度,游标的范围是[-100, 100]
 39     
 40     缺陷或BUG:
 41     1.在修改游标数据从而改变小球移动速度的时候,小球移动的距离得不到及时的更新
 42     导致小球可能会逃离画布
 43     2.小球在运动的过程中,有时候也有可能逃离画布
 44 
 45     总结:
 46     完成这个游戏,花了一个星期的下班时间。在这个过程中不仅回去学习了高中的数学知识,
 47     物理知识,很多东西都忘得差不多了,不过很快又学返回来了。
 48     游戏其实很多就是数学问题。
 49     
 50     游戏中还存在缺陷或BUG,希望志同道合者可以共同完善。
 51 
 52     修改记录:
 53     1.调整画布大小
 54     2.调整了小球的半径,以及小球的速度初始值,小球初始圆心坐标
 55     3.游标的范围修改为:[-200, 200]
 56     这些修改主要是针对上面的缺陷而进行的。
 57 
 58     优点:
 59     1.小球移动的过程更直观
 60     2.小球的移动速度变小,但是可以根据游标来修改小球移动速度
 61     3.界面比之前更加友好
 62 '''
 63 
 64 __author__ = {'author' : 'Hongten',
 65               'Email' : 'hongtenzone@foxmail.com',
 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          #放缩率
 73         self.scaling = 100.0
 74         #画布比例
 75         self.canvas_width = 10
 76         self.canvas_height = 5.6
 77         ## 画布
 78         self.draw = Canvas(self, width=(self.canvas_width * self.scaling),
 79                            height=(self.canvas_height * self.scaling),
 80                            bg='white')
 81 
 82         ## 游标(控制小球移动速度,范围:[-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         #小球直径
 89         self.ball_d = 1.0
 90         #小球碰撞墙壁的范围
 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         #游标度数
 97         self.scale_value = self.speed.get() * 0.1
 98        
 99         #存放小球数组
100         self.balls = []
101         #存放小球x坐标数组
102         self.ball_x = []
103         #存放小球y坐标数组
104         self.ball_y = []
105         #存放小球x轴方向速度数组
106         self.ball_v_x = []
107         #存放小球y轴方向速度数组
108         self.ball_v_y = []
109 
110         # 五个小球
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         #把五个小球放入数组
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         #第一个小球,即self.ball的圆心坐标(self.x, self.y),这里进行了放缩,目的是为了
130         #在小球移动的过程中更加流畅
131         self.x = 1.1        
132         self.y = 1.1
133         #第一个小球的速度方向
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         #更新小球的坐标
159         self.update_ball_x_y()
160         self.draw.pack(side=LEFT)
161 
162     def update_ball_x_y(self, *args):
163         '''更新小球的坐标,即把各个小球的圆心坐标信息以及速度信息存放到数组中,
164            便于在后面循环遍历的时候使用。'''
165         #第一个小球信息
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         '''更新各个小球速度信息,即小球碰撞到四周和另外的小球索要更新的速度信息'''
193         #游标值
194         self.scale_value = self.speed.get() * 0.1
195         #碰撞墙壁
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             #小球碰撞条件,即:(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                 #两小球速度交换
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                 #两小球速度交换
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         '''获取小球X轴坐标移动距离并且更新小球的圆心X坐标,返回X轴所需移动距离'''
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         '''获取小球Y轴坐标移动距离并且更新小球的圆心Y坐标,返回Y轴所需移动距离'''
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         '''移动第一个小球,编号为:0,这是根据数组:self.balls确定的。'''
248         self.update_ball_velocity(0)       
249         deltax = self.get_ball_deltax(0)
250         deltay = self.get_ball_deltay(0)
251         #小球移动
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         '''初始化函数'''
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()

源码下载:https://files.cnblogs.com/hongten/python_tkinter_pong.1.1.zip

 

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

 

More reading,and english is important.

 

I'm Hongten

 

 

 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

 

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

 

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

 

posted @ 2013-09-28 02:20  Hongten  阅读(8413)  评论(0编辑  收藏  举报
Fork me on GitHub