8000 done with step_03.rst (#2693) · pythonarcade/arcade@ef46798 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef46798

Browse files
jeramyrdDickerson
andauthored
done with step_03.rst (#2693)
Co-authored-by: Dickerson <jrdicke@sandia.gov>
1 parent 1c4d3d5 commit ef46798

File tree

5 files changed

+73
-34
lines changed

5 files changed

+73
-34
lines changed

arcade/examples/platform_tutorial/03_more_sprites.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self):
4949
# Create the ground
5050
# This shows using a loop to place multiple sprites horizontally
5151
for x in range(0, 1250, 64):
52-
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=0.5)
52+
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=TILE_SCALING)
5353
wall.center_x = x
5454
wall.center_y = 32
5555
self.wall_list.append(wall)
@@ -61,8 +61,7 @@ def __init__(self):
6161
for coordinate in coordinate_list:
6262
# Add a crate on the ground
6363
wall = arcade.Sprite(
64-
":resources:images/tiles/boxCrate_double.png", scale=0.5
65-
)
64+
":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING)
6665
wall.position = coordinate
6766
self.wall_list.append(wall)
6867

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def run_util(filename, run_name="__main__", init_globals=None):
196196
'sphinx.ext.viewcode', # display code with line numbers and line highlighting
197197
'sphinx_copybutton', # Adds a copy button to code blocks
198198
'sphinx_sitemap', # sitemap.xml generation
199+
'sphinx_togglebutton', #A way to toggle sections of text/code on or off.
199200
'doc.extensions.prettyspecialmethods', # Forker plugin for prettifying special methods
200201
]
201202

doc/tutorials/platform_tutorial/step_02.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ Source Code
134134
.. literalinclude:: ../../../arcade/examples/platform_tutorial/02_draw_sprites.py
135135
:caption: 02_draw_sprites - Draw and Position Sprites
136136
:linenos:
137-
:emphasize-lines: 24-30, 44-45
137+
:emphasize-lines: 24-32, 46-47

doc/tutorials/platform_tutorial/step_03.rst

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@ At the end, we'll have something like this:
1515
SpriteList
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`
2328
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.
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
3338
it is never better to draw an individual Sprite than it is to add it to a SpriteList. In fact, calling ``draw()``
3439
on 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 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.
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+
4972
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.
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

5884
1. 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

6593
2. 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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dev = [
4343
"sphinx-autobuild==2024.10.3", # April 2024 | Due to this, Python 3.10+ is required to serve docs
4444
"sphinx-copybutton==0.5.2", # April 2023
4545
"sphinx-sitemap==2.6.0", # April 2024
46+
"sphinx-togglebutton==0.3.2", # May 2025
4647
"pygments==2.19.1", # 2.18 has breaking changes in lexer
4748
"docutils==0.21.2", # ?
4849
# "pyyaml==6.0.1",

0 commit comments

Comments
 (0)
0