Advertisement
gorskaja2019

Untitled

Apr 24th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 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. balls = []
  28. for i in range(10):
  29. R = '%02x'%randint(0,255)
  30. G = '%02x'%randint(0,255)
  31. B = '%02x'%randint(0,255)
  32. color = '#'+R+G+B
  33. ball = Ball(canvas, color)
  34. balls.append(ball)
  35.  
  36. while True:
  37. for ball in balls:
  38. ball.draw()
  39. root.update_idletasks()
  40. root.update()
  41. sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement