Skip to content

Commit 65b5662

Browse files
jeramyrdDickerson
andauthored
Optional update for the platform tutorial. (#2690)
* first change to test * condense install section * condense install section - format 2 * testing an external link * install and step 1 finalized * step_02 done * fixing ~~~ thing --------- Co-authored-by: Dickerson <[email protected]>
1 parent 5630210 commit 65b5662

File tree

5 files changed

+177
-59
lines changed

5 files changed

+177
-59
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
default_language_version:
2-
python: python3.10
2+
python: python3.13
33

44
repos:
55
- repo: https://github.com/pre-commit/pre-commit-hooks

doc/_archive/install/windows.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ Windows
22
=======
33

44
To develop with the Arcade library, we need to install Python, then install
5-
Arcade.
5+
Arcade. If you are comfortable setting things up and know how to run a
6+
command prompt, this is a decent guide to setting things up. If the idea
7+
of a virtual environment is new, a better guide for setting up Python is
8+
here: `Your Python Coding Environment on Windows: Setup Guide <https://realpython.com/python-coding-setup-windows/>`_
69

710
Step 1: Install Python
811
----------------------

doc/get_started/install.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Install
88

99
.. _install_requirements:
1010

11+
.. note:: These steps require some basic computer admin knowledge.
12+
13+
If you are comfortable installing programs and know how to run a
14+
command prompt, this is a decent guide to setting things up. If the idea
15+
of a virtual environment is new, a better guide for setting up Python is
16+
here: `Your Python Coding Environment on Windows: Setup Guide <https://realpython.com/python-coding-setup-windows/>`_
17+
1118
Requirements
1219
------------
1320
Arcade requires a desktop, laptop, or compatible Single-Board Computer (SBC) with:

doc/tutorials/platform_tutorial/step_01.rst

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,36 @@
44
Step 1 - Install and Open a Window
55
----------------------------------
66

7-
Our first step is to make sure everything is installed, and that we can at least
8-
get a window open.
7+
You will do two things in this section.
8+
9+
1) Run the code from the arcade module directly. This will open a simple window.
10+
11+
2) Create your own file with the copied code to work on for the rest of the tutorial.
12+
Run it, you will get the same window, but THIS copy you control!
913

1014
Installation
1115
~~~~~~~~~~~~
12-
* Make sure Python is installed. `Download Python here <https://www.python.org/downloads/>`_
13-
if you don't already have it.
16+
For any of this to work, you need Python with the Arcade module installed.
17+
18+
* Setup instruction are here: :ref:`install`.
19+
20+
Verify the Installation (Step 1)
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
23+
Run the following code from a terminal in the folder that contains the arcade directory.
1424

15-
* Make sure the `Arcade library <https://pypi.org/project/arcade/>`_ is installed.
25+
.. code-block::
1626
17-
* You should first setup a virtual environment (venv) and activate it.
18-
* Install Arcade with ``pip install arcade``.
19-
* Here are the longer, official :ref:`install`.
27+
python -m arcade.examples.platform_tutorial.01_open_window
2028
21-
Open a Window
22-
~~~~~~~~~~~~~
29+
You should end up with a window like this:
2330

24-
The example below opens up a blank window. Set up a project and get the code
25-
below working.
31+
.. image:: step_01.png
32+
:width: 75%
33+
34+
The window is pretty useless... You can minimize it and even close it! However, if you got
35+
this going, you have succeeded in what is generally the hardest part of any new project
36+
- getting the environment setup! Congratulations! (Commence party dance sequence!)
2637

2738
.. note::
2839

@@ -31,14 +42,78 @@ below working.
3142
interesting things we can do first. Therefore we'll stick with a fixed-size
3243
window for this tutorial.
3344

45+
46+
Coding...The fun part! (Step 2)
47+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48+
You also need a code editor. There are dozens of options and we can't keep up with them
49+
here. A simple text editor could work, but better options abound. A web search is your
50+
friend - search for "Python editor" and pick one that looks good. Visual Studio Code
51+
(VS Code) is a nice free option. PyCharm is another.
52+
53+
54+
* Open your editor of choice and make a new file, call it main.py (the name is important!)
55+
* Copy this code into it.
56+
3457
.. literalinclude:: ../../../arcade/examples/platform_tutorial/01_open_window.py
3558
:caption: 01_open_window.py - Open a Window
3659
:linenos:
3760

38-
You should end up with a window like this:
61+
* Run the code from the same environment and in the folder with the code:
3962

40-
.. image:: step_01.png
41-
:width: 75%
63+
.. code-block::
64+
65+
python main.py
66+
67+
What is this code?
68+
~~~~~~~~~~~~~~~~~~
69+
70+
Explaining in depth every part of the code would be tedious and pretty overwhelming,
71+
and ultimatly not the most useful way to learn. But knowing the intent of sections is good!
72+
73+
Comments
74+
Triple quotations mark the begin and end of comments - sections of
75+
code that the computer ignores. It is for us humans!
76+
A second way of making a comment is the '#' symbol which lets python know to
77+
ignore everything on that line after the symbol.
78+
79+
import arcade
80+
This tells python which tools it needs to use to run the code. The magic sauce that does
81+
all the heavy lifting for the program so that you can focus on being creative!
82+
83+
# Constants section
84+
Remember how we said the window is a fixed size? Can you guess WHAT size it is fixed to
85+
from these lines? Did you notice a title on the window (go ahead and peek!).
86+
87+
class GameView(arcade.Window):
88+
Classes are extremely important programming concepts. In simple terms, it is a collection
89+
of data that you do things with. It keeps things organized!
90+
91+
def __init__(self):
92+
Data sets that make up class objects need initial information. You provide that here.
93+
94+
def setup(self):
95+
pass? what are we passing? Well, nothing, this is a place holder for future awesome code.
96+
pass tells Python that the function does nothing at the moment and it can move along.
97+
98+
def on_draw(self):
99+
Right now this function runs a simple command - self.clear(). Notice all the comments! That
100+
is so that you (the coder) knows what is going on in the function. More on this section to follow!
101+
102+
def main():
103+
This is the entry point for Python. When you run a program, Python looks for a main() function
104+
and runs it. Then three steps happen:
105+
106+
1) window = GameView() -> Your window object is created. Your __init__ function is automatically called.
107+
2) window.setup() - > You run the window object's setup function... Which does nothing at the moment (pass!)
108+
3) arcade.run() -> You turn on the engine and see the results. The program stays here until the
109+
window is closed. Then with nothing else to do, the program will terminate.
110+
111+
if __name__ == "__main__":
112+
If Python was run by starting this file in particular, then its "__name__" value will be "__main__".
113+
And the main() function will be called. Remember how we said the name of the file was important?
114+
115+
Challenge Exercises
116+
~~~~~~~~~~~~~~~~~~~
42117

43118
Once you get the code working, try figuring out how to adjust the code so you can:
44119

@@ -51,10 +126,8 @@ Once you get the code working, try figuring out how to adjust the code so you ca
51126

52127
* Look through the documentation for the :class:`arcade.Window`
53128
class to get an idea of everything it can do.
54-
55-
Run This Chapter
56-
~~~~~~~~~~~~~~~~
57-
58-
.. code-block::
59-
60-
python -m arcade.examples.platform_tutorial.01_open_window
129+
* Break it! Yes this sounds bad, but comment out some sections of the code and try to run it.
130+
You will make mistakes and this can help you get familure with error messages. Also, see if
131+
you can see in the error message WHERE the problem exists or other such clues. You already
132+
know because you made them... Make sure you save the good code and it is functional again
133+
before moving on.

doc/tutorials/platform_tutorial/step_02.rst

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,46 @@
33
Step 2 - Textures and Sprites
44
-----------------------------
55

6-
Our next step in this tutorial is to draw something on the Screen. In order to
7-
do that we need to cover two topics, Textures and Sprites.
6+
Our next step in this tutorial is to draw something on the Screen.
87

9-
At the end of this chapter, we'll have something that looks like this. It's largely the
10-
same as last chapter, but now we are drawing a character onto the screen:
8+
To see what we will accomplish, run this code:
9+
10+
.. code-block::
11+
12+
python -m arcade.examples.platform_tutorial.02_draw_sprites
13+
14+
15+
You should see the same window, but now with a character on the screen:
1116

1217
.. image:: images/title_02.png
1318
:width: 70%
1419

20+
Images in 2D games are created using Textures and Sprites. Let's discuss those ideas.
21+
1522
Textures
1623
~~~~~~~~
1724

1825
Textures are largely just an object to contain image data. Whenever you load an image
19-
file in Arcade, for example a ``.png`` or ``.jpeg`` file. It becomes a Texture.
26+
file in Arcade, for example a ``.png`` or ``.jpeg`` file, it becomes a Texture.
2027

2128
To do this, internally Arcade uses Pyglet to load the image data, and the texture is
2229
responsible for keeping track of this image data.
2330

2431
We can create a texture with a simple command, this can be done inside of our ``__init__``
25-
function. Go ahead and create a texture that we will use to draw a player.
32+
function. Go ahead and create a texture that we will use to draw a player by adding this code
33+
into the __init__ function of the GameView class. Right below the "super()..." statement is fine.
2634

2735
.. code-block::
2836
2937
self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")
3038
39+
What is the code doing?
40+
the 'self.' part the of statement officially attaches the stuff that comes behind it to become part
41+
of the class it is attached to. In this case, ANY function inside GameView now has access to a
42+
variable self.player_texture. That is how we 'share' the class data with the class methods (functions).
43+
arcade.load_texture() does all the dirty work of accepting a path to a image file and making it
44+
useful for the program.
45+
3146
.. note::
3247

3348
You might be wondering where this image file is coming from? And what is ``:resources:`` about?
@@ -46,12 +61,15 @@ function. Go ahead and create a texture that we will use to draw a player.
4661
Sprites
4762
~~~~~~~
4863

49-
If Textures are an instance of a particular image, then :class:`arcade.Sprite` is an instance of that image
50-
on the screen. Say we have a ground or wall texture. We only have one instance of the texture, but we can create
51-
multiple instances of Sprite, because we want to have many walls. These will use the same texture, but draw it
52-
in different positions, or even with different scaling, rotation, or colors/post-processing effects.
64+
While the texture data is now 'saved' into the class as a variable. We can't use it as is, we need to convert it
65+
to a Sprite. If Textures are an instance of a particular image from a file, then :class:`arcade.Sprite` is an
66+
instance of that image that can be put on the screen later on. Say we have a ground or wall texture. We only have
67+
one instance of the texture, but we can create multiple instances of Sprite, because we want to have many walls.
68+
These will use the same texture, but draw it in different positions, or even with different scaling, rotation,
69+
or colors/post-processing effects.
5370

54-
Creating a Sprite is simple, we can make one for our player in our ``__init__`` function, and then set it's position.
71+
Creating a Sprite is simple, we can make one for our player in our ``__init__`` function. Make sure it is
72+
right after the previous statement. See the challenge section for why this is important!
5573

5674
.. code-block::
5775
@@ -61,16 +79,54 @@ Creating a Sprite is simple, we can make one for our player in our ``__init__``
6179
6280
.. note::
6381

64-
You can also skip ``arcade.load_texture`` from the previous step and pass the image file to ``arcade.Sprite`` in place of the Texture object.
82+
You can also skip ``arcade.load_texture`` from the previous step and pass the image file to ``arcade.Sprite`` in place of the Texture object.
6583
A Texture will automatically be created for you. However, it may desirable in larger projects to manage your textures directly.
6684

67-
Now we can draw the sprite by adding this to our ``on_draw`` function:
85+
86+
Rendering
87+
~~~~~~~~~
88+
89+
If you ran your program as is, you will notice.... nothing new! We have simply given the class a
90+
texture and defined a sprite. But no instructions on what to do with that data. Remember, classes are objects
91+
that HAVE data (our image in sprite-form now) and DO stuff with the data.
92+
93+
Rendering is how we get our cool Sprite onto our window by adding the next command to our ``on_draw`` function. Place it under the
94+
"# Code to draw other things will go here" comment.
6895

6996
.. code-block::
7097
98+
# Code to draw other things will go here
7199
arcade.draw_sprite(self.player_sprite)
72100
73-
We're now drawing a Sprite to the screen! In the next chapter, we will introduce techniques to draw many(even hundreds of thousands) sprites at once.
101+
Now run the code! You will have to remember the code to run your program from now on :)
102+
103+
.. code-block::
104+
105+
python main.py
106+
107+
Challenge
108+
~~~~~~~~~
109+
110+
* Play with the center_x and center_y variables - what do they do?
111+
* Move the load.texture statement after the arcade.Sprite(self.player_texture) statement and run it.
112+
It fails. Why? Well, its because computers do things in order. If you try to use a variable before
113+
defining it, you will get an error. Computers don't try to guess what you want or bother looking
114+
around for the item. If it is not there, it instantly gives up.
115+
* See if you can find the location of the resources and use a different image file.
116+
* use a loop to create several copies of the image to the window - each with different locations. Hint:
117+
add the 'import random' module (another toolbox for Python) right under the import arcade statement.
118+
With the random module you can use random.randint(X, Y) which will give a random number between
119+
X and Y (including possibly X and Y).
120+
* EXTREME challenge - make your own image file - even a stick figure! And use that instead. paint.net
121+
is a good free option. aseprite.org is a low cost option and esotericsoftware.com has Spine for a
122+
more expensive option. MANY others exist. Don't spend too much time on this though :) Just enough to
123+
get your creative juices flowing! Maybe just use a picture of your face!
124+
* Explore the documentation for the :class:`arcade.Texture` class
125+
* Explore the documentation for the :class:`arcade.Sprite` class
126+
127+
Once you are finished with any or all challenges - make sure your code matches this so that future
128+
tutorial steps still work! Feel free to comment out sections of custom code you want to keep playing
129+
with later.
74130

75131
Source Code
76132
~~~~~~~~~~~
@@ -79,24 +135,3 @@ Source Code
79135
:caption: 02_draw_sprites - Draw and Position Sprites
80136
:linenos:
81137
:emphasize-lines: 24-30, 44-45
82-
83-
Running this code should result in a character being drawn on the screen, like in
84-
the image at the start of the chapter.
85-
86-
* Documentation for the :class:`arcade.Texture` class
87-
* Documentation for the :class:`arcade.Sprite` class
88-
89-
.. note::
90-
91-
Once you have the code up and working, try adjusting the code for the following:
92-
93-
* Adjust the code and try putting the sprite in new positions(Try setting the positions using other attributes of Sprite)
94-
* Use different images for the texture (see :ref:`resources` for the built-in images, or try using your own images.)
95-
* Practice placing more sprites in different ways(like placing many with a loop)
96-
97-
Run This Chapter
98-
~~~~~~~~~~~~~~~~
99-
100-
.. code-block::
101-
102-
python -m arcade.examples.platform_tutorial.02_draw_sprites

0 commit comments

Comments
 (0)