Advertisement
gorskaja2019

Untitled

May 1st, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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. self.hit_bottom = False
  16.  
  17. def draw(self):
  18. global lives
  19. self.canvas.move(self.id, self.x, self.y)
  20. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  21. if x2 >= self.canvas.winfo_width() or x1 <= 0:
  22. self.x *= -1
  23. if y1 <= 0:
  24. self.y *= -1
  25. if y2 >= self.canvas.winfo_height():
  26. self.hit_bottom = True
  27. lives -= 1
  28. self.canvas.itemconfigure(lives_text, text = 'Жизней: '+str(lives))
  29. if self.hit_paddle():
  30. self.y *= -1
  31.  
  32. def hit_paddle(self):
  33. global score
  34. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  35. (A1, B1, A2, B2) = self.canvas.coords(self.paddle.id)
  36. if x1 >= A1 and x2 <= A2 and y2 >= B1 and y2 <= B2 - 5:
  37. score += 1
  38. self.canvas.itemconfigure(score_text, text = 'Счет: '+str(score))
  39. return True
  40.  
  41. class Paddle:
  42. def __init__(self, canvas, color):
  43. self.canvas = canvas
  44. self.id = canvas.create_rectangle(0, 0, 100, 15, fill = color)
  45. self.canvas.move(self.id, 200, 300)
  46. self.x = 0
  47. self.canvas.bind_all('<Left>', self.move_left)
  48. self.canvas.bind_all('<Right>', self.move_right)
  49.  
  50. def draw(self):
  51. self.canvas.move(self.id, self.x, 0)
  52. (x1, y1, x2, y2) = self.canvas.coords(self.id)
  53. if x1 <= 0 or x2 >= 500:
  54. self.x *= -1
  55. self.canvas.move(self.id, 5*self.x, 0)
  56.  
  57.  
  58. def move_left(self, event):
  59. self.x = -2
  60. #self.canvas.move(self.id, -2, 0)
  61.  
  62. def move_right(self, event):
  63. self.x = 2
  64. #self.canvas.move(self.id, 2, 0)
  65.  
  66.  
  67. root = Tk()
  68. root.title('Пинг-понг')
  69. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  70. canvas.pack()
  71. score = 0
  72. lives = 3
  73. score_text = canvas.create_text(10,10,anchor = 'nw', font = 'Verdana 18', fill = 'darkblue', text = 'Счет: '+str(score))
  74. lives_text = canvas.create_text(500-10,10,anchor = 'ne', font = 'Verdana 18', fill = 'darkblue', text = 'Жизней: '+str(lives))
  75.  
  76. HarryPotter = Paddle(canvas, 'orange')
  77.  
  78. root.update()
  79.  
  80. balls = []
  81. for i in range(lives):
  82. R = '%02x'%randint(0,255)
  83. G = '%02x'%randint(0,255)
  84. B = '%02x'%randint(0,255)
  85. color = '#'+R+G+B
  86. ball = Ball(canvas, HarryPotter, color)
  87. balls.append(ball)
  88.  
  89. def on_click(event):
  90. while True:
  91. if lives > 0:
  92. for ball in balls:
  93. if ball.hit_bottom == False:
  94. ball.draw()
  95. HarryPotter.draw()
  96. else:
  97. canvas.create_text(220,200,anchor = 'c', font = 'Verdana 36', fill = 'red', text = 'Конец игры!')
  98. root.update_idletasks()
  99. root.update()
  100. sleep(0.01)
  101. canvas.bind('<Button-1>', on_click)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement