War game Prototype not working?

OK, I am trying to make a war game, and the movements do not work. There are no errors or anything, just it doesn’t do what I want it to. It is probably my fault for not paying enough attention. Please do not tell me to go and read the tutorials, as I already have. My cousin, Xboxjoshone helped me with this code. The code is located at jandestuff.com/safe/client.py

Perhaps you should better explain what a ‘war game’ is. That is kinda vague…

Xbox, you know what I mean, and they will know what I mean when they see the code. I am trying to make an FPS-like game.

It’s kind of hard to test it without the game assets. What is it you want it to do, and what is happening instead?
Need more info.

I want the camera to follow the gun, and to turn with the gun. I’m not sure what it is doing instead.

I think deps means that he wants the models + textures.

Oh, okay xbox. jandestuff.com/safe/models.zip

Now that I knew that you wanted the camera to follow the gun, I knew what to look for. And with some models I had something to look at. :slight_smile:

Not really sure what you are trying to do in the source. It seems you do some funky math and rotates the models so they doesn’t line up with how Panda usually do things (X is left-right, Y if forward-backward, and Z is up-down)

I parented the camera to the weapon and had to fiddle around with the values to get everything into view. Is this what you are after?

import direct.directbase.DirectStart
from direct.task import Task
from direct.showbase.DirectObject import DirectObject
from time import sleep
import math
#Create floater for cam to lookat because of problems
#make camera follow gun
#make bullets work
class World(DirectObject):
  def __init__(self):
    self.prevtime = 0
    self.keys = {"forward":0,"back":0,"left":0,"right":0,"shoot":0}
    self.ammo = ammo = 100
    self.weapon = loader.loadModel("mp5.x")
    self.weapon.setPos(0, 0, 50)
    self.weapon.setHpr(0, 0, -90)
    self.weapon.reparentTo(render)
    self.weapon.setScale(0.25, 0.25, 0.25)
    self.dummy = self.weapon.attachNewNode('dummyNode')
    self.dummy.setZ(156.3)

    self.ground = loader.loadModel("dirt.egg")
    self.ground.setPos(0, 0, 0)
    self.ground.setHpr(0, 0, -90)
    self.ground.reparentTo(render)
    self.ground.setScale(500, 500, 500)
    self.groundtex = loader.loadTexture("dirt.bmp")
    self.ground.setTexture(self.groundtex)

    base.disableMouse()
    base.setBackgroundColor(1, 1, 1)
    #base.camera.setPos(-1.4, 17, 56.3)
    #base.camera.setHpr(180, 0, 0)
    base.camera.reparentTo( self.weapon )
    base.camera.setPos(100,100,0)
    base.camera.setHpr(-25,180,90)

    #self.accept("arrow_left", self.left, [self.weapon])
    self.accept("arrow_left", self.setKey, ["left",1])
    self.accept("arrow_right", self.setKey, ["right",1])
    self.accept("arrow_up", self.setKey, ["forward",1])
    self.accept("arrow_down", self.setKey, ["back",1])
    self.accept("arrow_left-up", self.setKey, ["left",0])
    self.accept("arrow_right-up", self.setKey, ["right",0])
    self.accept("arrow_up-up", self.setKey, ["forward",0])
    self.accept("arrow_down-up", self.setKey, ["back",0])
    self.accept("mouse1",self.setKey, ["shoot",1])
    self.accept("mouse1-up",self.setKey, ["shoot",0])
    taskMgr.add(self.mainLoop, 'world main loop')
    #base.camera.lookAt(self.dummy)

  def setKey(self, key, se):
    try:
      self.keys[key] = se
      return True
    except:
      return False
  def mainLoop(self,task):
    # DO THIS ALL DAY LONG
    elapsed = task.time - self.prevtime
    if self.keys['forward'] == 1:
      backward = self.weapon.getNetTransform().getMat().getRow3(1)
      backward.setZ(0)
      backward.normalize()
      self.weapon.setPos(self.weapon.getPos() - backward*(elapsed*15))
      #base.camera.setPos(self.weapon.getPos())
      #p = base.camera.getNetTransform().getMat().getRow3(1)
      #p.normalize()
      #base.camera.setPos(base.camera.getPos() + p*(elapsed*45))
      #base.camera.setZ(56.3)
      #base.camera.lookAt(self.dummy)
    if self.keys['back'] == 1:
      backward = self.weapon.getNetTransform().getMat().getRow3(1)
      backward.setZ(0)
      backward.normalize()
      self.weapon.setPos(self.weapon.getPos() + backward*(elapsed*15))
      #base.camera.setPos(self.weapon.getPos())
      #p = base.camera.getNetTransform().getMat().getRow3(1)
      #p.normalize()
      #base.camera.setPos(base.camera.getPos() - p*(elapsed*15))
      #base.camera.setZ(56.3)
      #base.camera.lookAt(self.dummy)
    if self.keys['left'] == 1:
      self.weapon.setH(self.weapon.getH()+1)
      
      #angledegrees = elapsed*6.0
      #angleradians = angledegrees * (math.pi / 180.0)
      #base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),base.camera.getZ())
      #base.camera.setHpr(angledegrees, 0, 0)

    if self.keys['right'] == 1:
      self.weapon.setH(self.weapon.getH()-1)
      
      #angledegrees = elapsed*(-6.0)
      #angleradians = angledegrees * (math.pi / 180.0)
      #base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),base.camera.getZ())
      #base.camera.setHpr(angledegrees, 0, 0)
    if self.keys['shoot'] == 1:
      self.shoot(self.weapon)
    self.prevtime = task.time
    return Task.cont

  def shoot(self, obj): #add Rate of Fire (sleep)
    if self.ammo > 0:
      self.bullet = loader.loadModel("models/Created/bullet.egg")
      self.bullet.setPos(obj.getPos())
      self.bullet.setHpr(self.weapon.getHpr())
      self.bullet.reparentTo(render)
      self.bullet.setScale(10, 10, 10)
      self.bullettex = loader.loadTexture("models/Created/bullet.bmp")
      self.bullet.setTexture(self.bullettex)
      self.ammo -= 1
    else:
      print "Out of ammo"
    print obj.getHpr()
w = World()
run()

(I had to remove the paths in the model loading bit, since the folders wasn’t in the zip)

I think there is some FPS code here somewhere, have you looked at them yet? (I haven’t so I don’t know if they differ from what you are trying to do here)

Thank you so much! After some tweaking, it works exactly how I want it to! However, I still can’t get the bullets to work and I don’t quite understand the code that you made.

I’ve made tweaks and am uploading version 0.1 of Wargames.

http://new.jandestuff.com/safe/wargames.zip

P.S. The website will be available soon!