Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.app import App
- from kivy.uix.widget import Widget
- from kivy.properties import NumericProperty, ReferenceListProperty,\
- ObjectProperty, ListProperty
- from kivy.vector import Vector
- from kivy.clock import Clock
- from random import randint
- from kivy.factory import Factory
- class PongGame(Widget):
- def PongBall(Widget):
- velocity_x = NumericProperty(0)
- velocity_y = NumericProperty(0)
- velocity = ReferenceListProperty(velocity_x, velocity_y)
- with self.canvas:
- Color(0,1,1,1)
- self.size=(50, 5)
- self.elip = Ellipse(pos=self.pos, size=self.size)
- balls = ListProperty()
- def move(self):
- self.pos = Vector(*self.velocity) + self.pos
- def add_new_ball(self):
- ball = Factory.PongBall()
- ball.center = self.center
- ball.velocity = Vector(2, 0).rotate(randint(-45, 45))
- self.add_widget(ball)
- self.balls.append(ball)
- def update(self, dt):
- for ball in self.balls:
- self.ball.move()
- #bounce off top and bottom
- if (self.ball.y < 0) or (self.ball.top > self.height):
- self.ball.velocity_y *= -1.00
- #bounce off left and right
- if (self.ball.x < 0) or (self.ball.right > self.width):
- self.ball.velocity_x *= -1.00
- class PongApp(App):
- def build(self):
- game = PongGame()
- game.add_new_ball()
- Clock.schedule_interval(game.update, 1.0 / 60.0)
- return game
- if __name__ == '__main__':
- PongApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement