Advertisement
gorskaja2019

Untitled

Apr 24th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. from tkinter import*
  2. from random import *
  3. from time import *
  4.  
  5. class Ball:
  6. def __init__(self, canvas, color):
  7. self.canvas = canvas
  8. self.id = canvas.create_oval(10,10,25,25,fill=color)
  9. self.canvas.move(self.id, 225, 180)
  10. starts = [-3, -2, -1, 1, 2, 3]
  11. self.x = choice(starts)
  12. self.y = choice(starts)
  13.  
  14. def draw(self):
  15. self.canvas.move(self.id, self.x, self.y)
  16. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  17. if x2 >= self.canvas.winfo_width() or x1 <= 0:
  18. self.x *= -1
  19. if y2 >= self.canvas.winfo_height() or y1 <= 0:
  20. self.y *= -1
  21.  
  22. root = Tk()
  23. root.title('Пинг-понг')
  24. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  25. canvas.pack()
  26.  
  27. ball = Ball(canvas, 'red')
  28.  
  29. while True:
  30. ball.draw()
  31. root.update_idletasks()
  32. root.update()
  33. sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement