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 http://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
 11"""
 12
 13import arcade
 14import os
 15
 16# --- Constants ---
 17SPRITE_SCALING_PLAYER = 0.5
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprites with Properties Example"
 22
 23
 24class MyGame(arcade.Window):
 25    """ Our custom Window Class"""
 26
 27    def __init__(self):
 28        """ Initializer """
 29        # Call the parent class initializer
 30        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 31
 32        # Set the working directory (where we expect to find files) to the same
 33        # directory this .py file is in. You can leave this out of your own
 34        # code, but it is needed to easily run the examples using "python -m"
 35        # as mentioned at the top of this program.
 36        file_path = os.path.dirname(os.path.abspath(__file__))
 37        os.chdir(file_path)
 38
 39        # Variables that will hold sprite lists
 40        self.player_list = None
 41        self.coin_list = None
 42
 43        # Set up the player info
 44        self.player_sprite = None
 45
 46        # Set up sprite that will serve as trigger
 47        self.trigger_sprite = None
 48
 49        # Don't show the mouse cursor
 50        self.set_mouse_visible(False)
 51
 52        arcade.set_background_color(arcade.color.AMAZON)
 53
 54    def setup(self):
 55        """ Set up the game and initialize the variables. """
 56
 57        # Sprite lists
 58        self.player_list = arcade.SpriteList()
 59        self.coin_list = arcade.SpriteList()
 60
 61        # Set up the player
 62        # Character image from kenney.nl
 63        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
 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(100, 800, 100):
 70            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=0.3, center_x=x, center_y=400)
 71            coin.intensity = 'dim'
 72            coin.alpha = 64
 73            self.coin_list.append(coin)
 74
 75        # Create trigger
 76        self.trigger_sprite = arcade.Sprite(":resources:images/pinball/bumper.png", scale=0.5, center_x=750, center_y=50)
 77
 78    def on_draw(self):
 79        """ Draw everything """
 80        arcade.start_render()
 81        self.coin_list.draw()
 82        self.trigger_sprite.draw()
 83        self.player_list.draw()
 84
 85        # Put the instructions on the screen.
 86        instructions1 = "Touch a coin to set its intensity property to 'bright'."
 87        arcade.draw_text(instructions1, 10, 90, arcade.color.WHITE, 14)
 88        instructions2 = "Touch the trigger at the bottom-right to destroy all 'bright' sprites."
 89        arcade.draw_text(instructions2, 10, 70, arcade.color.WHITE, 14)
 90
 91        # Query the property on the coins and show results.
 92        coins_are_bright = [coin.intensity == 'bright' for coin in self.coin_list]
 93        output_any = f"Any sprites have intensity=bright? : {any(coins_are_bright)}"
 94        arcade.draw_text(output_any, 10, 40, arcade.color.WHITE, 14)
 95        output_all = f"All sprites have intensity=bright? : {all(coins_are_bright)}"
 96        arcade.draw_text(output_all, 10, 20, arcade.color.WHITE, 14)
 97
 98    def on_mouse_motion(self, x, y, dx, dy):
 99        """ Handle Mouse Motion """
100
101        # Move the center of the player sprite to match the mouse x, y
102        self.player_sprite.center_x = x
103        self.player_sprite.center_y = y
104
105    def on_update(self, delta_time):
106        """ Movement and game logic """
107
108        # Call update on all sprites (The sprites don't do much in this
109        # example though.)
110        self.coin_list.update()
111
112        # Generate a list of all sprites that collided with the player.
113        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
114
115        # Loop through each colliding sprite to set intensity=bright
116        for coin in coins_hit_list:
117            coin.intensity = 'bright'
118            coin.alpha = 255
119
120        hit_trigger = arcade.check_for_collision(self.player_sprite, self.trigger_sprite)
121        if hit_trigger:
122            intense_sprites = [sprite for sprite in self.coin_list if sprite.intensity == 'bright']
123            for coin in intense_sprites:
124                coin.remove_from_sprite_lists()
125
126
127def main():
128    """ Main method """
129    window = MyGame()
130    window.setup()
131    arcade.run()
132
133
134if __name__ == "__main__":
135    main()