gorskaja2019

Untitled

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