Tkinter Canvas Design so that the ball bounces on all sides of the canvas
In the bouncing ball game, we have to make the ball bounce on all sides, so we have to take into account that the ball is moving on the x-axis, and we need to change the following two lines in the __init__()
function of the original Ball class.
self.x = 0 # No horizontal movement
self.y = step # Vertical movement unit
Here are the results of the changes.
startPos = [-4, -3, -2 -1, 1, 2, 3, 4] # Random number of the initial x-axis displacement of the ball
shuffle(startPos) # Disorderly arrangement
self.x = startPos[0] # Ball initially moves horizontally in units
self.y = step # Vertical movement units
The idea of the above program modification is that when the ball game starts, the unit of movement of x-axis of each loop is generated by a random number. As for the ballMove() method, we have to consider the situation that the horizontal axis movement may collide with the left and right side of the canvas, is if the ball hits the left side of the canvas, set the ball to move along the x-axis is a positive value, that is, to the right.
if ballPos[0] <= 0: # Detects if the ball is over the left side of the canvas
self.x = step
If the ball hits the right side of the canvas, set the ball to move negatively on the x-axis, that is, to the left.
if ballPos[2] >= winW: # Detects if the ball is over the right side of the canvas
self.x = -step
Example#1
The ball can be moved around.
from tkinter import *
from random import *
import time
class Ball:#class Ball():#It is written the same way
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 initial ball position
startPos = [-4, -3, -2, -1, 1, 2, 3, 4] # Random number of the initial x-axis displacement of the ball
shuffle(startPos) # Upset the order
self.x = startPos[0] # the ball initially moves horizontally in units
self.y = step # vertical movement units
def ballMove(self):
self.canvas.move(self.id, self.x, self.y) # step is a positive value to move down
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
winW = 640 # Define the width of the canvas
winH = 480 # Define the height of the canvas
step = 3 # Define velocity think of it 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 canvas
canvas.pack()
tk.update()
ball = Ball(canvas,'yellow',winW,winH) # Defining the ball object
while True:
ball.ballMove()
tk.update()
time.sleep(speed) # Can control the speed of movement
# tk.mainloop()
Output: