Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

main.py and pong.kv

By: a guest on Jul 28th, 2012  |  syntax: Python  |  size: 5.75 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #Imports
  2. import kivy
  3. from kivy.config import Config
  4. #Set config settings before importing the rest to prevent overwriting
  5. kivy.require('1.1.1')
  6. Config.set('graphics', 'width', '1280')
  7. Config.set('graphics', 'height', '720')
  8. #Resume imports
  9. from kivy.app import App
  10. from kivy.uix.widget import Widget
  11. from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
  12. from kivy.vector import Vector
  13. from kivy.factory import Factory
  14. from kivy.clock import Clock
  15. from kivy.core.window import Window
  16. from kivy.base import runTouchApp
  17. from time import sleep
  18. from random import randint
  19. from time import sleep
  20. global player1Hit
  21. player1Hit = False
  22. global player2Hit
  23. player2Hit = False
  24.  
  25. #This class is the pongpaddle, instantiated twice... once for player 1 and once for player 2.
  26. class PongPaddle(Widget):
  27.     #I creat a variable called justHit, so after the NPC hits the ball, i will create a function to return it to the center
  28.     justHit = False    
  29.     score = NumericProperty(0)
  30.         #This function is what changes the velosity to be opposite after it hits the paddle
  31.     def bounce_ball(self, ball):
  32.         if self.collide_widget(ball):
  33.             self.justHit = True
  34.             vx, vy = ball.velocity
  35.             offset = (ball.center_y-self.center_y)/(self.height/2)
  36.             bounced = Vector(-1*vx, vy)
  37.             vel = bounced * 1.1
  38.             ball.velocity = vel.x, vel.y + offset
  39.  
  40.                                        
  41.                        
  42.  
  43. #This class is instantiated once and it controls the ball, for multiple balls or a higher difficulty I assume I can instantiate it more times
  44. class PongBall(Widget):
  45. #Not so sure what this does
  46.     velocity_x = NumericProperty(0)
  47.     velocity_y = NumericProperty(0)
  48.     velocity = ReferenceListProperty(velocity_x, velocity_y)
  49.  
  50.     def move(self):
  51.                 #This is what controls the ball, without it it won't move
  52.                
  53.                 #I believe it works by getting the current balls position, getting the velocity assigned (at the start 10,0), and then adding the current pos to the velocity
  54.                 #So if it were 0x to start with, it will get the velocity 10,0, then say 10 + 0 = 0. Now it's at 10x... so 10 x to 10x = 20x... then 20x to 20x = 40x... etc?
  55.                 #I'm probably wrong on this
  56.         self.pos = Vector(*self.velocity) + self.pos
  57.  
  58. #This class controls the class for the actual portion of the game.. should only be instantiated once
  59. class PongGame(Widget):
  60.         #create the ball, player1, and player2, inside the .kv file is what instantiates the classes they're related to?
  61.     ball = ObjectProperty(None)
  62.     player1 = ObjectProperty(None)
  63.     player2 = ObjectProperty(None)
  64.         #The function to serve the ball, it sets the ball in the center of the screen
  65.         #and then it sets the balls velocity to vel (10, 0)
  66.         #I'm not exactly sure how self.ball.velocity is defined
  67.     def serve_ball(self, vel=(10,0)):
  68.         self.ball.center = self.center
  69.         self.ball.velocity = vel
  70.        
  71.  
  72.     def update(self, *args):
  73.         self.ball.move()
  74.  
  75.         #bounce off paddles
  76.         self.player1.bounce_ball(self.ball)
  77.         self.player2.bounce_ball(self.ball)
  78.  
  79.         #bounce ball off bottom or top
  80.         if (self.ball.y < self.y) or (self.ball.top > self.top):
  81.             self.ball.velocity_y *= -1
  82.      
  83.                         #ai - impossible to beat
  84.         self.player2.center_y = self.ball.y                    
  85.  
  86.         #went off to a side to score point?
  87.         if self.ball.x < self.x - 40:
  88.             self.player2.score += 1
  89.             self.serve_ball(vel=(10,0))
  90.         if self.ball.x > self.width:
  91.             self.player1.score += 1
  92.             self.serve_ball(vel=(-10,0))
  93.                        
  94.         if self.player1.justHit == True:
  95.             print "Player 1 just hit!"
  96.             self.player1.justHit = False
  97.         if self.player2.justHit == True:
  98.             print "Player 2 just hit!"
  99.             self.player2.justHit = False
  100.                
  101.         #The function which allows us to hold the player and move it with the mouse
  102.     def on_touch_move(self, touch):
  103.         if touch.x < self.width/3:
  104.             self.player1.center_y = touch.y
  105.  
  106. #This instantiates the classes as per the .kv file?
  107. Factory.register("PongBall", PongBall)
  108. Factory.register("PongPaddle", PongPaddle)
  109. Factory.register("PongGame", PongGame)
  110.  
  111.  
  112. class PongApp(App):
  113.     def build(self):
  114.         game = PongGame()
  115.         game.serve_ball()
  116.                 #This says update game.update every 1/300 seconds
  117.         Clock.schedule_interval(game.update, 1.0/300.0)
  118.         return game
  119.  
  120.  
  121.  
  122. if __name__ in ('__android__', '__main__'):
  123.     PongApp().run()
  124.  
  125.  
  126. -------------------------------------------------------pong.kv file------------------------------------
  127.  
  128.  
  129.  
  130. #:kivy 1.0.9
  131.  
  132. <PongBall>:
  133.     size: 50, 50
  134.     canvas:
  135.         Ellipse:
  136.                         source:"ball.png"
  137.             pos: self.pos
  138.             size: self.size
  139.                                  
  140.  
  141. <PongPaddle>:
  142.     size: 25, 200
  143.         color: 1, 0.8, 0.3
  144.     canvas:
  145.         Rectangle:
  146.                         source:"player1.png"
  147.             pos:self.pos
  148.             size:self.size
  149.                        
  150.                        
  151.                        
  152.  
  153. <PongGame>:
  154.     ball: pong_ball
  155.     player1: player_left
  156.     player2: player_right
  157.    
  158.     canvas:
  159.         Rectangle:
  160.                         source:"middle.png"
  161.             pos: self.center_x-25, 0
  162.             size: 50, self.height
  163.                        
  164.    
  165.     Label:
  166.         font_size: 70  
  167.         center_x: root.width / 4
  168.         top: root.top - 50
  169.         text: str(root.player1.score)
  170.        
  171.     Label:
  172.         font_size: 70  
  173.         center_x: root.width * 3 / 4
  174.         top: root.top - 50
  175.         text: str(root.player2.score)
  176.    
  177.     PongBall:
  178.         id: pong_ball
  179.         center: self.parent.center
  180.        
  181.     PongPaddle:
  182.         id: player_left
  183.         x: root.x
  184.         center_y: root.center_y
  185.        
  186.     PongPaddle:
  187.         id: player_right
  188.         x: root.width-self.width
  189.         center_y: root.center_y
clone this paste RAW Paste Data