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