Using a Background Image#

Screenshot of using sprites to collect coins and a background image
sprite_collect_coins_background.py#
  1"""
  2Sprite Collect Coins with Background
  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_background
 10"""
 11from __future__ import annotations
 12
 13import random
 14import arcade
 15
 16PLAYER_SCALING = 0.5
 17COIN_SCALING = 0.25
 18
 19SCREEN_WIDTH = 1024
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprite Collect Coins with Background Example"
 22
 23
 24class MyGame(arcade.Window):
 25    """
 26    Main application class.
 27    """
 28
 29    def __init__(self, width, height, title):
 30        """ Initializer """
 31
 32        # Call the parent class initializer
 33        super().__init__(width, height, title)
 34
 35        # Background image will be stored in this variable
 36        self.background = None
 37
 38        # Variables that will hold sprite lists
 39        self.player_list = None
 40        self.coin_list = None
 41
 42        # Set up the player info
 43        self.player_sprite = None
 44        self.score = 0
 45        self.score_text = None
 46
 47        # Don't show the mouse cursor
 48        self.set_mouse_visible(False)
 49
 50        # Set the background color
 51        self.background_color = arcade.color.AMAZON
 52
 53    def setup(self):
 54        """ Set up the game and initialize the variables. """
 55
 56        # Load the background image. Do this in the setup so we don't keep reloading it all the time.
 57        # Image from:
 58        # https://wallpaper-gallery.net/single/free-background-images/free-background-images-22.html
 59        self.background = arcade.load_texture(":resources:images/backgrounds/abstract_1.jpg")
 60
 61        # Sprite lists
 62        self.player_list = arcade.SpriteList()
 63        self.coin_list = arcade.SpriteList()
 64
 65        # Set up the player
 66        self.score = 0
 67        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 68                                           scale=PLAYER_SCALING)
 69        self.player_sprite.center_x = 50
 70        self.player_sprite.center_y = 50
 71        self.player_list.append(self.player_sprite)
 72
 73        for i in range(50):
 74
 75            # Create the coin instance
 76            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=COIN_SCALING)
 77
 78            # Position the coin
 79            coin.center_x = random.randrange(SCREEN_WIDTH)
 80            coin.center_y = random.randrange(SCREEN_HEIGHT)
 81
 82            # Add the coin to the lists
 83            self.coin_list.append(coin)
 84
 85    def on_draw(self):
 86        """
 87        Render the screen.
 88        """
 89
 90        # This command has to happen before we start drawing
 91        self.clear()
 92
 93        # Draw the background texture
 94        arcade.draw_lrwh_rectangle_textured(0, 0,
 95                                            SCREEN_WIDTH, SCREEN_HEIGHT,
 96                                            self.background)
 97
 98        # Draw all the sprites.
 99        self.coin_list.draw()
100        self.player_list.draw()
101
102        # Render the text
103        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
104
105    def on_mouse_motion(self, x, y, dx, dy):
106        """
107        Called whenever the mouse moves.
108        """
109        self.player_sprite.center_x = x
110        self.player_sprite.center_y = y
111
112    def on_update(self, delta_time):
113        """ Movement and game logic """
114
115        # Call update on the coin sprites (The sprites don't do much in this
116        # example though.)
117        self.coin_list.update()
118
119        # Generate a list of all sprites that collided with the player.
120        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
121
122        # Loop through each colliding sprite, remove it, and add to the score.
123        for coin in hit_list:
124            coin.remove_from_sprite_lists()
125            self.score += 1
126
127
128def main():
129    """ Main function """
130    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
131    window.setup()
132    arcade.run()
133
134
135if __name__ == "__main__":
136    main()