Advertisement
here2share

Tkinter_BallCollisions.py ### ZZZ

Feb 19th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.64 KB | None | 0 0
  1. # Tkinter_BallCollisions.py ### ZZZ
  2.  
  3. #python tkinter
  4. #python version 3.3.2
  5.  
  6. from Tkinter import *
  7.  
  8. __author__ = {'author' : 'Hongten',
  9.               'Email' : 'hongtenzone@foxmail.com',
  10.               'Blog' : 'http://www.cnblogs.com/hongten/',
  11.               'Created' : '2013-09-28',
  12.               'Version' : '1.0'}
  13.  
  14. class Pong(Frame):
  15.     def createWidgets(self):
  16.         self.draw = Canvas(self, width="5i", height="5i", bg='white')
  17.  
  18.         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
  19.                            from_=-100, to=100)
  20.  
  21.         self.speed.pack(side=BOTTOM, fill=X)
  22.  
  23.         self.scaling_right = 4.8
  24.         self.scaling_left = 0.2
  25.         self.ball_d = 0.4
  26.         self.scale_value = self.speed.get()
  27.         self.scaling = 100.0
  28.  
  29.         self.balls = []
  30.         self.ball_x = []
  31.         self.ball_y = []
  32.         self.ball_v_x = []
  33.         self.ball_v_y = []
  34.  
  35.         self.ball = self.draw.create_oval("0.10i", "0.10i", "0.50i", "0.50i",
  36.                                           fill="red")
  37.         self.second_ball = self.draw.create_oval("0.70i", "0.70i", "1.10i", "1.10i",
  38.                                                  fill='black')
  39.         self.three_ball = self.draw.create_oval("1.30i", "1.30i", "1.70i", "1.70i",
  40.                                                  fill='brown')
  41.         self.four_ball = self.draw.create_oval("2.0i", "2.0i", "2.40i", "2.40i",
  42.                                                  fill='green')
  43.         self.five_ball = self.draw.create_oval("3.0i", "3.0i", "3.40i", "3.40i",
  44.                                                  fill='gray')
  45.  
  46.         self.balls.append(self.ball)
  47.         self.balls.append(self.second_ball)
  48.         self.balls.append(self.three_ball)
  49.         self.balls.append(self.four_ball)
  50.         self.balls.append(self.five_ball)
  51.  
  52.         self.x = 0.3        
  53.         self.y = 0.3
  54.         self.velocity_x = -0.2
  55.         self.velocity_y = 0.5
  56.  
  57.         self.second_ball_x = 0.9
  58.         self.second_ball_y = 0.9
  59.         self.second_ball_v_x = 0.4
  60.         self.second_ball_v_y = -0.5
  61.  
  62.         self.three_ball_x = 1.5
  63.         self.three_ball_y = 1.5
  64.         self.three_ball_v_x = -0.3
  65.         self.three_ball_v_y = -0.5
  66.  
  67.         self.four_ball_x = 2.2
  68.         self.four_ball_y = 2.2
  69.         self.four_ball_v_x = 0.1
  70.         self.four_ball_v_y = -0.5
  71.  
  72.         self.five_ball_x = 3.2
  73.         self.five_ball_y = 3.2
  74.         self.five_ball_v_x = 0.3
  75.         self.five_ball_v_y = 0.5
  76.  
  77.         self.update_ball_x_y()
  78.         self.draw.pack(side=LEFT)
  79.  
  80.     def update_ball_x_y(self, *args):
  81.         self.ball_x.append(self.x)
  82.         self.ball_y.append(self.y)
  83.         self.ball_v_x.append(self.velocity_x)
  84.         self.ball_v_y.append(self.velocity_y)
  85.  
  86.         self.ball_x.append(self.second_ball_x)
  87.         self.ball_y.append(self.second_ball_y)
  88.         self.ball_v_x.append(self.second_ball_v_x)
  89.         self.ball_v_y.append(self.second_ball_v_y)
  90.  
  91.         self.ball_x.append(self.three_ball_x)
  92.         self.ball_y.append(self.three_ball_y)
  93.         self.ball_v_x.append(self.three_ball_v_x)
  94.         self.ball_v_y.append(self.three_ball_v_y)
  95.  
  96.         self.ball_x.append(self.four_ball_x)
  97.         self.ball_y.append(self.four_ball_y)
  98.         self.ball_v_x.append(self.four_ball_v_x)
  99.         self.ball_v_y.append(self.four_ball_v_y)
  100.  
  101.         self.ball_x.append(self.five_ball_x)
  102.         self.ball_y.append(self.five_ball_y)
  103.         self.ball_v_x.append(self.five_ball_v_x)
  104.         self.ball_v_y.append(self.five_ball_v_y)
  105.    
  106.     def update_ball_velocity(self, index, *args):
  107.         self.scale_value = self.speed.get()
  108.         if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
  109.             self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
  110.         if (self.ball_y[index] > self.scaling_right) or (self.ball_y[index] < self.scaling_left):
  111.             self.ball_v_y[index] = -1.0 *  self.ball_v_y[index]
  112.  
  113.         for n in range(len(self.balls)):
  114.             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)):
  115.                 temp_vx = self.ball_v_x[index]
  116.                 temp_vy = self.ball_v_y[index]
  117.                 self.ball_v_x[index] = self.ball_v_x[n]
  118.                 self.ball_v_y[index] = self.ball_v_y[n]
  119.                 self.ball_v_x[n] = temp_vx
  120.                 self.ball_v_y[n] = temp_vy
  121.        
  122.     def get_ball_deltax(self, index, *args):
  123.         deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
  124.         self.ball_x[index] = self.ball_x[index] + deltax
  125.         return deltax
  126.  
  127.     def get_ball_deltay(self, index, *args):
  128.         deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
  129.         self.ball_y[index] = self.ball_y[index] + deltay
  130.         return deltay
  131.    
  132.     def moveBall(self, *args):
  133.         self.update_ball_velocity(0)      
  134.         deltax = self.get_ball_deltax(0)
  135.         deltay = self.get_ball_deltay(0)
  136.         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
  137.         self.after(10, self.moveBall)
  138.  
  139.     def move_second_ball(self, *args):
  140.         self.update_ball_velocity(1)      
  141.         deltax = self.get_ball_deltax(1)
  142.         deltay = self.get_ball_deltay(1)        
  143.         self.draw.move(self.second_ball,  "%ri" % deltax, "%ri" % deltay)
  144.         self.after(10, self.move_second_ball)
  145.  
  146.  
  147.     def move_three_ball(self, *args):
  148.         self.update_ball_velocity(2)      
  149.         deltax = self.get_ball_deltax(2)
  150.         deltay = self.get_ball_deltay(2)
  151.         self.draw.move(self.three_ball,  "%ri" % deltax, "%ri" % deltay)
  152.         self.after(10, self.move_three_ball)
  153.  
  154.     def move_four_ball(self, *args):
  155.         self.update_ball_velocity(3)      
  156.         deltax = self.get_ball_deltax(3)
  157.         deltay = self.get_ball_deltay(3)
  158.         self.draw.move(self.four_ball,  "%ri" % deltax, "%ri" % deltay)
  159.         self.after(10, self.move_four_ball)
  160.  
  161.     def move_five_ball(self, *args):
  162.         self.update_ball_velocity(4)      
  163.         deltax = self.get_ball_deltax(4)
  164.         deltay = self.get_ball_deltay(4)
  165.         self.draw.move(self.five_ball,  "%ri" % deltax, "%ri" % deltay)
  166.         self.after(10, self.move_five_ball)
  167.  
  168.            
  169.     def __init__(self, master=None):
  170.         Frame.__init__(self, master)
  171.         Pack.config(self)
  172.         self.createWidgets()
  173.         self.after(10, self.moveBall)
  174.         self.after(10, self.move_three_ball)
  175.         self.after(10, self.move_four_ball)
  176.         self.after(10, self.move_five_ball)
  177.         self.after(10, self.move_second_ball)
  178.        
  179.        
  180. game = Pong()
  181.  
  182. game.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement