What’s a Sprite?#
Each sprite describes where a game object is & how to draw it. This includes:
Where it is in the world
Where to find the image data
How big the image should be
The rest of this page will explain using the SpriteList
class to draw
sprites to the screen.
Why SpriteLists?#
They’re How Hardware Works#
Graphics hardware is designed to draw groups of objects at the same time. These groups are called batches.
Each SpriteList
automatically translates every Sprite
in it
into an optimized batch. It doesn’t matter if a batch has one or hundreds of
sprites: it still takes the same amount of time to draw!
This means that using fewer batches helps your game run faster, and that you should avoid trying to draw sprites one at a time.
They Help Develop Games Faster#
Sprite lists do more than just draw. They also have built-in features which save you time & effort, including:
Automatically skipping off-screen sprites
Collision detection
Debug drawing for hit boxes
Drawing with Sprites and SpriteLists#
Let’s get to the example code.
There are 3 steps to drawing sprites with a sprite list:
Create a
SpriteList
Create & append your
Sprite
instance(s) to the list
Here’s a minimal example:
1"""
2Minimal Sprite Example
3
4Draws a single sprite in the middle screen.
5
6If Python and Arcade are installed, this example can be run from the command line with:
7python -m arcade.examples.sprite_minimal
8"""
9from __future__ import annotations
10
11import arcade
12
13
14class WhiteSpriteCircleExample(arcade.Window):
15
16 def __init__(self):
17 super().__init__(800, 600, "White SpriteCircle Example")
18 self.sprites = None
19 self.setup()
20
21 def setup(self):
22 # 1. Create the SpriteList
23 self.sprites = arcade.SpriteList()
24
25 # 2. Create & append your Sprite instance to the SpriteList
26 self.circle = arcade.SpriteCircle(30, arcade.color.WHITE) # 30 pixel radius circle
27 self.circle.position = self.width // 2, self.height // 2 # Put it in the middle
28 self.sprites.append(self.circle) # Append the instance to the SpriteList
29
30 def on_draw(self):
31 # 3. Call draw() on the SpriteList inside an on_draw() method
32 self.sprites.draw()
33
34
35if __name__ == "__main__":
36 game = WhiteSpriteCircleExample()
37 game.run()
Using Images with Sprites#
Beginners should see the following to learn more, such as how to load images into sprites:
The
Sprite
API documentation
Viewports, Cameras, and Screens#
Intermediate users can move past the limitations of arcade.Window
with the following classes:
arcade.Camera
(examples) to control which part of game space is drawnarcade.View
(examples) for start, end, and menu screens