Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 7th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import kivy
  2. kivy.require('1.1.1')
  3.  
  4. from kivy.app import App
  5. from kivy.uix.widget import Widget
  6. from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
  7. from kivy.vector import Vector
  8. from kivy.factory import Factory
  9. from kivy.clock import Clock
  10. from kivy.graphics import Color
  11. from kivy.base import EventLoop
  12. from kivy.graphics import *
  13. class PongPaddle(Widget):
  14.     score = NumericProperty(0)
  15.     def build(self):
  16.         print 'builded'
  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.  
  26. class PongBall(Widget):
  27.     velocity_x = NumericProperty(0)
  28.     velocity_y = NumericProperty(0)
  29.     velocity = ReferenceListProperty(velocity_x, velocity_y)
  30.  
  31.     def move(self):
  32.         self.pos = Vector(*self.velocity) + self.pos
  33.  
  34.  
  35. class PongGame(Widget):
  36.     ball = ObjectProperty(None)
  37.     player1 = ObjectProperty(None)
  38.     player2 = ObjectProperty(None)
  39.    
  40.     def serve_ball(self, vel=(4,0)):
  41.         self.ball.center = self.center
  42.         self.ball.velocity = vel
  43.         self.background_color = (255, 255, 0, 0)
  44.     def update(self, *args):
  45.         self.ball.move()
  46.  
  47.         #bounce of paddles
  48.         self.player1.bounce_ball(self.ball)
  49.         self.player2.bounce_ball(self.ball)
  50.  
  51.         #bounce ball off bottom or top
  52.         if (self.ball.y < self.y) or (self.ball.top > self.top):
  53.             self.ball.velocity_y *= -1
  54.  
  55.         #went of to a side to score point?
  56.         if self.ball.x < self.x:
  57.             self.player2.score += 1
  58.             self.serve_ball(vel=(4,0))
  59.         if self.ball.x > self.width:
  60.             self.player1.score += 1
  61.             self.serve_ball(vel=(-4,0))
  62.  
  63.  
  64.     def on_touch_move(self, touch):
  65.         if touch.x < self.width/3:
  66.             self.player1.center_y = touch.y
  67.         if touch.x > self.width - self.width/3:
  68.             self.player2.center_y = touch.y
  69.  
  70.  
  71. Factory.register("PongBall", PongBall)
  72. Factory.register("PongPaddle", PongPaddle)
  73. Factory.register("PongGame", PongGame)
  74.  
  75.  
  76. class PongApp(App):
  77.     def build(self):
  78.         game = PongGame()
  79.         with game.canvas:
  80.             Color(100,0,0, 1)
  81.             Rectangle(pos=(0,0), size=game.size)
  82.         game.serve_ball()
  83.         Clock.schedule_interval(game.update, 1.0/60.0)
  84.         return game
  85.  
  86.  
  87.  
  88. if __name__ == '__main__':
  89.     PongApp().run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top