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