Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_ballision
- from Tkinter import *
- '''
- Judge
- Two balls
- {
- Center: A (x1, Y1) radius: R X speed: Vax Y shaft speed: Vay
- Center: B (X2, Y2) radius: R X speed: Vbx Y shaft speed: Vby
- }
- Collision condition:
- 1 the two ball of center distance of not more than two ball radius and (r+R), i.e.:
- {
- (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
- }
- The collision of 2 small ball, the number of exchange of two balls, i.e.:
- {
- tempVax = Vax
- tempVay = Vay
- Vax = Vbx
- Vay = Vby
- Vbx = tempVax
- Vby = tempVay
- Or:
- Vax = Vax + Vbx
- Vbx = Vax - Vbx
- Vax = Vax - Vbx
- Vay = Vay + Vby
- Vby = Vay - Vby
- Vay = Vay - Vby
- }
- The rules of the game:
- 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
- After the collision, the ball will return to change direction
- The following is used to move cursor speed adjusting ball, the cursor is in the range of[-100, 100]
- Defect or BUG:
- 1: changes in the cursor data so as to change the ball moving speed, moving the ball distance is not timely update
- Cause the ball may escape from the canvas
- 2: balls in athletic process, sometimes may also escape the canvas
- Summary:
- Finish this game, spent a week's time. In this process, not only to learn the knowledge of mathematics in high school,
- Knowledge of physics, a lot of things are forgotten, but soon learn to.
- The game actually is a lot of mathematical problems.
- Defects or BUG also exist in the game, hope that like-minded can work together to improve.
- 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
- 2: movement speed by 2 ball is smaller, but the ball moving speed changes according to the cursor
- '''
- __author__ = {'author' : 'Hongten',
- 'Email' : '',
- 'Blog' : 'http://www.cnblogs.com/hongten/',
- 'Created' : '2013-09-28',
- 'Version' : '1.1'}
- class Ballision(Frame):
- def createWidgets(self):
- #Discharge rate
- self.scaling = 100.0
- #The proportion of the canvas
- self.canvas_width = 10
- self.canvas_height = 5.6
- ## Canvas
- self.draw = Canvas(self, width=(self.canvas_width * self.scaling),
- height=(self.canvas_height * self.scaling),
- bg='white')
- ## The cursor (control the ball moving speed, range: [-100, 100])
- self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
- from_=-200, to=200)
- self.speed.pack(side=BOTTOM, fill=X)
- #The ball diameter
- self.ball_d = 1.0
- #The scope of small ball collision walls
- self.scaling_left = round(self.ball_d / 2, 1)
- self.scaling_right = self.canvas_width - self.scaling_left
- self.scaling_bottom = self.canvas_height - self.scaling_left
- self.scaling_top = self.scaling_left
- #The cursor.
- self.scale_value = self.speed.get() * 0.1
- #Storage ball array
- self.balls = []
- #Storage of X ball array
- self.ball_x = []
- #Storage of Y ball array
- self.ball_y = []
- #Storage of X axis speed ball array
- self.ball_v_x = []
- #Storage of Y axis speed ball array
- self.ball_v_y = []
- # Five balls
- self.ball = self.draw.create_oval("0.60i", "0.60i", "1.60i", "1.60i",
- fill="red")
- self.second_ball = self.draw.create_oval("2.0i", "2.0i", "3.0i", "3.0i",
- fill='black')
- self.three_ball = self.draw.create_oval("4.0i", "4.0i", "5.0i", "5.0i",
- fill='brown')
- self.four_ball = self.draw.create_oval("6.0i", "2.0i", "7.0i", "3.0i",
- fill='green')
- self.five_ball = self.draw.create_oval("8.0i", "3.0i", "9.0i", "4.0i",
- fill='gray')
- #The five ball into an array
- self.balls.append(self.ball)
- self.balls.append(self.second_ball)
- self.balls.append(self.three_ball)
- self.balls.append(self.four_ball)
- self.balls.append(self.five_ball)
- #The first ball, namely self.ball coordinates (self.x, self.y), here are shrinking, the purpose is to
- #In the process of moving the ball more fluent in
- self.x = 1.1
- self.y = 1.1
- #The first ball velocity direction
- self.velocity_x = -0.2
- self.velocity_y = 0.1
- self.second_ball_x = 2.5
- self.second_ball_y = 2.5
- self.second_ball_v_x = 0.1
- self.second_ball_v_y = -0.2
- self.three_ball_x = 4.5
- self.three_ball_y = 4.5
- self.three_ball_v_x = -0.1
- self.three_ball_v_y = -0.2
- self.four_ball_x = 6.5
- self.four_ball_y = 2.5
- self.four_ball_v_x = 0.1
- self.four_ball_v_y = -0.2
- self.five_ball_x = 8.5
- self.five_ball_y = 3.5
- self.five_ball_v_x = 0.1
- self.five_ball_v_y = 0.2
- #Coordinate updates the ball
- self.update_ball_x_y()
- self.draw.pack(side=LEFT)
- def update_ball_x_y(self, *args):
- '''Coordinate to update the ball, that each ball center coordinates information and velocity information is stored in an array,
- Easy to use in the back of cyclic traversal. '''
- #The first ball information
- self.ball_x.append(self.x)
- self.ball_y.append(self.y)
- self.ball_v_x.append(self.velocity_x)
- self.ball_v_y.append(self.velocity_y)
- self.ball_x.append(self.second_ball_x)
- self.ball_y.append(self.second_ball_y)
- self.ball_v_x.append(self.second_ball_v_x)
- self.ball_v_y.append(self.second_ball_v_y)
- self.ball_x.append(self.three_ball_x)
- self.ball_y.append(self.three_ball_y)
- self.ball_v_x.append(self.three_ball_v_x)
- self.ball_v_y.append(self.three_ball_v_y)
- self.ball_x.append(self.four_ball_x)
- self.ball_y.append(self.four_ball_y)
- self.ball_v_x.append(self.four_ball_v_x)
- self.ball_v_y.append(self.four_ball_v_y)
- self.ball_x.append(self.five_ball_x)
- self.ball_y.append(self.five_ball_y)
- self.ball_v_x.append(self.five_ball_v_x)
- self.ball_v_y.append(self.five_ball_v_y)
- def update_ball_velocity(self, index, *args):
- '''Update each ball speed information, namely the collision ball around and another ball for updating speed information'''
- #Cursor
- self.scale_value = self.speed.get() * 0.1
- #Collision walls
- if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
- self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
- if (self.ball_y[index] > self.scaling_bottom) or (self.ball_y[index] < self.scaling_top):
- self.ball_v_y[index] = -1.0 * self.ball_v_y[index]
- '''
- #TEST:
- for n in range(len(self.balls)):
- #print((self.ball_x[index] - self.ball_x[n])**2)
- #print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2))
- 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))
- '''
- for n in range(len(self.balls)):
- #Small ball collision conditions, i.e.: (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
- 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)):
- #Two ball speed exchange
- temp_vx = self.ball_v_x[index]
- temp_vy = self.ball_v_y[index]
- self.ball_v_x[index] = self.ball_v_x[n]
- self.ball_v_y[index] = self.ball_v_y[n]
- self.ball_v_x[n] = temp_vx
- self.ball_v_y[n] = temp_vy
- #print(self.ball_v_x, self.ball_v_y)
- '''
- #WRONG:
- for n in range(len(self.balls)):
- if (((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2) <= self.ball_d**2):
- #Two ball speed exchange
- self.ball_v_x[index] = self.ball_v_x[index] + self.ball_v_x[n]
- self.ball_v_x[n] = self.ball_v_x[0] - self.ball_v_x[n]
- self.ball_v_x[index] = self.ball_v_x[index] - self.ball_v_x[n]
- self.ball_v_y[index] = self.ball_v_y[index] + self.ball_v_y[n]
- self.ball_v_y[n] = self.ball_v_y[index] - self.ball_v_y[n]
- self.ball_v_y[index] = self.ball_v_y[index] - self.ball_v_y[n]
- print(self.ball_v_x, self.ball_v_y)
- '''
- def get_ball_deltax(self, index, *args):
- '''The ball X axis moving distance and update the ball center coordinates X, return to the X axis required to move the distance'''
- deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
- self.ball_x[index] = self.ball_x[index] + deltax
- return deltax
- def get_ball_deltay(self, index, *args):
- '''The ball Y axis moving distance and update the ball center coordinates Y, return to the Y axis required to move the distance'''
- deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
- self.ball_y[index] = self.ball_y[index] + deltay
- return deltay
- def moveBall(self, *args):
- '''Move the first ball, number: 0, which is based on the array: self.balls determination. '''
- self.update_ball_velocity(0)
- deltax = self.get_ball_deltax(0)
- deltay = self.get_ball_deltay(0)
- #The ball movement
- self.draw.move(self.ball, "%ri" % deltax, "%ri" % deltay)
- self.after(10, self.moveBall)
- def move_second_ball(self, *args):
- self.update_ball_velocity(1)
- deltax = self.get_ball_deltax(1)
- deltay = self.get_ball_deltay(1)
- self.draw.move(self.second_ball, "%ri" % deltax, "%ri" % deltay)
- self.after(10, self.move_second_ball)
- def move_three_ball(self, *args):
- self.update_ball_velocity(2)
- deltax = self.get_ball_deltax(2)
- deltay = self.get_ball_deltay(2)
- self.draw.move(self.three_ball, "%ri" % deltax, "%ri" % deltay)
- self.after(10, self.move_three_ball)
- def move_four_ball(self, *args):
- self.update_ball_velocity(3)
- deltax = self.get_ball_deltax(3)
- deltay = self.get_ball_deltay(3)
- self.draw.move(self.four_ball, "%ri" % deltax, "%ri" % deltay)
- self.after(10, self.move_four_ball)
- def move_five_ball(self, *args):
- self.update_ball_velocity(4)
- deltax = self.get_ball_deltax(4)
- deltay = self.get_ball_deltay(4)
- self.draw.move(self.five_ball, "%ri" % deltax, "%ri" % deltay)
- self.after(10, self.move_five_ball)
- def __init__(self, master=None):
- '''The initialization function'''
- Frame.__init__(self, master)
- Pack.config(self)
- self.createWidgets()
- self.after(10, self.moveBall)
- self.after(10, self.move_three_ball)
- self.after(10, self.move_four_ball)
- self.after(10, self.move_five_ball)
- self.after(10, self.move_second_ball)
- game = Ballision()
- game.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement