Tkinter Canvas Design racket movement
Since it is assumed to use the keyboard’s right and left shift keys to move the racket, it can be added inside Racket’s __init__()
function, using the bind_all( ) method to bind the way the keyboard keys move when they occur.
self.canvas.bind_all('KeyPress-Right', self.moveRight) # Bind Press and hold the right button
self.canvas.bind_all('KeyPress-Left', self.moveLeft) # Bind Press and hold the left button
Therefore, the following moveRight() and moveLeft() designs are added to the Racket class.
def moveLeft(self, event): # The number of units the racket moves to the left each time
self.x = -3
def moveRight(self, event): # The number of units the racket moves to the right each time
self.x = 3
The above design is equivalent to a displacement of 3 each time, if the game has a set level, you can let the novice displacement increase and let the displacement decrease as the level increases. In addition, this program adds a racket to move the main body design as follows.
def racketMove(self): # Design the racket to move
self.canvas.move(self.id, self.x, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0: # whether to touch the left side of the canvas when moving
self.x = 0
elif pos[2] >= winW: # whether to touch the right side of the canvas when moving
self.x = 0
The main program will also add calls to the racket move method.
while True:
ball.ballMove()
racket.racketMove()
tk.update()
time.sleep(speed) # Can control the speed of movement
Example#1
Add the design so that the racket can move left and right. line 31 of the following program sets the racket displacement to 0 at the beginning of the program. the following is the content of the racket class.
from tkinter import *
from random import *
import time
class Ball:#class Ball():#Written like this is the same
def __init__(self,canvas,color,winW,winH):
self.canvas = canvas
self.id = canvas.create_oval(0, 0, 20, 20, fill=color) # create ball object
self.canvas.move(self.id,winW/2,winH/2) # set the initial position of the ball
startPos = [-4, -3, -2, -1, 1, 2, 3, 4] # random number of initial x-axis movements of the ball
shuffle(startPos) # upset the ordering
self.x = startPos[0] # initial horizontal movement of the ball in units
self.y = step # vertical units of movement
def ballMove(self):
self.canvas.move(self.id, self.x, self.y) # step is positive for downward movement
ballPos = self.canvas.coords(self.id)
if ballPos[0] <= 0: # detect if the ball is over the left side of the canvas
self.x = step
if ballPos[1] <= 0: # detect if the ball is over the top of the canvas
self.y = step
if ballPos[2] >= winW: # detect if the ball goes over the right side of the canvas
self.x = -step
if ballPos[3] >= winH: # detect if the ball is over the bottom of the canvas
self.y = -step
class Racket():
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0,0,100,15,fill=color) # racket object
self.canvas.move(self.id, 270, 400)
self.x = 0
self.canvas.bind_all('<KeyPress-Right>',self.moveRight) # bind to press and hold the right button ##########################
self.canvas.bind_all('<KeyPress-Left>',self.moveLeft) # bind to hold left
def racketMove(self):
self.canvas.move(self.id,self.x,0)
pos = self.canvas.coords(self.id)
# self.x = 0
if pos[0] <= 0:
self.x = 0
elif pos[2] >= winW:
self.x = 0
def moveLeft(self,event):
print("Press Left Key")
self.x = -3
def moveRight(self,event):
print("Press Right Key")
self.x = 3
winW = 640 # Define the width of the canvas
winH = 480 # Define the height of the canvas
step = 3 # Define the velocity to be thought of as a displacement step
speed = 0.03 # Set the speed of movement
tk = Tk()
tk.title("Bouncing Ball-apidemos") # Game window title
tk.wm_attributes('-topmost',1) # Make sure the game window is at the top of the screen
canvas = Canvas(tk,width=winW, height=winH) # Create a canvas
canvas.pack()
tk.update()
# The order in which the following two lines of code are executed determines which object is occluded when the two overlap
racket = Racket(canvas,'purple') # Define the purple racket
ball = Ball(canvas,'yellow',winW,winH) # Define the ball object
while True:
ball.ballMove()
racket.racketMove()
tk.update()
time.sleep(speed) # You can control the speed of movement
# tk.mainloop()
Output: