Program Arcade Games by Paul Vincent Craven

Program Arcade Games by Paul Vincent Craven

Author:Paul Vincent Craven
Language: eng
Format: epub, mobi
Publisher: Apress, Berkeley, CA


K_RALT

right

alt

K_LALT

left

alt

Game Controller

Game controllers require a different set of code, but the idea is still simple.

To begin, check to see if the computer has a joystick, and initialize it before use. This should only be done once. Do it ahead of the main program loop:

# Current position

x_coord = 10

y_coord = 10

# Count the joysticks the computer has

joystick_count = pygame.joystick.get_count()

if joystick_count == 0:

# No joysticks!

print("Error, I didn’t find any joysticks.")

else:

# Use joystick #0 and initialize it

my_joystick = pygame.joystick.Joystick(0)

my_joystick.init()

A joystick will return two floating-point values. If the joystick is perfectly centered it will return (0, 0). If the joystick is fully up and to the left it will return (-1, -1). If the joystick is down and to the right it will return (1, 1). If the joystick is somewhere in between, values are scaled accordingly. See the controller images starting at the following figures to get an idea how it works.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.