Sprite Properties

Screen shot of using sprites to collect coins
sprite_properties.py
  1"""
  2Sprites with Properties Example
  3
  4Simple program to show how to store properties on sprites.
  5
  6Artwork from https://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.sprite_properties
 10"""
 11import arcade
 12
 13# --- Constants ---
 14SPRITE_SCALING_PLAYER = 0.5
 15
 16WINDOW_WIDTH = 1280
 17WINDOW_HEIGHT = 720
 18WINDOW_TITLE = "Sprites with Properties Example"
 19
 20
 21INSTRUCTIONS1 = (
 22    "Touch a coin to set its intensity property to 'bright'."
 23    "Press 'R' to reset the sprites"
 24)
 25INSTRUCTIONS2 = "Touch the trigger at the bottom-right to destroy all 'bright' sprites."
 26
 27
 28class GameView(arcade.View):
 29    """ Our custom Window Class"""
 30
 31    def __init__(self):
 32        """ Initializer """
 33        # Call the parent class initializer
 34        super().__init__()
 35
 36        # Variables that will hold sprite lists
 37        self.player_list = None
 38        self.coin_list = None
 39
 40        # Set up the player info
 41        self.player_sprite = None
 42
 43        # Set up sprite that will serve as trigger
 44        self.trigger_sprite = None
 45
 46        # Don't show the mouse cursor
 47        self.window.set_mouse_visible(False)
 48
 49        self.background_color = arcade.color.AMAZON
 50
 51    def setup(self):
 52        """ Set up the game and initialize the variables. """
 53
 54        # Sprite lists
 55        self.player_list = arcade.SpriteList()
 56        self.coin_list = arcade.SpriteList()
 57
 58        # Set up the player
 59        # Character image from kenney.nl
 60        self.player_sprite = arcade.Sprite(
 61            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 62            scale=0.75,
 63        )
 64        self.player_sprite.center_x = 50
 65        self.player_sprite.center_y = 150
 66        self.player_list.append(self.player_sprite)
 67
 68        # Create the sprites
 69        for x in range(180, 1100, 100):
 70            coin = arcade.Sprite(
 71                ":resources:images/items/coinGold.png",
 72                scale=0.3,
 73                center_x=x,
 74                center_y=400,
 75            )
 76            coin.intensity = 'dim'
 77            coin.alpha = 64
 78            self.coin_list.append(coin)
 79
 80        # Create trigger
 81        self.trigger_sprite = arcade.Sprite(
 82            ":resources:images/pinball/bumper.png", scale=0.5,
 83            center_x=750, center_y=50,
 84        )
 85
 86    def on_key_press(self, symbol, modifiers):
 87        if symbol == arcade.key.R:
 88            self.setup()
 89
 90    def on_draw(self):
 91        """ Draw everything """
 92        self.clear()
 93        self.coin_list.draw()
 94        arcade.draw_sprite(self.trigger_sprite)
 95        self.player_list.draw()
 96
 97        # Put the instructions on the screen.
 98        arcade.draw_text(INSTRUCTIONS1, 10, 90, arcade.color.WHITE, 14)
 99        arcade.draw_text(INSTRUCTIONS2, 10, 70, arcade.color.WHITE, 14)
100
101        # Query the property on the coins and show results.
102        coins_are_bright = [coin.intensity == 'bright' for coin in self.coin_list]
103        output_any = f"Any sprites have intensity=bright? : {any(coins_are_bright)}"
104        arcade.draw_text(output_any, 10, 40, arcade.color.WHITE, 14)
105        output_all = f"All sprites have intensity=bright? : {all(coins_are_bright)}"
106        arcade.draw_text(output_all, 10, 20, arcade.color.WHITE, 14)
107
108    def on_mouse_motion(self, x, y, dx, dy):
109        """ Handle Mouse Motion """
110
111        # Move the center of the player sprite to match the mouse x, y
112        self.player_sprite.center_x = x
113        self.player_sprite.center_y = y
114
115    def on_update(self, delta_time):
116        """ Movement and game logic """
117
118        # Call update on all sprites (The sprites don't do much in this
119        # example though.)
120        self.coin_list.update()
121
122        # Generate a list of all sprites that collided with the player.
123        coins_hit_list = arcade.check_for_collision_with_list(
124            self.player_sprite, self.coin_list,
125        )
126
127        # Loop through each colliding sprite to set intensity=bright
128        for coin in coins_hit_list:
129            coin.intensity = 'bright'
130            coin.alpha = 255
131
132        hit_trigger = arcade.check_for_collision(self.player_sprite, self.trigger_sprite)
133        if hit_trigger:
134            intense_sprites = [
135                sprite for sprite in self.coin_list if sprite.intensity == 'bright'
136            ]
137            for coin in intense_sprites:
138                coin.remove_from_sprite_lists()
139
140
141def main():
142    """ Main function """
143    # Create a window class. This is what actually shows up on screen
144    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
145
146    # Create and setup the GameView
147    game = GameView()
148    game.setup()
149
150    # Show GameView on screen
151    window.show_view(game)
152
153    # Start the arcade game loop
154    arcade.run()
155
156
157if __name__ == "__main__":
158    main()