#Imports
import kivy
from kivy.config import Config
#Set config settings before importing the rest to prevent overwriting
kivy.require('1.1.1')
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '720')
#Resume imports
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.factory import Factory
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.base import runTouchApp
from time import sleep
from random import randint
from time import sleep
global player1Hit
player1Hit = False
global player2Hit
player2Hit = False
#This class is the pongpaddle, instantiated twice... once for player 1 and once for player 2.
class PongPaddle(Widget):
#I creat a variable called justHit, so after the NPC hits the ball, i will create a function to return it to the center
justHit = False
score = NumericProperty(0)
#This function is what changes the velosity to be opposite after it hits the paddle
def bounce_ball(self, ball):
if self.collide_widget(ball):
self.justHit = True
vx, vy = ball.velocity
offset = (ball.center_y-self.center_y)/(self.height/2)
bounced = Vector(-1*vx, vy)
vel = bounced * 1.1
ball.velocity = vel.x, vel.y + offset
#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
class PongBall(Widget):
#Not so sure what this does
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
#This is what controls the ball, without it it won't move
#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
#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?
#I'm probably wrong on this
self.pos = Vector(*self.velocity) + self.pos
#This class controls the class for the actual portion of the game.. should only be instantiated once
class PongGame(Widget):
#create the ball, player1, and player2, inside the .kv file is what instantiates the classes they're related to?
ball = ObjectProperty(None)
player1 = ObjectProperty(None)
player2 = ObjectProperty(None)
#The function to serve the ball, it sets the ball in the center of the screen
#and then it sets the balls velocity to vel (10, 0)
#I'm not exactly sure how self.ball.velocity is defined
def serve_ball(self, vel=(10,0)):
self.ball.center = self.center
self.ball.velocity = vel
def update(self, *args):
self.ball.move()
#bounce off paddles
self.player1.bounce_ball(self.ball)
self.player2.bounce_ball(self.ball)
#bounce ball off bottom or top
if (self.ball.y < self.y) or (self.ball.top > self.top):
self.ball.velocity_y *= -1
#ai - impossible to beat
self.player2.center_y = self.ball.y
#went off to a side to score point?
if self.ball.x < self.x - 40:
self.player2.score += 1
self.serve_ball(vel=(10,0))
if self.ball.x > self.width:
self.player1.score += 1
self.serve_ball(vel=(-10,0))
if self.player1.justHit == True:
print "Player 1 just hit!"
self.player1.justHit = False
if self.player2.justHit == True:
print "Player 2 just hit!"
self.player2.justHit = False
#The function which allows us to hold the player and move it with the mouse
def on_touch_move(self, touch):
if touch.x < self.width/3:
self.player1.center_y = touch.y
#This instantiates the classes as per the .kv file?
Factory.register("PongBall", PongBall)
Factory.register("PongPaddle", PongPaddle)
Factory.register("PongGame", PongGame)
class PongApp(App):
def build(self):
game = PongGame()
game.serve_ball()
#This says update game.update every 1/300 seconds
Clock.schedule_interval(game.update, 1.0/300.0)
return game
if __name__ in ('__android__', '__main__'):
PongApp().run()
-------------------------------------------------------pong.kv file------------------------------------
#:kivy 1.0.9
<PongBall>:
size: 50, 50
canvas:
Ellipse:
source:"ball.png"
pos: self.pos
size: self.size
<PongPaddle>:
size: 25, 200
color: 1, 0.8, 0.3
canvas:
Rectangle:
source:"player1.png"
pos:self.pos
size:self.size
<PongGame>:
ball: pong_ball
player1: player_left
player2: player_right
canvas:
Rectangle:
source:"middle.png"
pos: self.center_x-25, 0
size: 50, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: str(root.player1.score)
Label:
font_size: 70
center_x: root.width * 3 / 4
top: root.top - 50
text: str(root.player2.score)
PongBall:
id: pong_ball
center: self.parent.center
PongPaddle:
id: player_left
x: root.x
center_y: root.center_y
PongPaddle:
id: player_right
x: root.width-self.width
center_y: root.center_y