@@ -15,16 +15,21 @@ At the end, we'll have something like this:
1515SpriteList
1616~~~~~~~~~~
1717
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 `
2328and 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.
2530
2631.. 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.
2833 This means we can send all the information about our sprites to the GPU, and then tell it to draw them all
2934 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
3035 our GPU every time.
@@ -33,34 +38,57 @@ Even if you are only drawing one Sprite, you should still create a SpriteList fo
3338it is never better to draw an individual Sprite than it is to add it to a SpriteList. In fact, calling ``draw() ``
3439on a Sprite just creates a SpriteList internally to draw that Sprite with.
3540
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+
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.
3748
3849.. code-block ::
3950
4051 self.player_list = arcade.SpriteList()
4152 self.player_list.append(self.player_sprite)
4253
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:
4462
4563.. code-block ::
4664
4765 self.player_list.draw()
4866
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+
4972Now 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.
5174
5275.. code-block ::
5376
5477 self.wall_list = arcade.SpriteList(use_spatial_hash=True)
5578
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!
5783
58841. Why not just use the same SpriteList we used for our player, and why is it named walls?
5985
6086 Eventually we will want to do collision detection between our character and these objects.
6187 In addition to drawing, SpriteLists also serve as a utility for collision detection. You can
6288 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'
6492
65932. What is ``use_spatial_hash ``?
6694
@@ -74,16 +102,15 @@ With our newly created SpriteList, let's go ahead and add some objects to it. We
74102.. code-block ::
75103
76104 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)
78106 wall.center_x = x
79107 wall.center_y = 32
80108 self.wall_list.append(wall)
81109
82110 coordinate_list = [[512, 96], [256, 96], [768, 96]]
83111 for coordinate in coordinate_list:
84112 wall = arcade.Sprite(
85- ":resources:images/tiles/boxCrate_double.png", scale=0.5
86- )
113+ ":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING)
87114 wall.position = coordinate
88115 self.wall_list.append(wall)
89116
@@ -103,26 +130,37 @@ Finally all we need to do in order to draw our new world, is draw the SpriteList
103130
104131 self.wall_list.draw()
105132
106- Source Code
107- ~~~~~~~~~~~
133+ Run the code and... wait the image is instantly disappearing! What did we do wrong? Try and debug it!
108134
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 ::
113136
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.
115139
116- .. note ::
140+ Solution: add this up by the '# constants' section
141+ TILE_SCALING = 0.5
117142
118- Once you have the code up and working, try-out the following:
119143
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.
122154
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:
125157
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
127165
128- python -m arcade.examples.platform_tutorial.03_more_sprites
166+ * Documentation for the :class: ` arcade.SpriteList ` class
0 commit comments