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