Move By Mouse
This is an example showing basic sprite usage. Collect coins with your mouse, and keep score!

Source Code
sprite_collect_coins.py
1"""
2Sprite Collect Coins
3
4A simple game demonstrating an easy way to create and use sprites.
5
6Artwork from https://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the
9command line with:
10python -m arcade.examples.sprite_collect_coins
11"""
12
13import random
14import arcade
15
16# --- Constants ---
17SPRITE_SCALING_PLAYER = 0.5
18SPRITE_SCALING_COIN = 0.4
19COIN_COUNT = 50
20
21WINDOW_WIDTH = 1280
22WINDOW_HEIGHT = 720
23WINDOW_TITLE = "Sprite Collect Coins Example"
24
25
26class GameView(arcade.View):
27
28 def __init__(self):
29 """ Initializer """
30 # Call the parent class initializer
31 super().__init__()
32
33 # Variables that will hold sprite lists
34 self.player_list = None
35 self.coin_list = None
36
37 # Create a variable to hold the player sprite
38 self.player_sprite = None
39
40 # Variables to hold the score and the Text object displaying it
41 self.score = 0
42 self.score_display = None
43
44 # Hide the mouse cursor while it's over the window
45 self.window.set_mouse_visible(False)
46
47 self.background_color = arcade.color.AMAZON
48
49 def setup(self):
50 """ Set up the game and initialize the variables. """
51
52 # Create the sprite lists
53 self.player_list = arcade.SpriteList()
54 self.coin_list = arcade.SpriteList()
55
56 # Reset the score and the score display
57 self.score = 0
58 self.score_display = arcade.Text(
59 text="Score: 0", x=10, y=20,
60 color=arcade.color.WHITE, font_size=14)
61
62 # Set up the player
63 # Character image from kenney.nl
64 img = ":resources:images/animated_characters/female_person/femalePerson_idle.png"
65 self.player_sprite = arcade.Sprite(img, scale=SPRITE_SCALING_PLAYER)
66 self.player_sprite.position = 50, 50
67 self.player_list.append(self.player_sprite)
68
69 # Create the coins
70 for i in range(COIN_COUNT):
71
72 # Create the coin instance
73 # Coin image from kenney.nl
74 coin = arcade.Sprite(":resources:images/items/coinGold.png",
75 scale=SPRITE_SCALING_COIN)
76
77 # Position the coin
78 coin.center_x = random.randrange(WINDOW_WIDTH)
79 coin.center_y = random.randrange(WINDOW_HEIGHT)
80
81 # Add the coin to the lists
82 self.coin_list.append(coin)
83
84 def on_draw(self):
85 """ Draw everything """
86
87 # Clear the screen to only show the background color
88 self.clear()
89
90 # Draw the sprites
91 self.coin_list.draw()
92 self.player_list.draw()
93
94 # Draw the score Text object on the screen
95 self.score_display.draw()
96
97 def on_mouse_motion(self, x, y, dx, dy):
98 """ Handle Mouse Motion """
99
100 # Move the player sprite to place its center on the mouse x, y
101 self.player_sprite.position = x, y
102
103 def on_update(self, delta_time):
104 """ Movement and game logic """
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,
108 self.coin_list)
109
110 # Keep track of the score from before collisions occur
111 old_score = self.score
112
113 # Loop through each colliding sprite, remove it, and add to the score.
114 for coin in coins_hit_list:
115 coin.remove_from_sprite_lists()
116 self.score += 1
117
118 # Update the score display if the score changed this tick
119 if old_score != self.score:
120 self.score_display.text = f"Score: {self.score}"
121
122
123def main():
124 """ Main function """
125 # Create a window class. This is what actually shows up on screen
126 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
127
128 # Create and setup the GameView
129 game = GameView()
130 game.setup()
131
132 # Show GameView on screen
133 window.show_view(game)
134
135 # Start the arcade game loop
136 arcade.run()
137
138
139if __name__ == "__main__":
140 main()