cool hit counter python development_tkinter_ball_completely_elastic_collision_game_source_code_download_Intefrankly

python development_tkinter_ball_completely_elastic_collision_game_source_code_download


Complete this blob of fully elastic collision game inspired by.

Here's a fully elastic collision game with small balls that I spent a week off work writing.

Game initialization status.

The bottom cursor and modifying the speed of the ball's movement

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

Source code section.

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

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
   7 Judgement
   8 Two small balls
  9     {
  10 Center of circle: A(x1,y1) Radius: r X-axis velocity: Vax Y-axis velocity: Vay
  11 Center of circle: B(x2,y2) Radius: R X-axis velocity: Vbx Y-axis velocity: Vby
 12     }
  13 The conditions for a collision are.
  14 1. The distance between the centers of the two spheres is not greater than the sum of the radii of the two spheres (r + R), i.e.
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
  18 2. After the collision of the spheres, the number of degrees of the two spheres are exchanged, 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 Rules of the Game.
  36 Five little balls move through the canvas and they collide with each other, and of course the little balls collide with the top, bottom, left and right
  37 After the collision, the ball will change direction and return
  38 And the bottom cursor is used to adjust the speed of the ball movement, the range of the cursor is [-100, 100]
 39     
  40 Defects or bugs.
  41 1. When modifying the cursor data and thus changing the speed of the ball movement, the distance moved by the ball is not updated in time
  42 causes the blob to potentially escape the canvas
  43 2. It is sometimes possible for a small ball to escape the canvas as it moves
 44 
  45 Summary.
  46 It took a week off work to complete this game. Not only did you go back and learn high school math in the process, the
  47 knowledge of physics, much of which was pretty much forgotten, but quickly learned to return to.
  48 A lot of the game is actually about math.
 49     
  50 There are still flaws or bugs in the game, and I hope that like-minded people can work together to improve it.
 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 ## Canvas
 62         self.draw = Canvas(self, width="5i", height="5i", bg='white')
 63 
  64 ## Cursor (controls ball movement 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 # Range of small balls colliding with walls
 71         self.scaling_right = 4.8
 72         self.scaling_left = 0.2
  73 # small ball diameter
 74         self.ball_d = 0.4
  75 # cursor degrees
 76         self.scale_value = self.speed.get()
  77 # Deflation rate
 78         self.scaling = 100.0
 79 
  80 # Holds an array of small balls
 81         self.balls = []
  82 # array of x-coordinates of the sphere
 83         self.ball_x = []
  84 # hold the array of y-coordinates of the sphere
 85         self.ball_y = []
  86 # Store the array of velocities in the x-axis of the ball
 87         self.ball_v_x = []
  88 # hold the array of velocities in the y-axis direction of the ball
 89         self.ball_v_y = []
 90 
  91 # Five little 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 # Put five balls into the 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 coordinates of the center of the circle of the first blob, self.ball (self.x, self.y), are deflated here, in order to
 111 # More fluid in the movement of the ball
112         self.x = 0.3        
113         self.y = 0.3
 114 # Direction of velocity of the first blob
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 # Update the coordinates of the blob
140         self.update_ball_x_y()
141         self.draw.pack(side=LEFT)
142 
143     def update_ball_x_y(self, *args):
144         '''Update the coordinates of the sphere, That is, the information about the coordinates of the center of each ball and the velocity information are stored in an array,
145             Easy to use later in the loop traversal。'''
 146 # First blob message
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 individual ball speed information, That is, the ball collides into the surrounding and another ball claiming updated velocity information'''
 174 # Cursor values
175         self.scale_value = self.speed.get()
 176 # Crash against the wall
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 small ball velocity 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 small 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         ''' Get the ballX The axis coordinates are shifted by a distance and the center of the circle of the sphere is updatedX coordinates, return toX Distance required to move the axis'''
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         ''' Get the ballY The axis coordinates are shifted by a distance and the center of the circle of the sphere is updatedY coordinates, return toY Distance required to move the axis'''
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, numbered:0, This is based on the array:self.balls sure。'''
229         self.update_ball_velocity(0)       
230         deltax = self.get_ball_deltax(0)
231         deltay = self.get_ball_deltay(0)
 232 #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         ''' 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()

Source Code Download.http://files.cnblogs.com/hongten/python_tkinter_pong.1.0.zip

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

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

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

Modified record. 1.Resizing the canvas 2.The radius of the ball, the initial value of the ball's velocity, and the coordinates of the initial center of the ball are adjusted. 3.The range of the cursor is modified to [-200, 200] These modifications were made primarily in response to the flaws above.

Pros. 1.The movement of the ball is more visual 2.The ball movement speed becomes smaller, but the ball movement speed can be modified according to the cursor 3.The interface is more user-friendly than before

Running effect.

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

  1 #python tkinter
  2 #python version 3.3.2
  3 
  4 from tkinter import *
  5 
  6 '''
   7 Judgement
   8 Two small balls
  9     {
  10 Center of circle: A(x1,y1) Radius: r X-axis velocity: Vax Y-axis velocity: Vay
  11 Center of circle: B(x2,y2) Radius: R X-axis velocity: Vbx Y-axis velocity: Vby
 12     }
  13 The conditions for a collision are.
  14 1. The distance between the centers of the two spheres is not greater than the sum of the radii of the two spheres (r + R), i.e.
 15     {
 16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
 17     }
  18 2. After the collision of the spheres, the number of degrees of the two spheres are exchanged, 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 Rules of the Game.
  36 Five little balls move through the canvas and they collide with each other, and of course the little balls collide with the top, bottom, left and right
  37 After the collision, the ball will change direction and return
  38 And the bottom cursor is used to adjust the speed of the ball movement, the range of the cursor is [-100, 100]
 39     
  40 Defects or bugs.
  41 1. When modifying the cursor data and thus changing the speed of the ball movement, the distance moved by the ball is not updated in time
  42 causes the blob to potentially escape the canvas
  43 2. It is sometimes possible for a small ball to escape the canvas as it moves
 44 
  45 Summary.
  46 It took a week off work to complete this game. Not only did you go back and learn high school math in the process, the
  47 knowledge of physics, much of which was pretty much forgotten, but quickly learned to return to.
  48 A lot of the game is actually about math.
 49     
  50 There are still flaws or bugs in the game, and I hope that like-minded people can work together to improve it.
 51 
  52 Modification of records.
  53 1. Resize the canvas
  54 2. Adjusted the radius of the ball, and the initial value of the ball's velocity, and the coordinates of the initial center of the ball
  55 3. The range of the cursor is modified to [-200, 200]
56 These changes were made primarily in response to the above deficiencies.
 57 
  58 Pros.
  59 1. The movement of the ball is more visual
  60 2. The speed of the ball movement becomes smaller, but the ball movement speed can be modified according to the cursor
  61 3. The interface is more user-friendly than before
 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 # Deflation rate
 73         self.scaling = 100.0
  74 #Canvas scale
 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 ## Cursor (controls ball movement 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 # small ball diameter
 89         self.ball_d = 1.0
  90 # The range at which the ball collides with the wall
 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 # cursor degrees
 97         self.scale_value = self.speed.get() * 0.1
 98        
  99 # Holds an array of small balls
100         self.balls = []
 101 # hold the array of the x-coordinates of the sphere
102         self.ball_x = []
 103 # hold the array of y-coordinates of the sphere
104         self.ball_y = []
 105 # Store the array of velocities in the x-axis direction of the ball
106         self.ball_v_x = []
 107 # hold the array of velocities in the y-axis direction of the ball
108         self.ball_v_y = []
109 
 110 # Five small 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 # Put five balls into the 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 coordinates of the circle center of the first blob, self.ball (self.x, self.y), are deflated here, for the purpose of
 130 # More fluid in the movement of the ball
131         self.x = 1.1        
132         self.y = 1.1
 133 # Direction of velocity of the first blob
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 # Update the coordinates of the blob
159         self.update_ball_x_y()
160         self.draw.pack(side=LEFT)
161 
162     def update_ball_x_y(self, *args):
163         '''Update the coordinates of the sphere, That is, the information about the coordinates of the center of each ball and the velocity information are stored in an array,
164             Easy to use later in the loop traversal。'''
 165 # First blob message
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 individual ball speed information, That is, the ball collides into the surrounding and another ball claiming updated velocity information'''
 193 # Cursor values
194         self.scale_value = self.speed.get() * 0.1
 195 # Crash against the wall
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 small ball velocity 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 small 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         ''' Get the ballX The axis coordinates are shifted by a distance and the center of the circle of the sphere is updatedX coordinates, return toX Distance required to move the axis'''
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         ''' Get the ballY The axis coordinates are shifted by a distance and the center of the circle of the sphere is updatedY coordinates, return toY Distance required to move the axis'''
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, numbered:0, This is based on the array:self.balls sure。'''
248         self.update_ball_velocity(0)       
249         deltax = self.get_ball_deltax(0)
250         deltay = self.get_ball_deltay(0)
 251 #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         ''' 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()

Source Code Download.http://files.cnblogs.com/hongten/python_tkinter_pong.1.1.zip

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


Recommended>>
1、Access file mdb accdb interconversion format
2、20 Changes in Smartphones in 2017 Trends for 2018
3、Yangzhou household average Internet speed to 71M broadband users reached 1 million 100 megabit users more than 40
4、Using Java8 in Android projects
5、Nanyang government affairs cloud platform opened 1790 peoples life matters cloud to do

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号