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"""
9import arcade
10
11
12class WhiteSpriteCircleExample(arcade.Window):
13
14 def __init__(self):
15 super().__init__(800, 600, "White SpriteCircle Example")
16 self.sprites = None
17 self.setup()
18
19 def setup(self):
20 # 1. Create the SpriteList
21 self.sprites = arcade.SpriteList()
22
23 # 2. Create & append your Sprite instance to the SpriteList
24 self.circle = arcade.SpriteCircle(30, arcade.color.WHITE) # 30 pixel radius circle
25 self.circle.position = self.width // 2, self.height // 2 # Put it in the middle
26 self.sprites.append(self.circle) # Append the instance to the SpriteList
27
28 def on_draw(self):
29 # 3. Call draw() on the SpriteList inside an on_draw() method
30 self.sprites.draw()
31
32
33if __name__ == "__main__":
34 game = WhiteSpriteCircleExample()
35 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