Game Controller

sprite_move_controller.py
  1"""
  2Move Sprite with Controller
  3
  4An example of one way to use controller and controller input to move a
  5sprite.
  6
  7Artwork from https://kenney.nl
  8
  9If Python and Arcade are installed, this example can be run from the
 10command line with:
 11python -m arcade.examples.sprite_move_controller
 12"""
 13import arcade
 14
 15SPRITE_SCALING = 0.5
 16
 17SCREEN_WIDTH = 800
 18SCREEN_HEIGHT = 600
 19SCREEN_TITLE = "Move Sprite with Controller Example"
 20
 21MOVEMENT_SPEED = 5
 22DEAD_ZONE = 0.05
 23
 24
 25class Player(arcade.Sprite):
 26    """ Player sprite """
 27
 28    def __init__(self, filename, scale):
 29        super().__init__(filename, scale=scale)
 30
 31        self.controller = None
 32
 33        controllers = arcade.get_controllers()
 34
 35        # If we have any...
 36        if controllers:
 37            # Grab the first one in  the list
 38            self.controller = controllers[0]
 39
 40            # Open it for input
 41            self.controller.open()
 42
 43            # Push this object as a handler for controller events.
 44            # Required for the controller events to be called.
 45            self.controller.push_handlers(self)
 46
 47    def update(self):
 48        """ Move the player """
 49
 50        # If there is a controller, grab the speed.
 51        if self.controller:
 52
 53            # x-axis
 54            movement_x = self.controller.leftx
 55            if abs(movement_x) < DEAD_ZONE:
 56                self.change_x = 0
 57            else:
 58                self.change_x = movement_x * MOVEMENT_SPEED
 59
 60            # y-axis
 61            movement_y = self.controller.lefty
 62            if abs(movement_y) < DEAD_ZONE:
 63                self.change_y = 0
 64            else:
 65                self.change_y = movement_y * MOVEMENT_SPEED
 66
 67        # Move the player
 68        self.center_x += self.change_x
 69        self.center_y += self.change_y
 70
 71        # Keep from moving off-screen
 72        if self.left < 0:
 73            self.left = 0
 74        elif self.right > SCREEN_WIDTH - 1:
 75            self.right = SCREEN_WIDTH - 1
 76
 77        if self.bottom < 0:
 78            self.bottom = 0
 79        elif self.top > SCREEN_HEIGHT - 1:
 80            self.top = SCREEN_HEIGHT - 1
 81
 82    # noinspection PyMethodMayBeStatic
 83    def on_button_press(self, controller, button_name):
 84        """ Handle button-down event for the controller """
 85        print(f"Button {button_name} down")
 86
 87    # noinspection PyMethodMayBeStatic
 88    def on_button_release(self, controller, button_name):
 89        """ Handle button-up event for the controller """
 90        print(f"Button {button_name} up")
 91
 92    # noinspection PyMethodMayBeStatic
 93    def on_stick_motion(self, controller, stick_name, x, y):
 94        """ Handle hat events """
 95        print(f"Movement on stick {stick_name}: ({x}, {y})")
 96
 97
 98class MyGame(arcade.Window):
 99    """
100    Main application class.
101    """
102
103    def __init__(self, width, height, title):
104        """
105        Initializer
106        """
107        # Call the parent class initializer
108        super().__init__(width, height, title)
109
110        # Variables that will hold sprite lists
111        self.all_sprites_list = None
112
113        # Set up the player info
114        self.player_sprite = None
115
116        # Set the background color
117        self.background_color = arcade.color.AMAZON
118
119        self.error_text = arcade.Text(
120            "There are no controllers, plug in a controller and run again.",
121            10,
122            10,
123            arcade.color.WHITE,
124            22,
125            width=SCREEN_WIDTH,
126            align="center",
127        )
128
129    def setup(self):
130        """ Set up the game and initialize the variables. """
131
132        # Sprite lists
133        self.all_sprites_list = arcade.SpriteList()
134
135        # Set up the player
136        self.player_sprite = Player(
137            ":resources:images/animated_characters/female_person/"
138            "femalePerson_idle.png",
139            SPRITE_SCALING,
140        )
141        self.player_sprite.position = self.width / 2, self.height / 2
142        self.all_sprites_list.append(self.player_sprite)
143
144    def on_draw(self):
145        """
146        Render the screen.
147        """
148
149        # This command has to happen before we start drawing
150        self.clear()
151
152        # Draw all the sprites.
153        self.all_sprites_list.draw()
154
155        # Print an error if there is no controller
156        if not self.player_sprite.controller:
157            self.error_text.draw()
158
159    def on_update(self, delta_time):
160        """ Movement and game logic """
161
162        # Call update on all sprites (The sprites don't do much in this
163        # example though.)
164        self.all_sprites_list.update()
165
166    def on_key_press(self, key, modifiers):
167        """Called whenever a key is pressed. """
168
169        if key == arcade.key.UP:
170            self.player_sprite.change_y = MOVEMENT_SPEED
171        elif key == arcade.key.DOWN:
172            self.player_sprite.change_y = -MOVEMENT_SPEED
173        elif key == arcade.key.LEFT:
174            self.player_sprite.change_x = -MOVEMENT_SPEED
175        elif key == arcade.key.RIGHT:
176            self.player_sprite.change_x = MOVEMENT_SPEED
177
178    def on_key_release(self, key, modifiers):
179        """Called when the user releases a key. """
180
181        if key == arcade.key.UP or key == arcade.key.DOWN:
182            self.player_sprite.change_y = 0
183        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
184            self.player_sprite.change_x = 0
185
186
187def main():
188    """ Main function """
189    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
190    window.setup()
191    arcade.run()
192
193
194if __name__ == "__main__":
195    main()