Advertisement
here2share

# tk_check_if_glichy.py

May 26th, 2021
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # tk_check_if_glichy.py
  2.  
  3. from Tkinter import *
  4. from random import randint
  5.  
  6. class Ball:
  7.     def __init__(self, canvas, x, y, color):
  8.         self.x = x
  9.         self.y = y
  10.         self.canvas = canvas
  11.         self.ball = canvas.create_oval(self.x, self.y, self.x+50, self.y+50, fill=color)
  12.  
  13.     def move_ball(self):
  14.         self.x += randint(0,2)
  15.         if self.x > 300:
  16.             self.x = -50
  17.         self.y += randint(0,2)
  18.         if self.y > 300:
  19.             self.y = -50
  20.         self.canvas.coords(self.ball, self.x, self.y, self.x+50, self.y+50)
  21.         self.canvas.after(1, self.move_ball)
  22.  
  23. # initialize root Window and canvas
  24. root = Tk()
  25. root.title("demo")
  26. root.resizable(False,False)
  27. canvas = Canvas(root, width = 300, height = 300)
  28. canvas.pack()
  29.  
  30. # create ball objects and animate them
  31. ball1 = Ball(canvas, 10, 10, 'red')
  32. ball2 = Ball(canvas, 60, 60, 'green')
  33. ball3 = Ball(canvas, 80, 10, 'orange')
  34.  
  35. ball1.move_ball()
  36. ball2.move_ball()
  37. ball3.move_ball()
  38.  
  39. root.mainloop()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement