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 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.

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},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
		self.jsWorks = False
		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)
			if (self.joystick.get_init() == True):
				self.jsWorks = True
				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")
			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")      
        # Create the main character, Ralph

		ralphStartPos = self.environ.find("**/start_point").getPos()
		self.ralph = Actor("models/ralph",

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

		# (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
			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])


		# Game state variables
		self.isMoving = False

		# Set up the camera
		# 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.ralphGroundCol = CollisionNode('ralphRay')
		self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
		self.ralphGroundHandler = CollisionHandlerQueue()
		self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

		self.camGroundRay = CollisionRay()
		self.camGroundCol = CollisionNode('camRay')
		self.camGroundColNp =
		self.camGroundHandler = CollisionHandlerQueue()
		self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)

		# Uncomment this line to see the collision rays
		#Uncomment this line to show a visual representation of the 
		#collisions occuring

	#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)
				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)
				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)
				self.setKey ("left",0)
				self.setKey ("right",0)
		if (self.joystick.get_button(0) == True):
		if (self.joystick.get_button(1) == True):
		#This loop will only make the events be processed.
		for event in pygame.event.get():

	def finishJoystick (self):
	# 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):

		# 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.
		if (self.keyMap["cam-left"]!=0):, -(elapsed*20))
		if (self.keyMap["cam-right"]!=0):, +(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.isMoving = True
			if self.isMoving:
				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() -
		camdist = camvec.length()
		if (camdist > 10.0): + camvec*(camdist-10))
			camdist = 10.0
		if (camdist < 5.0): - camvec*(5-camdist))
			camdist = 5.0

		# Now check for collisions.


		# 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.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
		if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):

		# 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.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
		if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):[0].getSurfacePoint(render).getZ()+1.0)
		if ( < self.ralph.getZ() + 2.0): + 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.setZ(self.ralph.getZ() + 2.0)

		return Task.cont

w = World()

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