Checking joystick values in the SGE Game Engine:
Using python Syntax Highlighting
sge.joystick.get_axis(0, 1) # Value of joystick 0, axis 1
sge.joystick.get_hat_x(1, 0) # Horizontal position of joystick 1, d-pad 0
sge.joystick.get_hat_y(1, 0) # Vertical position of joystick 1, d-pad 0
sge.joystick.get_pressed(0, 2) # Whether or not joystick 0, button 2 is pressed
Or you can use events. I tend to use these functions for things like movement, and events for things like jumping. The good thing about using events is it avoids the (slim) possibility of a button press not registering. One nice thing about gamepads is you can usually come up with a configuration that will work for everyone, especially because you don't have to worry about keyjamming. I find that this works on all of them: - Buttons 1 and 2: primary action (e.g. jumping) - Buttons 0 and 3: secondary action (e.g. running) And of course, it's pretty much universal for buttons 4 and 6 to be L buttons, buttons 5 and 7 to be R buttons, button 8 to be Select, and button 9 to be Start. The best case, though, is always to let the user customize the controls somehow. The way I usually do this is to have two dictionaries (associative arrays): one for keyboard controls, and one for joystick controls. Since joystick controls vary in type, I have a tuple (array) that goes something like (0, "button", 1) for joystick 0, button 1. Then I have some admittedly long code to handle all possible cases. It's a bit less simple than hardcoded control definitions or locking certain actions to certain types of controls, but definitely worth it:
Using python Syntax Highlighting
class Player(xsge_physics.Collider):
# ...
def refresh_input(self):
self.left_pressed = sge.keyboard.get_pressed(left_key[self.player])
self.right_pressed = sge.keyboard.get_pressed(
right_key[self.player])
self.up_pressed = sge.keyboard.get_pressed(up_key[self.player])
self.down_pressed = sge.keyboard.get_pressed(down_key[self.player])
# Add current joystick state
js_controls = [left_js, right_js, up_js, down_js]
js_states = [False for i in js_controls]
for i in six.moves.range(len(js_controls)):
if js_controls[i][self.player] is not None:
j, t, c = js_controls[i][self.player]
if t == "axis+":
v = sge.joystick.get_axis(j, c)
if v > joystick_threshold:
js_states[i] = abs(v)
elif t == "axis-":
v = sge.joystick.get_axis(j, c)
if v < -joystick_threshold:
js_states[i] = abs(v)
elif t == "axis0":
js_states[i] = (abs(sge.joystick.get_axis(j, c)) <=
joystick_threshold)
elif t == "hatx+":
js_states[i] = (sge.joystick.get_hat_x(j, c) == 1)
elif t == "hatx-":
js_states[i] = (sge.joystick.get_hat_x(j, c) == -1)
elif t == "haty+":
js_states[i] = (sge.joystick.get_hat_y(j, c) == 1)
elif t == "haty-":
js_states[i] = (sge.joystick.get_hat_y(j, c) == -1)
elif t == "hatx0":
js_states[i] = (sge.joystick.get_hat_x(j, c) == 0)
elif t == "haty0":
js_states[i] = (sge.joystick.get_hat_y(j, c) == 0)
elif t == "button":
js_states[i] = sge.joystick.get_pressed(j, c)
self.left_pressed = self.left_pressed or js_states[0]
self.right_pressed = self.right_pressed or js_states[1]
self.up_pressed = self.up_pressed or js_states[2]
self.down_pressed = self.down_pressed or js_states[3]
# ...
def event_joystick_axis_move(self, js_name, js_id, axis, value):
js_versions = [(js_id, "axis+", axis), (js_id, "axis-", axis)]
if value > joystick_threshold:
js = (js_id, "axis+", axis)
elif value < -joystick_threshold:
js = (js_id, "axis-", axis)
else:
js = (js_id, "axis0", axis)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
def event_joystick_hat_move(self, js_name, js_id, hat, x, y):
js_versions = [(js_id, "hatx+", hat), (js_id, "hatx-", hat)]
if x > 0:
js = (js_id, "hatx+", hat)
elif x < 0:
js = (js_id, "hatx-", hat)
else:
js = (js_id, "hatx0", hat)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
js_versions = [(js_id, "haty+", hat), (js_id, "haty-", hat)]
if y > 0:
js = (js_id, "haty+", hat)
elif y < 0:
js = (js_id, "haty-", hat)
else:
js = (js_id, "haty0", hat)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
def event_joystick_button_press(self, js_name, js_id, button):
js = (js_id, "button", button)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
Checking joystick values in the SGE Game Engine:
[syntax=python]
sge.joystick.get_axis(0, 1) # Value of joystick 0, axis 1
sge.joystick.get_hat_x(1, 0) # Horizontal position of joystick 1, d-pad 0
sge.joystick.get_hat_y(1, 0) # Vertical position of joystick 1, d-pad 0
sge.joystick.get_pressed(0, 2) # Whether or not joystick 0, button 2 is pressed
[/syntax]
Or you can use events. I tend to use these functions for things like movement, and events for things like jumping. The good thing about using events is it avoids the (slim) possibility of a button press not registering.
One nice thing about gamepads is you can usually come up with a configuration that will work for everyone, especially because you don't have to worry about keyjamming. I find that this works on all of them:
- Buttons 1 and 2: primary action (e.g. jumping)
- Buttons 0 and 3: secondary action (e.g. running)
And of course, it's pretty much universal for buttons 4 and 6 to be L buttons, buttons 5 and 7 to be R buttons, button 8 to be Select, and button 9 to be Start.
The best case, though, is always to let the user customize the controls somehow. The way I usually do this is to have two dictionaries (associative arrays): one for keyboard controls, and one for joystick controls. Since joystick controls vary in type, I have a tuple (array) that goes something like (0, "button", 1) for joystick 0, button 1. Then I have some admittedly long code to handle all possible cases. It's a bit less simple than hardcoded control definitions or locking certain actions to certain types of controls, but definitely worth it:
[syntax=python]
class Player(xsge_physics.Collider):
# ...
def refresh_input(self):
self.left_pressed = sge.keyboard.get_pressed(left_key[self.player])
self.right_pressed = sge.keyboard.get_pressed(
right_key[self.player])
self.up_pressed = sge.keyboard.get_pressed(up_key[self.player])
self.down_pressed = sge.keyboard.get_pressed(down_key[self.player])
# Add current joystick state
js_controls = [left_js, right_js, up_js, down_js]
js_states = [False for i in js_controls]
for i in six.moves.range(len(js_controls)):
if js_controls[i][self.player] is not None:
j, t, c = js_controls[i][self.player]
if t == "axis+":
v = sge.joystick.get_axis(j, c)
if v > joystick_threshold:
js_states[i] = abs(v)
elif t == "axis-":
v = sge.joystick.get_axis(j, c)
if v < -joystick_threshold:
js_states[i] = abs(v)
elif t == "axis0":
js_states[i] = (abs(sge.joystick.get_axis(j, c)) <=
joystick_threshold)
elif t == "hatx+":
js_states[i] = (sge.joystick.get_hat_x(j, c) == 1)
elif t == "hatx-":
js_states[i] = (sge.joystick.get_hat_x(j, c) == -1)
elif t == "haty+":
js_states[i] = (sge.joystick.get_hat_y(j, c) == 1)
elif t == "haty-":
js_states[i] = (sge.joystick.get_hat_y(j, c) == -1)
elif t == "hatx0":
js_states[i] = (sge.joystick.get_hat_x(j, c) == 0)
elif t == "haty0":
js_states[i] = (sge.joystick.get_hat_y(j, c) == 0)
elif t == "button":
js_states[i] = sge.joystick.get_pressed(j, c)
self.left_pressed = self.left_pressed or js_states[0]
self.right_pressed = self.right_pressed or js_states[1]
self.up_pressed = self.up_pressed or js_states[2]
self.down_pressed = self.down_pressed or js_states[3]
# ...
def event_joystick_axis_move(self, js_name, js_id, axis, value):
js_versions = [(js_id, "axis+", axis), (js_id, "axis-", axis)]
if value > joystick_threshold:
js = (js_id, "axis+", axis)
elif value < -joystick_threshold:
js = (js_id, "axis-", axis)
else:
js = (js_id, "axis0", axis)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
def event_joystick_hat_move(self, js_name, js_id, hat, x, y):
js_versions = [(js_id, "hatx+", hat), (js_id, "hatx-", hat)]
if x > 0:
js = (js_id, "hatx+", hat)
elif x < 0:
js = (js_id, "hatx-", hat)
else:
js = (js_id, "hatx0", hat)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
js_versions = [(js_id, "haty+", hat), (js_id, "haty-", hat)]
if y > 0:
js = (js_id, "haty+", hat)
elif y < 0:
js = (js_id, "haty-", hat)
else:
js = (js_id, "haty0", hat)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
def event_joystick_button_press(self, js_name, js_id, button):
js = (js_id, "button", button)
if js == tuple(action_js[self.player]):
self.action()
if js == tuple(pause_js[self.player]):
sge.game.current_room.pause()
[/syntax]
_________________
|