Change coins#

Screenshot of using sprites to change coins
sprite_change_coins.py#
  1"""
  2Sprite Change Coins
  3
  4This shows how you can change a sprite once it is hit, rather than eliminate it.
  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_change_coins
 10"""
 11
 12from __future__ import annotations
 13
 14import random
 15import arcade
 16
 17SPRITE_SCALING = 1
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprite Change Coins"
 22
 23
 24class Collectable(arcade.Sprite):
 25    """ This class represents something the player collects. """
 26
 27    def __init__(self, filename, scale):
 28        super().__init__(filename, scale=scale)
 29        # Flip this once the coin has been collected.
 30        self.changed = False
 31
 32
 33class MyGame(arcade.Window):
 34    """
 35    Main application class.a
 36    """
 37
 38    def __init__(self, width, height, title):
 39        super().__init__(width, height, title)
 40
 41        # Sprite lists
 42        self.player_list = None
 43        self.coin_list = None
 44
 45        # Set up the player
 46        self.score = 0
 47        self.player_sprite = None
 48
 49    def setup(self):
 50        """ Set up the game and initialize the variables. """
 51
 52        # Sprite lists
 53        self.player_list = arcade.SpriteList()
 54        self.coin_list = arcade.SpriteList()
 55
 56        # Set up the player
 57        self.score = 0
 58        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
 59                                           "femalePerson_idle.png", scale=0.5)
 60        self.player_sprite.center_x = 50
 61        self.player_sprite.center_y = 50
 62        self.player_list.append(self.player_sprite)
 63
 64        for i in range(50):
 65            # Create the coin instance
 66            coin = Collectable(":resources:images/items/coinGold.png", scale=SPRITE_SCALING)
 67            coin.width = 30
 68            coin.height = 30
 69
 70            # Position the coin
 71            coin.center_x = random.randrange(SCREEN_WIDTH)
 72            coin.center_y = random.randrange(SCREEN_HEIGHT)
 73
 74            # Add the coin to the lists
 75            self.coin_list.append(coin)
 76
 77        # Don't show the mouse cursor
 78        self.set_mouse_visible(False)
 79
 80        # Set the background color
 81        self.background_color = arcade.color.AMAZON
 82
 83    def on_draw(self):
 84        """
 85        Render the screen.
 86        """
 87
 88        # This command has to happen before we start drawing
 89        self.clear()
 90
 91        # Draw all the sprites.
 92        self.coin_list.draw()
 93        self.player_list.draw()
 94
 95        # Put the text on the screen.
 96        output = f"Score: {self.score}"
 97        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
 98
 99    def on_mouse_motion(self, x, y, dx, dy):
100        """
101        Called whenever the mouse moves.
102        """
103        self.player_sprite.center_x = x
104        self.player_sprite.center_y = y
105
106    def on_update(self, delta_time):
107        """ Movement and game logic """
108
109        # Call update on all sprites (The sprites don't do much in this
110        # example though.)
111        self.player_list.update()
112        self.coin_list.update()
113
114        # Generate a list of all sprites that collided with the player.
115        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
116
117        # Loop through each colliding sprite, change it, and add to the score.
118        for coin in hit_list:
119            # Have we collected this?
120            if not coin.changed:
121                # No? Then do so
122                coin.append_texture(arcade.load_texture(":resources:images/pinball/bumper.png"))
123                coin.set_texture(1)
124                coin.changed = True
125                coin.width = 30
126                coin.height = 30
127                self.score += 1
128
129
130def main():
131    """ Main function """
132    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
133    window.setup()
134    arcade.run()
135
136
137if __name__ == "__main__":
138    main()