Move By Mouse#
This is an example showing basic sprite usage. Collect coins with your mouse, and keep score!
Source Code#
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
13from __future__ import annotations
14
15import random
16import arcade
17
18# --- Constants ---
19SPRITE_SCALING_PLAYER = 0.5
20SPRITE_SCALING_COIN = .25
21COIN_COUNT = 50
22
23SCREEN_WIDTH = 800
24SCREEN_HEIGHT = 600
25SCREEN_TITLE = "Sprite Collect Coins Example"
26
27
28class MyGame(arcade.Window):
29 """ Our custom Window Class"""
30
31 def __init__(self):
32 """ Initializer """
33 # Call the parent class initializer
34 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
35
36 # Variables that will hold sprite lists
37 self.player_list = None
38 self.coin_list = None
39
40 # Create a variable to hold the player sprite
41 self.player_sprite = None
42
43 # Variables to hold the score and the Text object displaying it
44 self.score = 0
45 self.score_display = None
46
47 # Hide the mouse cursor while it's over the window
48 self.set_mouse_visible(False)
49
50 self.background_color = arcade.color.AMAZON
51
52 def setup(self):
53 """ Set up the game and initialize the variables. """
54
55 # Create the sprite lists
56 self.player_list = arcade.SpriteList()
57 self.coin_list = arcade.SpriteList()
58
59 # Reset the score and the score display
60 self.score = 0
61 self.score_display = arcade.Text(
62 text="Score: 0", start_x=10, start_y=20,
63 color=arcade.color.WHITE, font_size=14)
64
65 # Set up the player
66 # Character image from kenney.nl
67 img = ":resources:images/animated_characters/female_person/femalePerson_idle.png"
68 self.player_sprite = arcade.Sprite(img, scale=SPRITE_SCALING_PLAYER)
69 self.player_sprite.position = 50, 50
70 self.player_list.append(self.player_sprite)
71
72 # Create the coins
73 for i in range(COIN_COUNT):
74
75 # Create the coin instance
76 # Coin image from kenney.nl
77 coin = arcade.Sprite(":resources:images/items/coinGold.png",
78 scale=SPRITE_SCALING_COIN)
79
80 # Position the coin
81 coin.center_x = random.randrange(SCREEN_WIDTH)
82 coin.center_y = random.randrange(SCREEN_HEIGHT)
83
84 # Add the coin to the lists
85 self.coin_list.append(coin)
86
87 def on_draw(self):
88 """ Draw everything """
89
90 # Clear the screen to only show the background color
91 self.clear()
92
93 # Draw the sprites
94 self.coin_list.draw()
95 self.player_list.draw()
96
97 # Draw the score Text object on the screen
98 self.score_display.draw()
99
100 def on_mouse_motion(self, x, y, dx, dy):
101 """ Handle Mouse Motion """
102
103 # Move the player sprite to place its center on the mouse x, y
104 self.player_sprite.position = x, y
105
106 def on_update(self, delta_time):
107 """ Movement and game logic """
108
109 # Generate a list of all sprites that collided with the player.
110 coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
111 self.coin_list)
112
113 # Keep track of the score from before collisions occur
114 old_score = self.score
115
116 # Loop through each colliding sprite, remove it, and add to the score.
117 for coin in coins_hit_list:
118 coin.remove_from_sprite_lists()
119 self.score += 1
120
121 # Update the score display if the score changed this tick
122 if old_score != self.score:
123 self.score_display.text = f"Score: {self.score}"
124
125
126def main():
127 """ Main function """
128 window = MyGame()
129 window.setup()
130 arcade.run()
131
132
133if __name__ == "__main__":
134 main()