Little Game

I wrote this little game

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.gui.DirectGui import OnscreenText
from direct.gui.DirectGui import OnscreenImage
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import *
import math,sys

class world(DirectObject):
      def _init_ (self):
            
            #Carico il modello 'ship1'
            self.ship=loader.loadModel('Models/ship1')
            self.ship.reparentTo(render)
            #Carico l'ambiente amb1
            self.amb1=loader.loadModel('Models/amb1')
            self.amb1.reparentTo(render)
            
            #posiziono la camera
            base.disableMouse()
            base.camera.setPosHpr(self.ship.getX()-60,self.ship.getY(),self.ship.getZ()+9,self.ship.getH()+270,0,0)
            
            self.accept('arrow_up',self.setKey,['avanti',1])
            self.accept('arrow_up-up', self.setKey, ['avanti',0])

            taskMgr.add(self.move,"moveTask")
            
      def setKey (self,key,value):
            self.keyMap[key]=value
            
      def move (self,task):
            if (keyMap['avanti']!=0):
                  self.ship.setPos(self.ship.getX()+5,self.ship.getY(),self.ship.getZ())

w=world()
run()

but the result is a blank window.

what is the problem? can you help me please?
[/code]

i’d guess your camera isnt looking at your ship.
you can try to use the lookAt() method to make sure your ship will be in view.

nothing to do!

Now I’m trying to do the same thing without classes.

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.gui.DirectGui import OnscreenText
from direct.gui.DirectGui import OnscreenImage
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import *
import math,sys

#Carico il modello 'ship1'
ship=loader.loadModel('Models/ship1')
ship.reparentTo(render)
#Carico l'ambiente amb1
amb1=loader.loadModel('Models/amb1')
amb1.reparentTo(render)
amb1.setPosHpr(50,4,-5,90,0,0)

#posiziono la camera
base.disableMouse()
base.camera.setPos(ship.getX()-60,ship.getY(),ship.getZ()+9)
base.camera.lookAt(ship)

run()

Is it possible?

last version: blank window!

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.gui.DirectGui import OnscreenText
from direct.gui.DirectGui import OnscreenImage
from direct.task import Task
import math

class world (DirectObject):
	def _init_(self):
		self.keyMap={'avanti':0}

		#Carico il modello 'ship1'
		self.ship=loader.loadModel('Models/ship1')
		self.ship.reparentTo(render)
		#Carico l'ambiente amb1
		self.amb1=loader.loadModel('Models/amb1')
		self.amb1.reparentTo(render)
		self.amb1.setPosHpr(50,4,-5,90,0,0)

		#posiziono la camera
		base.disableMouse()
		base.camera.setPos(self.ship.getX()-60,self.ship.getY(),self.ship.getZ()+9)
		base.camera.lookAt(self.ship)

		self.accept('arrow_up',setkey,['avanti',1])
		taskMgr.add(self.move,'moveTask')
		
	def setkey(self,key,value):
		self.keyMap[key]=value
	
	def move(self,task):
		
		if (keyMap['avanti']!=1) :
			self.ship.setPos(self.ship.getX()+1,self.ship.getY(),self.ship.getZ())
	
w=world()
run()

Why??? :cry:

Are you sure that your ship model is not invisible? And that it’s not too far away or too close?

David

yes i’m sure!

Well, here’s one problem:

   def _init_(self):

You have misspelled the name of the init method (it should have a double leading and trailing underscore, not a single underscore). Your constructor is never getting called!

I discovered this by pasting your code and discovering that w.keyMap, w.ship, and w.amb1 were all undefined, as if they’d never been assigned.

David

Thanks it works!

EDIT:

but the ship doesn’t move why?

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.gui.DirectGui import OnscreenText
from direct.gui.DirectGui import OnscreenImage
from direct.task import Task
import math

class world (DirectObject):
	def __init__(self):
		self.keyMap={'avanti':0}

		#Carico il modello 'ship1'
		self.ship=loader.loadModel('Models/ship1')
		self.ship.reparentTo(render)
		#Carico l'ambiente amb1
		self.amb1=loader.loadModel('Models/amb1')
		self.amb1.reparentTo(render)
		self.amb1.setPosHpr(50,4,-5,90,0,0)

		#posiziono la camera
		base.disableMouse()
		base.camera.setPos(self.ship.getX()-60,self.ship.getY(),self.ship.getZ()+9)
		base.camera.lookAt(self.ship)

		self.accept('arrow_up',self.setkey,['avanti',1])
		taskMgr.add(self.move,'moveTask')
		
	def setkey(self,key,value):
		self.keyMap[key]=value
	
	def move(self,task):
		if (self.keyMap['avanti']!=1) :
			self.ship.setFluidPos(self.ship.getX()+10,self.ship.getY(),self.ship.getZ())
	
w=world()
run()

now it work!

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.gui.DirectGui import OnscreenText
from direct.gui.DirectGui import OnscreenImage
from direct.task import Task
import math

class world (DirectObject):
	def __init__(self):
		self.keyMap={'avanti':0,'rotSX':0,'rotDX':0}
		self.prevtime=0

		#Carico il modello 'ship1'
		self.ship=loader.loadModel('Models/ship1')
		self.ship.reparentTo(render)
		#Carico l'ambiente amb1
		self.amb1=loader.loadModel('Models/amb1')
		self.amb1.reparentTo(render)
		self.amb1.setPosHpr(50,4,-5,90,0,0)

		#posiziono la camera
		base.disableMouse()
		base.camera.setPos(self.ship.getX()-60,self.ship.getY(),self.ship.getZ()+9)
		base.camera.lookAt(self.ship)
		base.camera.reparentTo(self.ship)

		self.accept('arrow_up',self.setkey,['avanti',1])
		self.accept('arrow_up-up',self.setkey,['avanti',0])
		self.accept('arrow_left',self.setkey,['rotSX',1])
		self.accept('arrow_left-up',self.setkey,['rotSX',0])
		self.accept('arrow_right',self.setkey,['rotDX',1])
		self.accept('arrow_right-up',self.setkey,['rotDX',0])
		taskMgr.add(self.move,'moveTask')
		
	def setkey(self,key,value):
		self.keyMap[key]=value
	
	def move(self,task):
		elapsed=task.time-self.prevtime
		if (self.keyMap['avanti']!=0) :
			back=self.ship.getNetTransform().getMat().getRow3(0)
			back.normalize()
			self.ship.setFluidPos(self.ship.getPos()+back*(elapsed*10))
		if (self.keyMap['rotSX']!=0):
			self.ship.setH(self.ship.getH()+elapsed*180)
		if (self.keyMap['rotDX']!=0):
			self.ship.setH(self.ship.getH()-elapsed*180)
		self.prevtime=task.time
		return task.cont
w=world()
run()