Advertisement
gorskaja2019

Untitled

May 1st, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 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, paddle, color):
  7. self.canvas = canvas
  8. self.paddle = paddle
  9. self.id = canvas.create_oval(10,10,25,25,fill=color)
  10. self.canvas.move(self.id, 225, 180)
  11. starts = [-3, -2, -1, 1, 2, 3]
  12. shuffle(starts)
  13. self.x = choice(starts)
  14. self.y = choice(starts)
  15.  
  16. def draw(self):
  17. self.canvas.move(self.id, self.x, self.y)
  18. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  19. if x2 >= self.canvas.winfo_width() or x1 <= 0:
  20. self.x *= -1
  21. if y2 >= self.canvas.winfo_height() or y1 <= 0:
  22. self.y *= -1
  23.  
  24. class Paddle:
  25. def __init__(self, canvas, color):
  26. self.canvas = canvas
  27. self.id = canvas.create_rectangle(0, 0, 100, 15, fill = color)
  28. self.canvas.move(self.id, 200, 300)
  29. self.x = 0
  30. self.canvas.bind_all('<Left>', self.move_left)
  31. self.canvas.bind_all('<Right>', self.move_right)
  32.  
  33. def draw(self):
  34. self.canvas.move(self.id, self.x, 0)
  35. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  36. if x1 <= 0 or x2 >= 500:
  37. self.x *= -1
  38. self.canvas.move(self.id, 5*self.x, 0)
  39.  
  40.  
  41. def move_left(self, event):
  42. self.x = -2
  43. #self.canvas.move(self.id, -2, 0)
  44.  
  45. def move_right(self, event):
  46. self.x = 2
  47. #self.canvas.move(self.id, 2, 0)
  48.  
  49.  
  50. root = Tk()
  51. root.title('Пинг-понг')
  52. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  53. canvas.pack()
  54.  
  55. HarryPotter = Paddle(canvas, 'orange')
  56.  
  57. balls = []
  58. for i in range(3):
  59. R = '%02x'%randint(0,255)
  60. G = '%02x'%randint(0,255)
  61. B = '%02x'%randint(0,255)
  62. color = '#'+R+G+B
  63. ball = Ball(canvas, HarryPotter, color)
  64. balls.append(ball)
  65.  
  66. while True:
  67. for ball in balls:
  68. ball.draw()
  69. HarryPotter.draw()
  70. root.update_idletasks()
  71. root.update()
  72. sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement