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:

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:
arcade.draw_sprite(self.player_sprite)
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
1"""
2Platformer Game
3
4python -m arcade.examples.platform_tutorial.02_draw_sprites
5"""
6import arcade
7
8# Constants
9WINDOW_WIDTH = 1280
10WINDOW_HEIGHT = 720
11WINDOW_TITLE = "Platformer"
12
13
14class GameView(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__(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
23
24 # Variable to hold our texture for our player
25 self.player_texture = arcade.load_texture(
26 ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
27 )
28
29 # Separate variable that holds the player sprite
30 self.player_sprite = arcade.Sprite(self.player_texture)
31 self.player_sprite.center_x = 64
32 self.player_sprite.center_y = 128
33
34 self.background_color = arcade.csscolor.CORNFLOWER_BLUE
35
36 def setup(self):
37 """Set up the game here. Call this function to restart the game."""
38 pass
39
40 def on_draw(self):
41 """Render the screen."""
42
43 # Clear the screen to the background color
44 self.clear()
45
46 # Draw our sprites
47 arcade.draw_sprite(self.player_sprite)
48
49
50def main():
51 """Main function"""
52 window = GameView()
53 window.setup()
54 arcade.run()
55
56
57if __name__ == "__main__":
58 main()
Running this code should result in a character being drawn on the screen, like in the image at the start of the chapter.
Documentation for the
arcade.Texture
classDocumentation for the
arcade.Sprite
class
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