Advertisement
here2share

# Tk_ballision

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