A bouncing ball game written by python3.6 using tkinter

import random
import time
from tkinter import *


#Define a ball class below, with canvas and color objects
class Ball: #Define a function of the Ball class
    def __init__(self, canvas, paddle, color): #This is the attribute function of the Ball class, and the functions under the Ball class have these properties
        self.canvas=canvas
        self.paddle=paddle
        self.id=canvas.create_oval(10,10,25,25,fill=color)#Return the call value of the drawn ball and put it into the object self.id
        self.canvas.move(self.id,245,100) #Move the ball to (245,100) coordinates,
        starts=[-3,-2,-1,1,2,3]      
        random.shuffle(starts)
        self.x=starts[0] #Make the ball move randomly in the left and right directions
        self.y=-3 #The default starting ball moves upwards
        self.canvas_height=self.canvas.winfo_height()#The return value of the canvas height function winfo_height() is put into the canvas_height object
        self.canvas_width=self.canvas.winfo_width() #winfo_width() returns the width of the canvas and puts it in the canvas_width object
        self.hit_bottom=False #Set the initial value of hit_bottom to false


    def hit_paddle(self,pos): #declare a function for calling
        paddle_pos=self.canvas.coords(self.paddle.id) #Put the coordinates of the racket (x1,y1)(x2,y2) into paddle_pos
        if pos[2]>=paddle_pos[0] and pos[0]<=paddle_pos[2]: #
            if pos[3]>=paddle_pos[1] and pos[3]<=paddle_pos[3]: #Compare whether the y-axis of the small ball is in the y-axis of the racket
                return True #Indicates that the ball touches the racket
        return False #Indicates that the ball did not touch the racket


    def draw(self): #declare the draw function,
        self.canvas.move(self.id,self.x,self.y) #Move the ball, the movement speed is (self.x, self.y), the attributes in init can be used directly
        pos=self.canvas.coords(self.id)#Put the coordinates of the upper left and lower right corners of the ball into the pos object in the form of a list (possible tuple)
        if pos[1]<=0: #If the ball touches the top of the canvas
            self.y=3 #Then change the moving direction to the bottom
        if pos[3]>=self.canvas_height: #If the ball touches the bottom of the canvas, return hit_bottom as True
            self.hit_bottom=True
        if self.hit_paddle(pos)==True: #The ball touches the racket, then change the direction of the Y axis to move up
            self.y=-3
        if pos[0]<=0: #If the ball touches the left side of the canvas, change the X-axis speed to 3 pixels to the right each time
            self.x=3
        if pos[2]>=self.canvas_width: #If the ball touches the right side of the canvas, change the speed to 3 pixels to the left each time
            self.x=-3

class Paddle: #Define a paddle class
    def __init__(self, canvas, color): #Attribute function of the paddle class, there are two variables canvas and color by default
        self.canvas=canvas #Assign the canvas object to self.canvas
        self.id=canvas.create_rectangle(0,0,100,10,fill=color) #Create a racket and store the call number of the racket in self.id
        self.canvas.move(self.id,200,300) #Move the racket to (200,300)
        self.x=0    #
        self.canvas_width=self.canvas.winfo_width() #Put the width of the canvas into the canvas_width object
        self.canvas.bind_all('<KeyPress-Left>',self.turn_left) #Use the bind_all() function to bind the left key of the keyboard and the tun_left function
        self.canvas.bind_all('<KeyPress-Right>',self.turn_right) #Bind the keyboard right key and turn_right function
      

    def draw(self): #declare a draw function
        self.canvas.move(self.id,self.x,0) #The speed of moving the racket left and right is self.x, and it does not move by default
        pos=self.canvas.coords(self.id) #Store the coordinates of the upper left and lower right corners of the racket into the pos object
        if pos[0]<=0: #If the x-axis of the racket is less than 0, it will no longer move to the right
            self.x=0
        elif pos[2]>=self.canvas_width: #If the racket is to exceed the right side of the canvas, the movement speed of the racket becomes 0
            self.x=0

    def turn_left(self,evt):#The evt here is the parameter passed by the caller, changing the movement speed of the racket to the left,
        self.x=-5

    def turn_right(self,evt): #Change the movement speed of the racket to the right every 5 pixels
        self.x=5


    
#Create a frame and name and fix it, then create a canvas for the frame
tk=Tk() #Create a frame object tk
tk.title('Game') #The name displayed by the frame object tk is 'game'
tk.resizable(0,0) #fixed frame
tk.wm_attributes('-topmost',1) #Display in the outermost layer
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) #Create a canvas canvas, which belongs to the tk frame object,
canvas.pack() #Display canvas changes
tk.update() #Display frame changes


#Assign the class to the object ball, if the ball is called, the function of the class can be realized
paddle=Paddle(canvas,"blue") #Call the shot class for the object paddle
ball=Ball(canvas,paddle,'green') #Call the class of the ball for the object ball

while True: #Pay attention to the while statement to prevent an infinite loop, set it to true first
    if ball.hit_bottom==False: #If the bottom is not touched, execute the following statement
        ball.draw() #call the function draw() of the ball object
        paddle.draw()#calls the function draw() of the paddle object
        tk.update_idletasks()
        tk.update() #Update the frame
        time.sleep(0.01) #Sleep for 0.01 seconds
    elif ball.hit_bottom==True: #If the ball touches the bottom
        canvas.create_text(200,100,text='Aha,you lose it,\nHow about try again?',font=('Times',22)) #Create text '...' at (200,100) coordinates, font size 22
        tk.update() #Update content

Guess you like

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