@@ -15,16 +15,21 @@ At the end, we'll have something like this:
15
15
SpriteList
16
16
~~~~~~~~~~
17
17
18
- :class: `arcade.SpriteList ` exists to draw a collection of Sprites all at once. Let's say for example that you have
19
- 100,000 box Sprites that you want to draw. Without SpriteList you would have to put all of your sprites into a list,
20
- and then run a for loop over that which calls ``draw() `` on every sprite.
21
-
22
- This approach is extremely un-performant. Instead, you can add all of your boxes to a :class: `arcade.SpriteList `
18
+ Imagine how you (as a human) would draw this picture. Maybe you would draw the character first. Then
19
+ the first box, then the second box, etc... Now a long strip of green and brown stuff we will call grass.
20
+ So likely your first thought to programming would be to make a loop over a list of sprites and call ``draw() ``
21
+ on every sprite, one at a time. Computers are fast right? Yes, but this approach is extremely inefficient.
22
+ See the note below on how Arcade uses GPUs. What if we could draw EVERY object at the same time! Computers
23
+ can surpass the frail limitiation of you mortal humans!
24
+
25
+ But to do that, the computer does need your help (and the computer humbly apologizes for the insult!).
26
+ :class: `arcade.SpriteList ` exists to draw a collection of Sprites all at once. Let's say for example that
27
+ you have 100,000 box Sprites that you want to draw. You can add all of your boxes to a :class: `arcade.SpriteList `
23
28
and then draw the SpriteList. Doing this, you are able to draw all 100,000 sprites for approximately the exact
24
- same cost as drawing one sprite.
29
+ same cost as drawing one sprite. Which is pretty amazing.
25
30
26
31
.. note ::
27
- This is due to Arcade being a heavily GPU based library. GPUs are really good at doing things in batches.
32
+ Arcade is a heavily GPU based library. GPUs are really good at doing things in batches.
28
33
This means we can send all the information about our sprites to the GPU, and then tell it to draw them all
29
34
at once. However if we just draw one sprite at a time, then we have to go on a round trip from our CPU to
30
35
our GPU every time.
@@ -33,34 +38,57 @@ Even if you are only drawing one Sprite, you should still create a SpriteList fo
33
38
it is never better to draw an individual Sprite than it is to add it to a SpriteList. In fact, calling ``draw() ``
34
39
on a Sprite just creates a SpriteList internally to draw that Sprite with.
35
40
36
- Let's go ahead and create one for our player inside our ``__init__ `` function, and add the player to it.
41
+ Quiz - where in our code should we place the data object of our :class: `arcade.SpriteList `?
42
+
43
+ .. toggle ::
44
+
45
+ If you guessed the ``__init__ `` function you are correct!
46
6D40
+
47
+ So that is where we are going to put the following code. Just add it to the bottom of the list of other data objects.
37
48
38
49
.. code-block ::
39
50
40
51
self.player_list = arcade.SpriteList()
41
52
self.player_list.append(self.player_sprite)
42
53
43
- Then in our ``on_draw `` function, we can draw the SpriteList for the character instead of drawing the Sprite directly:
54
+ Then in our ``on_draw `` function, we can draw the SpriteList for the character instead of drawing the Sprite directly.
55
+ So we will replace this line of code:
56
+
57
+ .. code-block ::
58
+
59
+ arcade.draw_sprite(self.player_sprite)
60
+
61
+ with this line of code:
44
62
45
63
.. code-block ::
46
64
47
65
self.player_list.draw()
48
66
67
+ Now run it and you get your character on the screen. All by herself... alone in an empty world... sad face.
68
+
69
+ Time to make the world!
70
+ ~~~~~~~~~~~~~~~~~~~~~~~
71
+
49
72
Now let's try and build a world for our character. To do this, we'll create a new SpriteList for the objects we'll draw,
50
- we can do this in our ``__init__ `` function.
73
+ we can do this again in our ``__init__ `` function.
51
74
52
75
.. code-block ::
53
76
54
77
self.wall_list = arcade.SpriteList(use_spatial_hash=True)
55
78
56
- There's a little bit to unpack in this snippet of code. Let's address each issue:
79
+ Wait, why did we make a new list of Sprites? I thought the idea was to make one master list of everything we needed to draw?
80
+ What is a spatial_hash and why is it True?
81
+
82
+ Great questions!
57
83
58
84
1. Why not just use the same SpriteList we used for our player, and why is it named walls?
59
85
60
86
Eventually we will want to do collision detection between our character and these objects.
61
87
In addition to drawing, SpriteLists also serve as a utility for collision detection. You can
62
88
for example check for collisions between two SpriteLists, or pass SpriteLists into several physics
63
- engines. We will explore these topics in later chapters.
89
+ engines. We will explore these topics in later chapters. And you will notice that we are going to
90
+ pass in pictures of grass and boxes - which we will treat as barriers. So they will be added to
91
+ the 'walls'
64
92
65
93
2. What is ``use_spatial_hash ``?
66
94
@@ -74,16 +102,15 @@ With our newly created SpriteList, let's go ahead and add some objects to it. We
74
102
.. code-block ::
75
103
76
104
for x in range(0, 1250, 64):
77
- wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
105
+ wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale= TILE_SCALING)
78
106
wall.center_x = x
79
107
wall.center_y = 32
80
108
self.wall_list.append(wall)
81
109
82
110
coordinate_list = [[512, 96], [256, 96], [768, 96]]
83
111
for coordinate in coordinate_list:
84
112
wall = arcade.Sprite(
85
- ":resources:images/tiles/boxCrate_double.png", scale=0.5
86
- )
113
+ ":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING)
87
114
wall.position = coordinate
88
115
self.wall_list.append(wall)
89
116
@@ -103,26 +130,37 @@ Finally all we need to do in order to draw our new world, is draw the SpriteList
103
130
104
131
self.wall_list.draw()
105
132
106
- Source Code
107
- ~~~~~~~~~~~
133
+ Run the code and... wait the image is instantly disappearing! What did we do wrong? Try and debug it!
108
134
109
- .. literalinclude :: ../../../arcade/examples/platform_tutorial/03_more_sprites.py
110
- :caption: 03_more_sprites - Many Sprites with a SpriteList
111
- :linenos:
112
- :emphasize-lines: 35-65, 80-81
135
+ .. toggle ::
113
136
114
- * Documentation for the :class: `arcade.SpriteList ` class
137
+ You might have noticed that we use TILE_SCALING but we never defined it! So the image starts creating, but crashes
138
+ before we get to even see what is wrong! This is a hard bug to find as there is no error message.
115
139
116
- .. note ::
140
+ Solution: add this up by the '# constants' section
141
+ TILE_SCALING = 0.5
117
142
118
- Once you have the code up and working, try-out the following:
119
143
120
- * See if you can change the colors of all the boxes and ground using the SpriteList
121
- * Try and make a SpriteList invisible
144
+ Challenge
145
+ ~~~~~~~~~
146
+ * Try changing the new variable TILE_SCALING, what does it do?
147
+ * Make some floating boxes. Do it either randomly or fixed.
148
+ * Read the documentation for the :class: `arcade.SpriteList ` class
149
+ * See if you can change the colors of all the boxes and ground using the SpriteList
150
+ * Try and make a SpriteList invisible
151
+ * EXTREME Challeng Our __init__ class is getting pretty bloated. The loops we created to make the walls would be better suited
152
+ to have their own function that we call. See if you can make a function 'def build_walls(self):' that has that same
153
+ code. Then call that function with build_walls() after you define the self.wall_list.
122
154
123
- Run This Chapter
124
- ~~~~~~~~~~~~~~~~
155
+ Your code should produce the same image as shown on the top of this page. Before proceeding, make sure you comment
156
+ out custom code so we are on the same page. Also, if you are still having problems, compare with this code:
125
157
126
- .. code-block ::
158
+ Source Code
159
+ ~~~~~~~~~~~
160
+
161
+ .. literalinclude :: ../../../arcade/examples/platform_tutorial/03_more_sprites.py
162
+ :caption: 03_more_sprites - Many Sprites with a SpriteList
163
+ :linenos:
164
+ :emphasize-lines: 13-14, 37-39, 41-47, 49-66, 80-82
127
165
128
- python -m arcade.examples.platform_tutorial.03_more_sprites
166
+ * Documentation for the :class: ` arcade.SpriteList ` class
0 commit comments