Tkinter Canvas Designed to let the ball bounce up and down
If you want the designed ball to bounce up and down, you must first understand how the tkinter module defines the position of the object. In fact, for this example, you can use the coords() method to get the object position, and its return value is the coordinates of the top left and bottom right corners of the object.
Example#1
The main thing is to create a ball and then use the coords() method to list the messages of the ball position.
from tkinter import *
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk,width=500, height=150)
canvas.pack()
oval_id = canvas.create_oval(10,50,60,100,fill='yellow',outline='lightgray')
ballPos = canvas.coords(oval_id)
print(ballPos) # [10.0, 50.0, 60.0, 100.0]
Output:
deepinout@ubuntu:~/apidemos.com$ python3 demos.py
[10.0, 50.0, 60.0, 100.0]
If you use the above execution results, you can use the following diagram to explain.
Equivalent to the following results can be obtained with the coords( ) method.
ballPos[0]: the left x-axis coordinate of the ball, which can be used to determine if it hits the left side of the canvas.
ballPos[1]: the upper y-axis coordinate of the ball, which can be used to determine whether it hits the top of the canvas or not.
ballPos[2]: the right x-axis coordinate of the ball, can be used to determine if it hits the right side of the canvas.
ballPos[3]: the left y-axis of the ball, which can be used to determine if the ball hits the bottom of the canvas.
Example#2
The design allows the ball to move up and down. Actually this program just changes the Ball category content.
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 the initial position of the ball
self.x = 0 # No horizontal movement
self.y = step # Vertical movement unit
def ballMove(self):
self.canvas.move(self.id, self.x, self.y) # step is a positive value indicating a downward movement
ballPos = self.canvas.coords(self.id)
if ballPos[1] <= 0:
self.y = step
if ballPos[3] >= winH:
self.y = -step
winW = 640 # Define the width of the canvas
winH = 480 # Define the canvas height
step = 3 # Defining velocity can be thought of as a displacement step
speed = 0.03 # Set movement speed
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:
Line 11 of the program defines that the ball does not move on the x-axis and line 12 defines that the y-axis moves in step units. line 15 gets the ball’s position information and lines 16 and 17 detect that the ball is moving down in step units if it hits the top of the canvas and lines 18 and 19 detect that the ball is moving up in step units if it hits the bottom of the canvas (because of the negative value).