just the start i hope.
import direct.directbase.DirectStart
import sys, math, random
from direct.showbase.DirectObject import DirectObject
from direct.gui.DirectGui import *
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from direct.task.Task import Task
def addTitle(text):
return OnscreenText(text=text, style=1, fg=(1,1,1,1),
pos=(1.0,0.8), align=TextNode.ARight, scale = .07)
def addInstructions(pos, msg):
return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
pos=(-1.3, pos), align=TextNode.ALeft, scale = .06)
def addScore(pos, msg):
return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
pos=(-1.3, pos), align=TextNode.ALeft, scale = .06, mayChange = 1)
class World (DirectObject):
def __init__(self):
base.win.setClearColor(Vec4(0,0,0,1))
base.setBackgroundColor(0,0,0,1)
# Post the instructions
self.title = addTitle("Ping")
self.inst1 = addInstructions(0.85, "[ESC]: Quit")
self.inst2 = addInstructions(0.75, "[W] Move the paddle up")
self.inst3 = addInstructions(0.69, "[S] Move the paddle down")
self.inst4 = addInstructions(0.62, "[Space] start")
self.score1= addScore(-0.7, "Score:")
self.score2= addScore(-0.8, "")
#the court is going to be from -40 to 40 for x, -15 to 15 for z, y =0
#choosen based on camera distance and trial and error
self.northwall = 15
self.southwall = -15
self.westwall = -40
self.eastwall = 40
#load the ball and paddle
self.ball = loader.loadModel('ball')
self.ball.reparentTo(render)
self.ball.setPos(0,0,0) #center court
self.ball.setColor(1,0,0,1)
self.paddle = loader.loadModel('paddle')
self.paddle.reparentTo(render)
self.paddle.setPos(-30,0,0)
self.paddle.setColor(0,0,1,1)
#make an ambient light
self.alight = AmbientLight('alight')
self.alight.setColor(VBase4(0.6,0.6,0.6, 1))
self.alnp = render.attachNewNode(self.alight)
render.setLight(self.alnp)
#set the 'camera'
base.disableMouse()
self.camDist =120
self.camHeight = 0
base.camera.setPos(0,-self.camDist,self.camHeight)
#set up the keyboard controls
self.accept("w", self.moveUpF, [1])
self.accept("w-up", self.moveUpF, [-1])
self.accept("s", self.moveDownF, [1])
self.accept("s-up", self.moveDownF, [-1])
self.accept("space", self.startBallF)
self.accept("escape", sys.exit)
#variables
self.startCount = 0
self.ballVmotion = random.uniform(0.1,0.4) * 0.3
self.ballHmotion = random.uniform(0.1,1.5) * 0.3
def startBallF(self):
if self.startCount ==0:
taskMgr.add(self.moveBallT, "moveBallT")
self.score = 0
self.score2 ['text'] = str(self.score)
self.startCount = 1 #so you don't start a new game in the middle of a game
def moveBallT(self,task):
#move the ball
self.ball.setX(self.ball.getX()+self.ballHmotion)
self.ball.setZ(self.ball.getZ()+self.ballVmotion)
#end the game if the paddle misses the ball
if self.ball.getX() < self.westwall:
self.ball.setPos(0,0,0)
self.startCount =0
return Task.done
#if the ball is located within the paddle area (4X1) , make the ball bounce off the paddle
if self.ball.getX() < (self.paddle.getX() + 0.5) and self.ball.getX() > (self.paddle.getX() - 0.5) and self.ball.getZ() < (self.paddle.getZ()+2) and self.ball.getZ() > (self.paddle.getZ()-2):
self.score += 1
print self.score
self.score2 ['text'] = str(self.score)
self.ballHmotion = self.ballHmotion * -1
self.ballVmotion = self.ballVmotion * -1
return Task.cont
if self.ball.getZ() > self.northwall:
self.ballVmotion = self.ballVmotion * -1
return Task.cont
if self.ball.getZ() < self.southwall:
self.ballVmotion = self.ballVmotion * -1
return Task.cont
if self.ball.getX() > self.eastwall:
self.ballHmotion = self.ballHmotion * -1
return Task.cont
else:
self.ballVmotion = self.ballVmotion
self.ballHmotion = self.ballHmotion
return Task.cont
#paddle motion
def moveUpF (self,arg):
if arg==1 and self.paddle.getZ() <= self.northwall:
taskMgr.add(self.moveUpT, "moveUpT")
else:
taskMgr.remove("moveUpT")
def moveUpT (self,task):
if self.paddle.getZ() <= self.northwall:
self.paddle.setZ(self.paddle.getZ() + 0.5) #0.5 choosen by trial and error
return Task.cont
def moveDownF (self,arg):
if arg==1 and self.paddle.getZ() >= self.southwall:
taskMgr.add(self.moveDownT, "moveDownT")
else:
taskMgr.remove("moveDownT")
def moveDownT (self,task):
if self.paddle.getZ() >= self.southwall:
self.paddle.setZ(self.paddle.getZ() - 0.5) #0.5 choosen by trial and error
return Task.cont
w=World()
run()