# https://youtu.be/qK8Pfll5ha8
from tkinter import *
from time import sleep
class Ball:
def __init__(self, canvas, x, y, dia, xVel, yVel, color, outline):
self.canvas = canvas
self.image = canvas.create_oval(x, y, dia, dia, fill=color, outline=outline)
self.xVel = xVel
self.yVel = yVel
def move(self):
coordinates = self.canvas.coords(self.image)
if (coordinates[2] >= (self.canvas.winfo_width()) or coordinates[0] < 0):
self.xVel = -self.xVel
if (coordinates[3] >= (self.canvas.winfo_height()) or coordinates[1] < 0):
self.yVel = -self.yVel
self.canvas.move(self.image, self.xVel, self.yVel)
win = Tk()
win.geometry("500x500")
WIDTH, HEIGHT = 500, 500
canvas = Canvas(win, width=WIDTH, height=HEIGHT)
canvas.place(x=0, y=0)
volley_ball = Ball(canvas, 0, 0, 100, 1, 1, "black", "black")
tennis_ball = Ball(canvas, 0, 0, 50, 4, 3, "yellow", "white")
basket_ball = Ball(canvas, 0, 0, 125, 4, 3, "orange", "white")
while True:
volley_ball.move()
tennis_ball.move()
basket_ball.move()
win.update()
sleep(0.01)
win.mainloop()
No comments:
Post a Comment