Step 2 - Textures and Sprites#

Our next step in this tutorial is to draw something on the Screen. In order to do that we need to cover two topics, Textures and Sprites.

At the end of this chapter, we’ll have something that looks like this. It’s largely the same as last chapter, but now we are drawing a character onto the screen:

../../_images/title_02.png

Textures#

Textures are largely just an object to contain image data. Whenever you load an image file in Arcade, for example a .png or .jpeg file. It becomes a Texture.

To do this, internally Arcade uses Pyglet to load the image data, and the texture is responsible for keeping track of this image data.

We can create a texture with a simple command, this can be done inside of our __init__ function. Go ahead and create a texture that we will use to draw a player.

self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")

Note

You might be wondering where this image file is coming from? And what is :resources: about?

The :resources: section of the string above is what Arcade calls a resource handle. You can register your own resource handles to different asset directories. For example you might want to have a :characters: and a :objects: handle.

However, you don’t have to use a resource handle here, anywhere that you can load files in Arcade will accept resource handles, or just strings to filepaths, or Path objects from pathlib

Arcade includes the :resources: handle with a bunch of built-in assets from kenney.

For more information checkout Built-In Resources

Sprites#

If Textures are an instance of a particular image, then arcade.Sprite is an instance of that image on the screen. Say we have a ground or wall texture. We only have one instance of the texture, but we can create multiple instances of Sprite, because we want to have many walls. These will use the same texture, but draw it in different positions, or even with different scaling, rotation, or colors/post-processing effects.

Creating a Sprite is simple, we can make one for our player in our __init__ function, and then set it’s position.

self.player_sprite = arcade.Sprite(self.player_texture)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 128

Note

You can also skip arcade.load_texture from the previous step and pass the image file to arcade.Sprite in place of the Texture object. A Texture will automatically be created for you. However, it may desirable in larger projects to manage your textures directly.

Now we can draw the sprite by adding this to our on_draw function:

self.player_sprite.draw()

We’re now drawing a Sprite to the screen! In the next chapter, we will introduce techniques to draw many(even hundreds of thousands) sprites at once.

Source Code#

02_draw_sprites - Draw and Position Sprites#
 1"""
 2Platformer Game
 3
 4python -m arcade.examples.platform_tutorial.02_draw_sprites
 5"""
 6import arcade
 7
 8# Constants
 9SCREEN_WIDTH = 800
10SCREEN_HEIGHT = 600
11SCREEN_TITLE = "Platformer"
12
13
14class MyGame(arcade.Window):
15    """
16    Main application class.
17    """
18
19    def __init__(self):
20
21        # Call the parent class and set up the window
22        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
23
24        # Variable to hold our texture for our player
25        self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")
26
27        # Separate variable that holds the player sprite
28        self.player_sprite = arcade.Sprite(self.player_texture)
29        self.player_sprite.center_x = 64
30        self.player_sprite.center_y = 128
31
32        self.background_color = arcade.csscolor.CORNFLOWER_BLUE
33
34    def setup(self):
35        """Set up the game here. Call this function to restart the game."""
36        pass
37
38    def on_draw(self):
39        """Render the screen."""
40
41        # Clear the screen to the background color
42        self.clear()
43
44        # Draw our sprites
45        self.player_sprite.draw()
46
47
48def main():
49    """Main function"""
50    window = MyGame()
51    window.setup()
52    arcade.run()
53
54
55if __name__ == "__main__":
56    main()

Running this code should result in a character being drawn on the screen, like in the image at the start of the chapter.

Note

Once you have the code up and working, try adjusting the code for the following:

  • Adjust the code and try putting the sprite in new positions(Try setting the positions using other attributes of Sprite)

  • Use different images for the texture (see Built-In Resources for the built-in images, or try using your own images.)

  • Practice placing more sprites in different ways(like placing many with a loop)

Run This Chapter#

python -m arcade.examples.platform_tutorial.02_draw_sprites