Я начинаю использовать TKinter и пытаюсь сделать несколько мячей для боя в качестве тренировочного упражнения. Когда я создаю уникальный овал и заставляю его двигаться, все работает, но когда я создаю класс Ball с методом move, шар вообще не движется. Я не получаю сообщение об ошибке, но он отказывается двигаться.
"""
Initialisation
"""
from tkinter import *
import time
w_height = 600
w_width = 800
xspeed = 10
yspeed = 10
window = Tk()
window.geometry ("{}x{}".format(w_width, w_height))
window.title("Bouncing Balls")
canvas = Canvas(window, width = w_width - 50,height = w_height - 50, bg="black")
"""
If i create the ball like that and make it move it works fine
"""
ball1 = canvas.create_oval(10, 10, 50, 50, fill ="red")
while True:
canvas.move(ball1, xspeed, yspeed)
window.update()
time.sleep(0.05)
"""
But if i try to create the ball using a class it doesn't work anymore...
"""
class Ball:
def __init__(self, posx, posy, r):
canvas.create_oval(posx-r, posy-r, posx+r, posy+r, fill ="red")
def move(self, dx, dy):
canvas.move(self, dx, dy)
ball1 = Ball(50,50,10)
while True:
ball1.move(xspeed, yspeed)
window.update()
time.sleep(0.05)
Я думал, что это даст тот же результат, но в первом случае мяч движется, а во втором - нет, и я не могу понять, почему.