Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.screenmanager import ScreenManager, Screen
  3. from kivy.lang import Builder
  4. from kivy.uix.widget import Widget
  5. from kivy.properties import NumericProperty, ReferenceListProperty,\
  6. ObjectProperty
  7. from kivy.vector import Vector
  8. from kivy.clock import Clock
  9. from kivy.uix.popup import Popup
  10. from kivy.uix.label import Label
  11.  
  12. Builder.load_file("pong.kv")
  13.  
  14. class PongPaddle(Widget):
  15. score = NumericProperty(0)
  16.  
  17. def bounce_ball(self, ball):
  18. if self.collide_widget(ball):
  19. vx, vy = ball.velocity
  20. offset = (ball.center_y - self.center_y) / (self.height / 2)
  21. bounced = Vector(-1 * vx, vy)
  22. vel = bounced * 1.1
  23. ball.velocity = vel.x, vel.y + offset
  24.  
  25. class PongBall(Widget):
  26. velocity_x = NumericProperty(0)
  27. velocity_y = NumericProperty(0)
  28. velocity = ReferenceListProperty(velocity_x, velocity_y)
  29.  
  30. def move(self):
  31. self.pos = Vector(*self.velocity) + self.pos
  32.  
  33. class PongGame(Widget):
  34. ball = ObjectProperty(None)
  35. player1 = ObjectProperty(None)
  36. player2 = ObjectProperty(None)
  37.  
  38.  
  39. def serve_ball(self, vel=(4, 0)):
  40. self.ball.center = self.center
  41. self.ball.velocity = vel
  42.  
  43. global win_label
  44.  
  45. def winner_check(self):
  46. winner = ''
  47. maxScore = 1
  48. if self.player1.score == maxScore:
  49. Clock.unschedule(gs.game.update)
  50. gs.game.boxwinner.opacity = 1
  51. gs.game.gamewinner.text = 'Winner: Player 1!'
  52.  
  53. elif self.player2.score == maxScore:
  54. Clock.unschedule(gs.game.update)
  55. gs.game.boxwinner.opacity = 1
  56. gs.game.gamewinner.text = 'Winner: Player 2!'
  57.  
  58.  
  59. def update(self, dt):
  60. self.ball.move()
  61.  
  62. self.winner_check()
  63.  
  64.  
  65. # відбивання від ракеток
  66. self.player1.bounce_ball(self.ball)
  67. self.player2.bounce_ball(self.ball)
  68.  
  69. # відбивання від верхньої й нижньої межі
  70. if (self.ball.y < self.y) or (self.ball.top > self.top):
  71. self.ball.velocity_y *= -1
  72.  
  73. # вихід за межі поля й нарахування ігрових очок
  74. if self.ball.x < self.x:
  75. self.player2.score += 1
  76. self.serve_ball(vel=(4, 0))
  77. if self.ball.x > self.width:
  78. self.player1.score += 1
  79. self.serve_ball(vel=(-4, 0))
  80.  
  81.  
  82.  
  83.  
  84. def on_touch_move(self, touch):
  85. if touch.x < self.width / 3:
  86. self.player1.center_y = touch.y
  87. if touch.x > self.width - self.width / 3:
  88. self.player2.center_y = touch.y
  89. if self.width / 3 < touch.x < self.width - self.width / 3:
  90. sm.current = "menuScreen"
  91. Clock.unschedule(gs.game.update)
  92.  
  93. # Declare both screens
  94. class MenuScreen(Screen):
  95. def start_game(self):
  96.  
  97. gs.game.player1.score = 0
  98. gs.game.player2.score = 0
  99. gs.game.gamewinner.text = ''
  100. gs.game.boxwinner.opacity = 0
  101. sm.current="gameScreen"
  102. gs.game.serve_ball()
  103. Clock.unschedule(gs.game.update)
  104. Clock.schedule_interval(gs.game.update, 1.0 / 60.0)
  105.  
  106. def resume_game(self):
  107. sm.current = 'gameScreen'
  108. Clock.schedule_interval(gs.game.update, 1.0 / 60.0)
  109.  
  110. class GameScreen(Screen):
  111. game = ObjectProperty(None)
  112.  
  113.  
  114.  
  115. # Create the screen manager
  116. sm = ScreenManager()
  117. gs = GameScreen(name='gameScreen')
  118. sm.add_widget(MenuScreen(name='menuScreen'))
  119. sm.add_widget(gs)
  120.  
  121.  
  122. class PongApp(App):
  123.  
  124. def build(self):
  125. return sm
  126.  
  127. if __name__ == '__main__':
  128. PongApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement