python ball game code

#python tkinter
#python version 3.3.2
from tkinter import *
'''
    judge
    two balls
    {
        Center: A(x1,y1) Radius: r X-axis speed: Vax Y-axis speed: Vay
        Center: B(x2,y2) Radius: RX axis speed: Vbx Y axis speed: Vby
    }
    The conditions for collision are:
    1. The distance between the centers of the two small balls is not greater than the sum of the radii of the two small balls (r+R), namely:
    {
        (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
    }
    2. After the balls collide, the degrees of the two balls are exchanged, namely:
    {
        tempVax = Wax
        tempVay = Vay
        Wax = Vbx
        Loan = Vby
        Vbx = tempVax
        Vby = tempVay
        or:
        Wax = Wax + Vbx
        Vbx = Wax - Vbx
        Wax = Wax - Vbx
        Loan = Loan + Vby
        Vby = Vay - Vby
        Loan = Loan - Vby
    }
    game rules:
    Five balls move in the canvas, and they will collide with each other. Of course, the balls will collide with up, down, left, and right.
    After collision, the ball will change direction and return
    The cursor at the bottom is used to adjust the moving speed of the ball. The range of the cursor is [-100, 100]

   
'''
__author__ = {'author' : 'Hongten',
              'Email' : '[email protected]',
              'Blog': 'http://www.cnblogs.com/hongten/',
              'Created' : '2013-09-28',
              'Version' : '1.1'}
class Pong(Frame):
    def createWidgets(self):
         # zoom ratio
        self.scaling = 100.0
        #canvas scale
        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')
        ## Cursor (controls the speed of the ball, range: [-100, 100])
        self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
                           from_=-200, to=200)

        self.speed.pack(side=BOTTOM, fill=X)
        #Small diameter
        self.ball_d = 1.0
        #The range where the ball hits the wall
        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

        # Cursor degrees
        self.scale_value = self.speed.get() * 0.1

        #Store the ball array
        self.balls = []
        #Store the ball x coordinate array
        self.ball_x = []
        #Store the ball y coordinate array
        self.ball_y = []
        #Store the speed array of the ball in the x-axis direction
        self.ball_v_x = []
        #Store the velocity array in the y-axis direction of the ball
        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')
        # put five balls into the 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, the center coordinates of self.ball (self.x, self.y), is scaled here for the purpose of
        #More smoother during the movement of the ball
        self.x = 1.1        
        self.y = 1.1
        # Velocity direction of the first ball
        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
        
        #Update the coordinates of the ball
        self.update_ball_x_y()
        self.draw.pack(side=LEFT)
    def update_ball_x_y(self, *args):
        '''Update the coordinates of the ball, that is, store the center coordinate information and speed information of each ball in the array,
           It is convenient to use when looping through later. '''
        #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 the speed information of each ball, that is, the ball collides with the surrounding and other balls ask for updated speed information'''
        #Cursor value
        self.scale_value = self.speed.get() * 0.1
        #collision wall
        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)):
            #Ball collision conditions, namely: (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)):
                #The speed of the two balls is exchanged
                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):
                #The speed of the two balls is exchanged
                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):
        '''Get the movement distance of the X-axis coordinate of the ball and update the X-coordinate of the center of the ball, and return the required movement distance of the X-axis'''
        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):
        '''Get the movement distance of the Y-axis coordinate of the ball and update the Y-coordinate of the center of the ball, and return the required movement distance of the Y-axis'''
        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, numbered: 0, which is determined according to the array: self.balls. '''
        self.update_ball_velocity(0)       
        deltax = self.get_ball_deltax(0)
        deltay = self.get_ball_deltay(0)
        #ball move
        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):
        '''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 = Pong()
game.mainloop ()


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859511&siteId=291194637