Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_check_if_glichy.py
- from Tkinter import *
- from random import randint
- class Ball:
- def __init__(self, canvas, x, y, color):
- self.x = x
- self.y = y
- self.canvas = canvas
- self.ball = canvas.create_oval(self.x, self.y, self.x+50, self.y+50, fill=color)
- def move_ball(self):
- self.x += randint(0,2)
- if self.x > 300:
- self.x = -50
- self.y += randint(0,2)
- if self.y > 300:
- self.y = -50
- self.canvas.coords(self.ball, self.x, self.y, self.x+50, self.y+50)
- self.canvas.after(1, self.move_ball)
- # initialize root Window and canvas
- root = Tk()
- root.title("demo")
- root.resizable(False,False)
- canvas = Canvas(root, width = 300, height = 300)
- canvas.pack()
- # create ball objects and animate them
- ball1 = Ball(canvas, 10, 10, 'red')
- ball2 = Ball(canvas, 60, 60, 'green')
- ball3 = Ball(canvas, 80, 10, 'orange')
- ball1.move_ball()
- ball2.move_ball()
- ball3.move_ball()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement