Okay, so the OdeWorld is a seperate class? I haven’t been programming using OOP so far so I am not sure if I already have some of that in my main class.
This is my code as of current.
from direct.showbase.ShowBaseGlobal import globalClock
from panda3d.core import loadPrcFileData
from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from panda3d.core import DirectionalLight
from panda3d.core import CollisionTraverser
from panda3d.core import CollisionHandlerPusher
from panda3d.core import CollisionSphere, CollisionNode, CollisionCapsule, CollisionPlane
from panda3d.core import Vec3, Point3
from direct.gui.DirectGui import *
from panda3d.ai import *
from panda3d.ode import OdeBody, OdeMass, OdeWorld
configVars = """
window-title My Game
win-size 880 500
show-frame-rate-meter True
cursor-hidden False
show-scene-graph-analyzer-meter True
sync-video 1
"""
loadPrcFileData("", configVars)
keyMap = {
"w": False,
"s": False,
"a": False,
"d": False,
"arrowleft": False,
"arrowright": False,
"p": False,
"space": False
}
# callback function to update the keymap
def updateKeyMap(key, state):
keyMap[key] = state
MyGame = OdeWorld
MyGame.setGravity(0, 0, -9.81)
class MyGame(ShowBase):
def __init__(self):
super().__init__()
# Only disables the default camera control
self.disableMouse()
self.render.setShaderAuto()
# setting some basic constants
self.x = 0
self.z = 0
self.y = 0
self.speed = 20
# creating a new nodepath for the light, rendering it and positioning it
self.mainlight = DirectionalLight("mainlight")
self.mainlight.setColor((10, 10, 10, 10))
self.mainLightNodePath = self.render.attachNewNode(self.mainlight)
# Turn it around by 45 degrees, and tilt it down by 45 degrees
self.mainLightNodePath.setHpr(-55, -65, 30)
self.render.setLight(self.mainLightNodePath)
self.set_background_color(0.6, 0.6, 1.0, 1.0)
# loads the wolf model, sets it's position and scales it down.
self.wolf = Actor("my-models/wolf")
self.wolf.setScale(0.8, 0.8, 0.8)
self.wolf.setPos(0, 0, 0)
self.wolf.setHpr(0, 0, 0)
self.wolf.reparentTo(self.render)
self.player = Actor("my-models/sphere")
self.player.setScale(0.8, 0.8, 0.8)
self.player.setPos(0, -30, -1.2)
self.player.setHpr(0, 0, 0)
self.player.reparentTo(self.render)
# set camera back on the y axis
#self.cam.setPos(0, -30, 0)
#self.cam.setHpr(0, -3, 0)
# the keyboard controls for when the buttons are pressed and released
self.accept("a", updateKeyMap, ["a", True])
self.accept("a-up", updateKeyMap, ["a", False])
self.accept("d", updateKeyMap, ["d", True])
self.accept("d-up", updateKeyMap, ["d", False])
self.accept("w", updateKeyMap, ["w", True])
self.accept("w-up", updateKeyMap, ["w", False])
self.accept("s", updateKeyMap, ["s", True])
self.accept("s-up", updateKeyMap, ["s", False])
self.accept("p", updateKeyMap, ["p", True])
self.accept("p-up", updateKeyMap, ["p", True])
self.accept("arrowright", updateKeyMap, ["arrowright", True])
self.accept("arrowright-up", updateKeyMap, ["arrowright", False])
self.accept("arrowleft", updateKeyMap, ["arrowleft", True])
self.accept("arrowleft-up", updateKeyMap, ["arrowleft", False])
self.accept("space", updateKeyMap, ["space", True])
self.accept("space-up", updateKeyMap, ["space", False])
# initialising the basic collisions
self.cTrav = CollisionTraverser()
self.pusher = CollisionHandlerPusher()
# creating a node for an active collision for the player
playerNode = CollisionNode("player")
# Add a collision-sphere centred on (0, 0, 0), and with a radius of 0.3
playerNode.addSolid(CollisionCapsule(0, 0, 0, 0, 0, 40, 3.5))
player = self.cam.attachNewNode(playerNode)
self.pusher.addCollider(player, self.player)
self.cTrav.addCollider(player, self.pusher)
player.show()
# creating a node for an active collision for the wolf
wolfNode = CollisionNode("wolf")
# Add a collision-sphere centred on (0, 0, 0), and with a radius of 0.3
wolfNode.addSolid(CollisionCapsule(0, 0, 0, 0, 0, 40, 3.5))
wolf = self.wolf.attachNewNode(wolfNode)
self.pusher.addCollider(wolf, self.wolf)
self.cTrav.addCollider(wolf, self.pusher)
# if this is unhashed, it shows the collision boundaries around each active collision
# wolf.show()
# The GUI
# The title menu is initialised with the size of its box
self.titleMenu = DirectDialog(frameSize=(-0.7, 0.7, -0.7, 0.7),
fadeScreen=0.4,
relief=DGG.GROOVE)
# The game name is given properties
title = DirectLabel(text="My Game",
scale=0.25,
pos=(0, 0, 0.4),
parent=self.titleMenu)
# A button is initialised for starting the game, if pressed it will run the method 'startGame'
btn = DirectButton(text="Start Game",
# command=self.startGame,
pos=(0, 0, 0),
parent=self.titleMenu,
scale=0.1)
# A button is initialised for quitting the game, if pressed it will run the method for 'quit'
btn = DirectButton(text="Quit",
# command=self.quit,
pos=(0, 0, -0.3),
parent=self.titleMenu,
scale=0.1)
# The paused menu is initialised with the size of its box
self.paused = DirectDialog(frameSize=(-0.9, 0.9, -0.7, 0.7),
fadeScreen=0.4,
relief=DGG.GROOVE)
# The title of the paused screen is given its properties
title = DirectLabel(text="Paused",
scale=0.2,
pos=(0, 0, 0.4),
parent=self.paused)
# A button is initialised for continuing the game, if pressed it will run the method 'continue'
btn = DirectButton(text="Continue",
# command=self.continue,
pos=(0, 0, 0.1),
parent=self.paused,
scale=0.1)
# A button is initialised for quitting the game, if pressed it will run the method 'quit'
btn = DirectButton(text="Quit",
# command=self.quit,
pos=(0, 0, -0.15),
parent=self.paused,
scale=0.1)
# A title for inputting the volume percentage
title = DirectLabel(text="Volume (0-100)%",
scale=0.07,
pos=(-0.2, 0, -0.4),
parent=self.paused)
# A form is initialised to input the percentage audio volume
entry = DirectEntry(text="",
scale=0.07,
numLines=1,
parent=self.paused,
pos=(0.15, 0, -0.4),
frameColor=(0, 0, 0, 0))
# The game over menu is initialised with the size of its box
self.gameOverScreen = DirectDialog(frameSize=(-0.7, 0.7, -0.7, 0.7),
fadeScreen=0.4,
relief=DGG.GROOVE)
# The title is given its properties
label = DirectLabel(text="Game Over!",
parent=self.gameOverScreen,
scale=0.2,
pos=(0, 0, 0.3))
# ?
self.finalScoreLabel = DirectLabel(text="",
parent=self.gameOverScreen,
scale=0.09,
pos=(0, 0, 0))
# A button is initialised to start the game, if pressed it will run the method 'startGame'
btn = DirectButton(text="Play again!",
# command=self.startGame,
pos=(-0.3, 0, -0.3),
parent=self.gameOverScreen,
scale=0.09)
# A button is initialised to quit the game, if pressed it will run the method 'quit'
btn = DirectButton(text="Quit",
# command=self.quit,
pos=(0.3, 0, -0.31),
parent=self.gameOverScreen,
scale=0.1)
self.AIworld = AIWorld(self.render)
self.AIchar = AICharacter("pursuer", self.wolf, 100, 0.05, 5)
self.AIworld.addAiChar(self.AIchar)
self.AIbehaviors = self.AIchar.getAiBehaviors()
self.AIbehaviors.pursue(self.player)
# AI World update
self.taskMgr.add(self.AIUpdate, "AIUpdate")
self.taskMgr.add(self.update, "update")
# to update the AIWorld
def AIUpdate(self, task):
self.AIworld.update()
return task.cont
def update(self, task):
dt = globalClock.getDt()
print(dt)
startPos = self.player.getPos()
posCam = self.cam.getPos()
self.angle = 0
# controlling the camera using key bindings
pos = self.cam.getPos()
if keyMap["a"]:
startPos.x -= self.speed * dt
posCam.x -= self.speed * dt
self.player.setHpr(-90, 0, 0)
if keyMap["d"]:
startPos.x += self.speed * dt
posCam.x += self.speed * dt
self.player.setHpr(90, 0, 0)
if keyMap["w"]:
startPos.y += self.speed * dt
posCam.y += self.speed * dt
self.player.setHpr(0, 0, 0)
if keyMap["s"]:
startPos.y -= self.speed * dt
posCam.y -= self.speed * dt
self.player.setHpr(180, 0, 0)
if keyMap["space"]:
startPos.z += self.speed * dt
posCam.z += self.speed * dt
self.player.setHpr(0, 0, 0)
self.player.setPos(startPos)
self.cam.setPos(posCam)
# prints the mouse pointer coords every second
md1 = self.win.getPointer(0)
print(md1.getX(), md1.getY())
mouseX = md1.getX() * dt
mouseY = md1.getY() * dt
self.player.setHpr(mouseX, 0, 0)
# md = self.win.getPointer(0)
# heading = 0
# pitch = 0
# x = md.getX()
# y = md.getY()
# heading += y
# pitch += x
# self.cam.setHpr(heading, pitch, 0)
# Hides all of the screens to begin with
self.paused.hide()
self.titleMenu.hide()
self.gameOverScreen.hide()
# If the user presses space key, it will bring the paused interface up
if keyMap["p"]:
self.paused.show()
self.gameOverScreen.hide()
self.finalScoreLabel["text"] = "Achievements: "
self.finalScoreLabel.setText()
return task.cont
game = MyGame()
game.run()