Added gamepad support. Yes, more globals... Globals are good.
This commit is contained in:
parent
21216f361a
commit
ab2a5f58f5
66
__init__.py
66
__init__.py
@ -48,6 +48,34 @@ except ImportError:
|
|||||||
localConfig = configparser.ConfigParser()
|
localConfig = configparser.ConfigParser()
|
||||||
globalConfig = configparser.ConfigParser()
|
globalConfig = configparser.ConfigParser()
|
||||||
|
|
||||||
|
# Gamepad state globals
|
||||||
|
try:
|
||||||
|
pygame.joystick.init()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
gamepadLeft = False
|
||||||
|
gamepadRight = False
|
||||||
|
gamepadUp = False
|
||||||
|
gamepadDown = False
|
||||||
|
gamepadX = False
|
||||||
|
gamepadCircle = False
|
||||||
|
gamepadSquare = False
|
||||||
|
gamepadTriangle = False
|
||||||
|
gamepadL1 = False
|
||||||
|
gamepadR1 = False
|
||||||
|
gamepadL2 = False
|
||||||
|
gamepadR2 = False
|
||||||
|
gamepadShare = False
|
||||||
|
gamepadOptions = False
|
||||||
|
gamepadPlaystation = False
|
||||||
|
gamepad = None if pygame.joystick.get_count() == 0 else pygame.joystick.Joystick(0)
|
||||||
|
if gamepad:
|
||||||
|
try:
|
||||||
|
gamepad.init()
|
||||||
|
except:
|
||||||
|
gamepad = None
|
||||||
|
|
||||||
# Volume control globals
|
# Volume control globals
|
||||||
bgmVolume = 0.75 # Default background music volume
|
bgmVolume = 0.75 # Default background music volume
|
||||||
sfxVolume = 1.0 # Default sound effects volume
|
sfxVolume = 1.0 # Default sound effects volume
|
||||||
@ -245,6 +273,44 @@ def initialize_gui(gameTitle):
|
|||||||
|
|
||||||
return soundData
|
return soundData
|
||||||
|
|
||||||
|
def update_gamepad():
|
||||||
|
"""Update global gamepad state variables."""
|
||||||
|
global gamepadLeft, gamepadRight, gamepadUp, gamepadDown
|
||||||
|
global gamepadX, gamepadCircle, gamepadSquare, gamepadTriangle
|
||||||
|
global gamepadL1, gamepadR1, gamepadL2, gamepadR2
|
||||||
|
global gamepadShare, gamepadOptions, gamepadPlaystation, gamepad
|
||||||
|
|
||||||
|
try:
|
||||||
|
if gamepad:
|
||||||
|
# Get D-pad state
|
||||||
|
hat = gamepad.get_hat(0)
|
||||||
|
gamepadLeft = hat[0] < 0
|
||||||
|
gamepadRight = hat[0] > 0
|
||||||
|
gamepadUp = hat[1] > 0
|
||||||
|
gamepadDown = hat[1] < 0
|
||||||
|
|
||||||
|
# Also check analog stick with deadzone
|
||||||
|
x_axis = gamepad.get_axis(0)
|
||||||
|
if x_axis < -0.2: # Left deadzone
|
||||||
|
gamepadLeft = True
|
||||||
|
elif x_axis > 0.2: # Right deadzone
|
||||||
|
gamepadRight = True
|
||||||
|
|
||||||
|
# Update button states
|
||||||
|
gamepadX = gamepad.get_button(0)
|
||||||
|
gamepadCircle = gamepad.get_button(1)
|
||||||
|
gamepadTriangle = gamepad.get_button(2)
|
||||||
|
gamepadSquare = gamepad.get_button(3)
|
||||||
|
gamepadL1 = gamepad.get_button(4)
|
||||||
|
gamepadR1 = gamepad.get_button(5)
|
||||||
|
gamepadL2 = gamepad.get_button(6)
|
||||||
|
gamepadR2 = gamepad.get_button(7)
|
||||||
|
gamepadShare = gamepad.get_button(8)
|
||||||
|
gamepadOptions = gamepad.get_button(9)
|
||||||
|
gamepadPlaystation = gamepad.get_button(10)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def adjust_master_volume(change):
|
def adjust_master_volume(change):
|
||||||
"""Adjust the master volume for all sounds.
|
"""Adjust the master volume for all sounds.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user