Different Levels of Clearing Coins#

Screenshot of using sprites to collect coins
sprite_collect_coins_diff_levels.py#
  1"""
  2Sprite Collect Coins with Different Levels
  3
  4Simple program to show basic sprite usage.
  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_collect_coins_diff_levels
 10"""
 11
 12from __future__ import annotations
 13
 14import random
 15import arcade
 16
 17SPRITE_SCALING = 0.5
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprite Collect Coins with Different Levels Example"
 22
 23
 24class FallingCoin(arcade.Sprite):
 25    """ Simple sprite that falls down """
 26
 27    def update(self):
 28        """ Move the coin """
 29
 30        # Fall down
 31        self.center_y -= 2
 32
 33        # Did we go off the screen? If so, pop back to the top.
 34        if self.top < 0:
 35            self.bottom = SCREEN_HEIGHT
 36
 37
 38class RisingCoin(arcade.Sprite):
 39    """ Simple sprite that falls up """
 40
 41    def update(self):
 42        """ Move the coin """
 43
 44        # Move up
 45        self.center_y += 2
 46
 47        # Did we go off the screen? If so, pop back to the bottom.
 48        if self.bottom > SCREEN_HEIGHT:
 49            self.top = 0
 50
 51
 52class MyGame(arcade.Window):
 53    """
 54    Main application class.
 55    """
 56
 57    def __init__(self, width, height, title):
 58        """ Initialize """
 59
 60        # Call the parent class initializer
 61        super().__init__(width, height, title)
 62
 63        # Variables that will hold sprite lists
 64        self.player_list = None
 65        self.coin_list = None
 66
 67        # Set up the player info
 68        self.player_sprite = None
 69        self.score = 0
 70
 71        self.level = 1
 72
 73        # Don't show the mouse cursor
 74        self.set_mouse_visible(False)
 75
 76        # Set the background color
 77        self.background_color = arcade.color.AMAZON
 78
 79    def level_1(self):
 80        for i in range(20):
 81
 82            # Create the coin instance
 83            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=SPRITE_SCALING / 3)
 84
 85            # Position the coin
 86            coin.center_x = random.randrange(SCREEN_WIDTH)
 87            coin.center_y = random.randrange(SCREEN_HEIGHT)
 88
 89            # Add the coin to the lists
 90            self.coin_list.append(coin)
 91
 92    def level_2(self):
 93        for i in range(30):
 94
 95            # Create the coin instance
 96            coin = FallingCoin(":resources:images/items/coinBronze.png", scale=SPRITE_SCALING / 2)
 97
 98            # Position the coin
 99            coin.center_x = random.randrange(SCREEN_WIDTH)
100            coin.center_y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT * 2)
101
102            # Add the coin to the lists
103            self.coin_list.append(coin)
104
105    def level_3(self):
106        for i in range(30):
107
108            # Create the coin instance
109            coin = RisingCoin(":resources:images/items/coinSilver.png", scale=SPRITE_SCALING / 2)
110
111            # Position the coin
112            coin.center_x = random.randrange(SCREEN_WIDTH)
113            coin.center_y = random.randrange(-SCREEN_HEIGHT, 0)
114
115            # Add the coin to the lists
116            self.coin_list.append(coin)
117
118    def setup(self):
119        """ Set up the game and initialize the variables. """
120
121        self.score = 0
122        self.level = 1
123
124        # Sprite lists
125        self.player_list = arcade.SpriteList()
126        self.coin_list = arcade.SpriteList()
127
128        # Set up the player
129        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
130                                           scale=SPRITE_SCALING)
131        self.player_sprite.center_x = 50
132        self.player_sprite.center_y = 50
133        self.player_list.append(self.player_sprite)
134
135        self.level_1()
136
137    def on_draw(self):
138        """
139        Render the screen.
140        """
141
142        # This command has to happen before we start drawing
143        self.clear()
144
145        # Draw all the sprites.
146        self.player_sprite.draw()
147        self.coin_list.draw()
148
149        # Put the text on the screen.
150        output = f"Score: {self.score}"
151        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 15)
152
153        output = f"Level: {self.level}"
154        arcade.draw_text(output, 10, 35, arcade.color.WHITE, 15)
155
156    def on_mouse_motion(self, x, y, dx, dy):
157        """
158        Called whenever the mouse moves.
159        """
160        self.player_sprite.center_x = x
161        self.player_sprite.center_y = y
162
163    def on_update(self, delta_time):
164        """ Movement and game logic """
165
166        # Call update on all sprites (The sprites don't do much in this
167        # example though.)
168        self.coin_list.update()
169
170        # Generate a list of all sprites that collided with the player.
171        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
172
173        # Loop through each colliding sprite, remove it, and add to the score.
174        for coin in hit_list:
175            coin.remove_from_sprite_lists()
176            self.score += 1
177
178        # See if we should go to level 2
179        if len(self.coin_list) == 0 and self.level == 1:
180            self.level += 1
181            self.level_2()
182        # See if we should go to level 3
183        elif len(self.coin_list) == 0 and self.level == 2:
184            self.level += 1
185            self.level_3()
186
187
188def main():
189    """ Main function """
190    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
191    window.setup()
192    arcade.run()
193
194
195if __name__ == "__main__":
196    main()