Collect Coins - Mouse

Screen shot of using sprites to collect coins
sprite_collect_coins.py
  1"""
  2Sprite Collect Coins
  3
  4Simple program to show basic sprite usage.
  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_collect_coins
 10"""
 11
 12import random
 13import arcade
 14
 15# --- Constants ---
 16SPRITE_SCALING_PLAYER = 0.5
 17SPRITE_SCALING_COIN = .25
 18COIN_COUNT = 50
 19
 20SCREEN_WIDTH = 800
 21SCREEN_HEIGHT = 600
 22SCREEN_TITLE = "Sprite Collect Coins Example"
 23
 24
 25class MyGame(arcade.Window):
 26    """ Our custom Window Class"""
 27
 28    def __init__(self):
 29        """ Initializer """
 30        # Call the parent class initializer
 31        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 32
 33        # Variables that will hold sprite lists
 34        self.player_list = None
 35        self.coin_list = None
 36
 37        # Set up the player info
 38        self.player_sprite = None
 39        self.score = 0
 40
 41        # Don't show the mouse cursor
 42        self.set_mouse_visible(False)
 43
 44        arcade.set_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        # Score
 54        self.score = 0
 55
 56        # Set up the player
 57        # Character image from kenney.nl
 58        img = ":resources:images/animated_characters/female_person/femalePerson_idle.png"
 59        self.player_sprite = arcade.Sprite(img, SPRITE_SCALING_PLAYER)
 60        self.player_sprite.center_x = 50
 61        self.player_sprite.center_y = 50
 62        self.player_list.append(self.player_sprite)
 63
 64        # Create the coins
 65        for i in range(COIN_COUNT):
 66
 67            # Create the coin instance
 68            # Coin image from kenney.nl
 69            coin = arcade.Sprite(":resources:images/items/coinGold.png",
 70                                 SPRITE_SCALING_COIN)
 71
 72            # Position the coin
 73            coin.center_x = random.randrange(SCREEN_WIDTH)
 74            coin.center_y = random.randrange(SCREEN_HEIGHT)
 75
 76            # Add the coin to the lists
 77            self.coin_list.append(coin)
 78
 79    def on_draw(self):
 80        """ Draw everything """
 81        arcade.start_render()
 82        self.coin_list.draw()
 83        self.player_list.draw()
 84
 85        # Put the text on the screen.
 86        output = f"Score: {self.score}"
 87        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
 88
 89    def on_mouse_motion(self, x, y, dx, dy):
 90        """ Handle Mouse Motion """
 91
 92        # Move the center of the player sprite to match the mouse x, y
 93        self.player_sprite.center_x = x
 94        self.player_sprite.center_y = y
 95
 96    def on_update(self, delta_time):
 97        """ Movement and game logic """
 98
 99        # Call update on all sprites (The sprites don't do much in this
100        # example though.)
101        self.coin_list.update()
102
103        # Generate a list of all sprites that collided with the player.
104        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
105                                                              self.coin_list)
106
107        # Loop through each colliding sprite, remove it, and add to the score.
108        for coin in coins_hit_list:
109            coin.remove_from_sprite_lists()
110            self.score += 1
111
112
113def main():
114    """ Main method """
115    window = MyGame()
116    window.setup()
117    arcade.run()
118
119
120if __name__ == "__main__":
121    main()