Roaming Ralph, now with joypad (using pygame)

Since the joypad manual is incomplete/inaccurate, here we have the Roaming Ralph code changed to support joypad. I wrote it with a playstation 2/3/USB control, I tried my best to code it in a way it should work with any joypad with two or more buttons and a D-pad. If you have no joypad connected it will warn just run the normal example (with keyboard).
I also just tested with a very old clone 8 button gamepad, just fixed a bug. Please, test with your different gamepads and let me know if there is any bugs. It should be helpful for all.

Any feedbacks are welcome :smiley: .

PS: You need pygame installed.
PS 2: Sorry if the text is a little confusing, it is pretty late, I will edit it tomorrow as needed.


# Original file

# Author: Ryan Myers
# Models: Jeff Styers, Reagan Heller

# Last Updated: 6/13/2005
#
# This tutorial provides an example of creating a character
# and having it walk around on uneven terrain, as well
# as implementing a fully rotatable camera.

# Roaming Ralph with pygame joypad
# Changes by: Fernando Coelho
# This tutorial should teach you how to use a joypad in Roaming Ralph.
# It is highly recomended that you already understood how roaming ralph example
# works, otherwise you will get very confused about the controls.
# Before any new information there will be an (New joypad information) title
# This guide was written and tested by using a playstation joypad, it SHOULD
# work with any other joypad, please let me know if you tested with another
# and found problems. 


import direct.directbase.DirectStart
from pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from pandac.PandaModules import Filename
from pandac.PandaModules import PandaNode,NodePath,Camera,TextNode
from pandac.PandaModules import Vec3,Vec4,BitMask32
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.task.Task import Task
from direct.showbase.DirectObject import DirectObject
import random, sys, os, math

#(New joypad information):
# New imports needed by pygame
import pygame
from pygame.locals import *

SPEED = 0.5

# Figure out what directory this program is in.
MYDIR=os.path.abspath(sys.path[0])
MYDIR=Filename.fromOsSpecific(MYDIR).getFullpath()

font = loader.loadFont("cmss12")

# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1), font = font,
                        pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1), font = font,
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

class World(DirectObject):

	def __init__(self):
        
		self.keyMap = {"left":0, "right":0, "forward":0, "cam-left":0, "cam-right":0}
		base.win.setClearColor(Vec4(0,0,0,1))

        # Post the instructions

		# (New joypad information):
		# Try to load the joystick, if no joypad is found or if there is any errors,
		# it will load as classic roaming ralph
		pygame.init()
		self.jsWorks = False
		pygame.joystick.init()
		print "Joypad init: ", pygame.joystick.get_init()
		print "Joypad count: ", pygame.joystick.get_count()
		if (pygame.joystick.get_init() == True and pygame.joystick.get_count() >= 1):
			self.joystick = pygame.joystick.Joystick(0)
			self.joystick.init()
			if (self.joystick.get_init() == True):
				self.jsWorks = True
			else:
				print "Error trying to load the joypad."

		if (self.jsWorks == True):
			self.title = addTitle("Panda3D Tutorial: Roaming Ralph (With joypad)")
			self.inst1 = addInstructions(0.95, "[ESC]: Quit")
			self.inst2 = addInstructions(0.90, "[Left]: Rotate Ralph Left")
			self.inst3 = addInstructions(0.85, "[Right]: Rotate Ralph Right")
			self.inst4 = addInstructions(0.80, "[Up]: Run Ralph Forward")
			self.inst6 = addInstructions(0.70, "[Button 1]: Rotate Camera Left")
			self.inst7 = addInstructions(0.65, "[Button 2]: Rotate Camera Right")
			
		else:			
			self.title = addTitle("Panda3D Tutorial: Roaming Ralph (Walking on Uneven Terrain)")
			self.inst1 = addInstructions(0.95, "[ESC]: Quit")
			self.inst2 = addInstructions(0.90, "[Left Arrow]: Rotate Ralph Left")
			self.inst3 = addInstructions(0.85, "[Right Arrow]: Rotate Ralph Right")
			self.inst4 = addInstructions(0.80, "[Up Arrow]: Run Ralph Forward")
			self.inst6 = addInstructions(0.70, "[A]: Rotate Camera Left")
			self.inst7 = addInstructions(0.65, "[S]: Rotate Camera Right")
			
        # Set up the environment
        #
        # This environment model contains collision meshes.  If you look
        # in the egg file, you will see the following:
        #
        #    <Collide> { Polyset keep descend }
        #
        # This tag causes the following mesh to be converted to a collision
        # mesh -- a mesh which is optimized for collision, not rendering.
        # It also keeps the original mesh, so there are now two copies ---
        # one optimized for rendering, one for collisions.  

		self.environ = loader.loadModel("models/world")      
		self.environ.reparentTo(render)
		self.environ.setPos(0,0,0)
        
        # Create the main character, Ralph

		ralphStartPos = self.environ.find("**/start_point").getPos()
		self.ralph = Actor("models/ralph",
								{"run":"models/ralph-run",
                                 "walk":"models/ralph-walk"})
		self.ralph.reparentTo(render)
		self.ralph.setScale(.2)
		self.ralph.setPos(ralphStartPos)

        # Create a floater object.  We use the "floater" as a temporary
        # variable in a variety of calculations.
        
		self.floater = NodePath(PandaNode("floater"))
		self.floater.reparentTo(render)	

		# (New joypad information):
		# Configure only the ESC the joypad keys will be handled in a different function,
		# if the joypad was not loaded successfully, it will use the keyboard instead.
		# We need to finish the joypad info before leaving, so let's creat a new function.
		if (self.jsWorks == True):
			self.accept("escape", self.finishJoystick)
		
		# Accept the control keys for movement and rotation
		else:
			self.accept("escape", sys.exit)
			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("a", self.setKey, ["cam-left",1])
			self.accept("s", self.setKey, ["cam-right",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("a-up", self.setKey, ["cam-left",0])
			self.accept("s-up", self.setKey, ["cam-right",0])

		taskMgr.add(self.move,"moveTask")

		# Game state variables
		self.isMoving = False

		# Set up the camera
		
		base.disableMouse()
		base.camera.setPos(self.ralph.getX(),self.ralph.getY()+10,2)
		
		# We will detect the height of the terrain by creating a collision
		# ray and casting it downward toward the terrain.  One ray will
		# start above ralph's head, and the other will start above the camera.
		# A ray may hit the terrain, or it may hit a rock or a tree.  If it
		# hits the terrain, we can detect the height.  If it hits anything
		# else, we rule that the move is illegal.

		self.cTrav = CollisionTraverser()

		self.ralphGroundRay = CollisionRay()
		self.ralphGroundRay.setOrigin(0,0,1000)
		self.ralphGroundRay.setDirection(0,0,-1)
		self.ralphGroundCol = CollisionNode('ralphRay')
		self.ralphGroundCol.addSolid(self.ralphGroundRay)
		self.ralphGroundCol.setFromCollideMask(BitMask32.bit(0))
		self.ralphGroundCol.setIntoCollideMask(BitMask32.allOff())
		self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
		self.ralphGroundHandler = CollisionHandlerQueue()
		self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

		self.camGroundRay = CollisionRay()
		self.camGroundRay.setOrigin(0,0,1000)
		self.camGroundRay.setDirection(0,0,-1)
		self.camGroundCol = CollisionNode('camRay')
		self.camGroundCol.addSolid(self.camGroundRay)
		self.camGroundCol.setFromCollideMask(BitMask32.bit(0))
		self.camGroundCol.setIntoCollideMask(BitMask32.allOff())
		self.camGroundColNp = base.camera.attachNewNode(self.camGroundCol)
		self.camGroundHandler = CollisionHandlerQueue()
		self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)

		# Uncomment this line to see the collision rays
		#self.ralphGroundColNp.show()
		#self.camGroundColNp.show()
	   
		#Uncomment this line to show a visual representation of the 
		#collisions occuring
		#self.cTrav.showCollisions(render)
	   

	
	#Records the state of the arrow keys
	def setKey(self, key, value):
		self.keyMap[key] = value

	# (New joypad information):
	# This function will handle the joypad,  everything here is joypad related code.
	def handleJoypad(self):	
		self.hat_x = 0
		self.hat_y = 0
		# controls the joypad directionals, the return value is x, y.
		# x = -1 means left.
		# x = 1 means right.
		# y = -1 means down.
		# y = 1 means up.
		# Only those values and 0 are returned.
		if (self.joystick.get_numhats() >= 1):
			hat = self.joystick.get_hat(0)
			self.hat_x = hat[0]
			self.hat_y = hat[1]
			
			#controls left and right
			if (self.hat_x == -1):
				self.setKey ("left",1)
			elif (self.hat_x == 1):
				self.setKey ("right",1)
			else:
				self.setKey ("left",0)
				self.setKey ("right",0)
			
			#controls up and down
			if (self.hat_y == 1):
				self.setKey ("forward",1)
			elif (self.hat_y == 0):
				self.setKey ("forward",0)
	
		# controls the joypad axis, remember that this was coded using a playstation II/III joypad.
		# For every even axis this loop controls left and right
		# For every odd axis this loop controls forward
		# 0.25 value is to enable a deadzone, the return value is always a float between -1.0 and 1.0.
		if (self.joystick.get_numaxes() != 0 and self.hat_x == 0 and self.hat_y == 0):
			self.goFoward = False
			self.goLeft = False
			self.goRight = False
			for i in range(self.joystick.get_numaxes()/2):
				left_right_axes = self.joystick.get_axis(i*2)
				if(left_right_axes <= -0.25):
					self.goLeft = True
				elif (left_right_axes >= 0.25):
					self.goRight = True
				
				up_down_axes = self.joystick.get_axis((i*2)+1)
				if (up_down_axes <= -0.25 and self.goFoward == False):
					self.goFoward = True

			# Define if Ralph should go foward
			if (self.goFoward == True):
				self.setKey ("forward",1)
			else:
				self.setKey ("forward",0)
			
			# Define if Ralph should turn around
			if(self.goLeft == True):
				self.setKey ("left",1)
			elif (self.goRight == True):
				self.setKey ("right",1)
			else: 
				self.setKey ("left",0)
				self.setKey ("right",0)
			
		if (self.joystick.get_button(0) == True):
			self.setKey("cam-left",1)
		else:
			self.setKey("cam-left",0)
		
		if (self.joystick.get_button(1) == True):
			self.setKey("cam-right",1)
		else:
			self.setKey("cam-right",0)
				
		#This loop will only make the events be processed.
		for event in pygame.event.get():
			True 

			
			
	def finishJoystick (self):
		self.joystick.quit()
		pygame.joystick.quit()
		sys.exit()
			
	# Accepts arrow keys to move either the player or the menu cursor,
	# Also deals with grid checking and collision detection
	def move(self, task):

		# (New joypad information):
		# Call the function that will handle the joypad stuff.
		if(self.jsWorks == True):
			self.handleJoypad()	

		# Get the time elapsed since last frame. We need this
		# for framerate-independent movement.
		elapsed = globalClock.getDt()

		# If the camera-left key is pressed, move camera left.
		# If the camera-right key is pressed, move camera right.

		base.camera.lookAt(self.ralph)
		if (self.keyMap["cam-left"]!=0):
			base.camera.setX(base.camera, -(elapsed*20))
		if (self.keyMap["cam-right"]!=0):
			base.camera.setX(base.camera, +(elapsed*20))

		# save ralph's initial position so that we can restore it,
		# in case he falls off the map or runs into something.

		startpos = self.ralph.getPos()

		# If a move-key is pressed, move ralph in the specified direction.

		if (self.keyMap["left"]!=0):
			self.ralph.setH(self.ralph.getH() + elapsed*300)
		if (self.keyMap["right"]!=0):
			self.ralph.setH(self.ralph.getH() - elapsed*300)
		if (self.keyMap["forward"]!=0):
			self.ralph.setY(self.ralph, -(elapsed*25))

		# If ralph is moving, loop the run animation.
		# If he is standing still, stop the animation.

		if (self.keyMap["forward"]!=0) or (self.keyMap["left"]!=0) or (self.keyMap["right"]!=0):
			if self.isMoving is False:
				self.ralph.loop("run")
				self.isMoving = True
		else:
			if self.isMoving:
				self.ralph.stop()
				self.ralph.pose("walk",5)
				self.isMoving = False

		# If the camera is too far from ralph, move it closer.
		# If the camera is too close to ralph, move it farther.

		camvec = self.ralph.getPos() - base.camera.getPos()
		camvec.setZ(0)
		camdist = camvec.length()
		camvec.normalize()
		if (camdist > 10.0):
			base.camera.setPos(base.camera.getPos() + camvec*(camdist-10))
			camdist = 10.0
		if (camdist < 5.0):
			base.camera.setPos(base.camera.getPos() - camvec*(5-camdist))
			camdist = 5.0

		# Now check for collisions.

		self.cTrav.traverse(render)

		# Adjust ralph's Z coordinate.  If ralph's ray hit terrain,
		# update his Z. If it hit anything else, or didn't hit anything, put
		# him back where he was last frame.

		entries = []
		for i in range(self.ralphGroundHandler.getNumEntries()):
			entry = self.ralphGroundHandler.getEntry(i)
			entries.append(entry)
		entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
									 x.getSurfacePoint(render).getZ()))
		if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):
			self.ralph.setZ(entries[0].getSurfacePoint(render).getZ())
		else:
			self.ralph.setPos(startpos)

		# Keep the camera at one foot above the terrain,
		# or two feet above ralph, whichever is greater.
		
		entries = []
		for i in range(self.camGroundHandler.getNumEntries()):
			entry = self.camGroundHandler.getEntry(i)
			entries.append(entry)
		entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
									 x.getSurfacePoint(render).getZ()))
		if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):
			base.camera.setZ(entries[0].getSurfacePoint(render).getZ()+1.0)
		if (base.camera.getZ() < self.ralph.getZ() + 2.0):
			base.camera.setZ(self.ralph.getZ() + 2.0)
			
		# The camera should look in ralph's direction,
		# but it should also try to stay horizontal, so look at
		# a floater which hovers above ralph's head.
		
		self.floater.setPos(self.ralph.getPos())
		self.floater.setZ(self.ralph.getZ() + 2.0)
		base.camera.lookAt(self.floater)

		return Task.cont


w = World()
run()

Great! I have a smartjoy so this will work great for me. Thanks a lot.