Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. __author__ = 'Ania'
  2. """
  3. Nalezy napisac gre Pong na urzadzenie mobilne
  4. """
  5. #:kivy 1.9.0
  6. from kivy.app import App
  7. from kivy.uix.widget import Widget
  8. from kivy.properties import NumericProperty, ReferenceListProperty, Clock, \
  9. ObjectProperty
  10. from kivy.vector import Vector
  11.  
  12. """
  13. klasa reprezentujaca pilke (jej ruch)
  14. """
  15. class PongBall(Widget):
  16.  
  17. # predkosc pilki na osiach x i y
  18. velocity_x = NumericProperty(10000)
  19. velocity_y = NumericProperty(10000)
  20. velocity = ReferenceListProperty(velocity_x, velocity_y)
  21.  
  22. def move(self):
  23. self.pos = Vector(*self.velocity) + self.pos
  24. """
  25. klasa reprezentujaca paletke tenisowa
  26. """
  27. class PongPaddle(Widget):
  28.  
  29. score = NumericProperty(0)
  30.  
  31. def bounce_ball(self, ball):
  32. if self.collide_widget(ball):
  33. vx, vy = ball.velocity
  34. offset = (ball.center_y - self.center_y) / (self.height / 2)
  35. bounced = Vector(-1 * vx,vy)
  36. vel = bounced * 1.1
  37. ball.velocity = vel.x, vel.y + offset
  38. """
  39. klasa przedstawiajaca gre
  40. """
  41. class PongGame(Widget):
  42. ball = ObjectProperty(None)
  43. player1 = ObjectProperty(None)
  44. player2 = ObjectProperty(None)
  45.  
  46. def serve_ball(self, vel = (4,0)):
  47. self.ball.center = self.center
  48. self.ball.velocity = vel
  49.  
  50. def update(self, dt):
  51. self.ball.move()
  52.  
  53. # ruch paletek
  54. self.player1.bounce_ball(self.ball)
  55. self.player2.bounce_ball(self.ball)
  56.  
  57. # ruch pilki gora-dol
  58. if(self.ball.y< self.y) or (self.ball.top > self.top):
  59. self.ball.velocity_y *= -1
  60.  
  61. # zdobycie punktu
  62. if self.ball.x < self.x:
  63. self.player2.score += 1
  64. self.serve_ball(vel=(4, 0))
  65. if self.ball.x > self.width:
  66. self.player1.score += 1
  67. self.serve_ball(vel=(-4, 0))
  68. #poruszanie paletkami przez uzytkownika
  69. def on_touch_move(self, touch):
  70. if touch.x < self.width / 3:
  71. self.player1.center_y = touch.y
  72. if touch.x > self.width - self.width /3:
  73. self.player2.center_y = touch.y
  74.  
  75. """
  76. aplikacja
  77. """
  78. class PongApp(App):
  79. def build(self):
  80. game = PongGame()
  81. game.serve_ball()
  82. Clock.schedule_interval(game.update, 1.0/60.0)
  83. return game
  84. """
  85. uruchomienie gry
  86. """
  87. if __name__ == '__main__':
  88. PongApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement