Skip to content

Commit 0547165

Browse files
committed
Merge branch 'dev'
2 parents 58383d0 + 013814f commit 0547165

File tree

19 files changed

+1701
-65
lines changed

19 files changed

+1701
-65
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ website/
2424

2525
#the release
2626
release/
27+
28+
#The website
29+
website/
File renamed without changes.

documentation/basic/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ <h1>chapters</h1>
99
<a href="lesson3.html">lesson 3: but what if</a>
1010
<a href="lesson4.html">lesson 4: wait, is that math?</a>
1111
<a href="lesson5.html">lesson 5: No, I didn't say my jewels</a>
12-
<a href="lesson6.html">lesson 6: game1</a>
13-
<a href="lesson7.html">lesson 7: balista!</a>
12+
<a href="lesson6.html">lesson 6: balista!</a>
13+
<a href="lesson7.html">lesson 7: game1</a>
1414
<a href="lesson8.html">lesson 8: dict ... hey! get your mind out of the gutter!</a>
1515
<a href="lesson9.html">lesson 9: shhh, it's classified</a></p>
1616

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
[lesson 3: but what if](lesson3.html)
1010
[lesson 4: wait, is that math?](lesson4.html)
1111
[lesson 5: No, I didn't say my jewels](lesson5.html)
12-
[lesson 6: game1](lesson6.html)
13-
[lesson 7: balista!](lesson7.html)
12+
[lesson 6: balista!](lesson6.html)
13+
[lesson 7: game1](lesson7.html)
1414
[lesson 8: dict ... hey! get your mind out of the gutter!](lesson8.html)
1515
[lesson 9: shhh, it's classified](lesson9.html)
1616

documentation/basic/lesson4.html

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<p><title>Lesson 4: wait, is that math? -- Basic Tutorial</title></p>
2+
3+
<p><a href="index.html">Back to Index</a></p>
4+
5+
<hr />
6+
7+
<h1>code</h1>
8+
9+
<pre><code>#ATTACK!!!
10+
import pyaudiogame
11+
spk = pyaudiogame.speak
12+
MyApp = pyaudiogame.App("My Application")
13+
14+
#Lets create a storage box so we can put our draggon's hp in there
15+
storage = pyaudiogame.cash
16+
17+
#Now lets make our draggon's hp in the storage
18+
storage.dragon_hp = 100
19+
20+
#Now lets make our hero's hit strength
21+
hero_hit = 10
22+
23+
#An attack function
24+
def attack():
25+
#When the hero attacks he takes the dragon's hp
26+
storage.dragon_hp = storage.dragon_hp - hero_hit
27+
28+
#Now lets make a way for our hero to attack
29+
def logic(actions):
30+
key = actions['key']
31+
if key == "space" and storage.dragon_hp &gt; 0:
32+
attack()
33+
spk("You, the hero of our tail swing your sword. You hit the dragon for %s damage! Now our poor dragon has %s hp left" % (hero_hit, storage.dragon_hp))
34+
if storage.dragon_hp &lt;= 0:
35+
spk("You, the hero of our story killed the dragon. The whole town thanks you!")
36+
spk("Press escape to go back home")
37+
38+
MyApp.logic = logic
39+
MyApp.run()
40+
</code></pre>
41+
42+
<h1>What you should see</h1>
43+
44+
<p>Press space and you should hear: <br />
45+
"You, the hero of our tail swing your sword. You hit the dragon for 10 damage! Now our poor dragon has 90 hp left" <br />
46+
hit space again and you hear <br />
47+
"You, the hero of our tail swing your sword. You hit the dragon for 10 damage! Now our poor dragon has 80 hp left" <br />
48+
Hit space 8 more times and you will hear: <br />
49+
"You, the hero of our tail swing your sword. You hit the dragon for 10 damage! Now our poor dragon has 0 hp left"
50+
"You, the hero of our story killed the dragon. The whole town thanks you!" <br />
51+
"Press escape to go back home"</p>
52+
53+
<h1>What just happened?</h1>
54+
55+
<p>Lets go through the code line by line: </p>
56+
57+
<pre><code>#ATTACK!!!
58+
import pyaudiogame
59+
spk = pyaudiogame.speak
60+
MyApp = pyaudiogame.App("My Application")
61+
</code></pre>
62+
63+
<p>These are just the normal magical lines we have had all throughout. </p>
64+
65+
<pre><code>#Lets create a storage box so we can put our draggon's hp in there
66+
storage = pyaudiogame.cash
67+
</code></pre>
68+
69+
<p>pyaudiogame has a module for storing data. That probably makes no sense right now, but in the next few lessons you will learn all about this. Just know that after writing this line, you can store variables in storage. People may say something about "global" variables and those are used, but this way is more "pythonic" and safer as there is no way you will mistake <code>storage.dragon_hp</code> for anything except for what it is. In pyaudiogame.cash there is also some features for saving data from one session to the next you will learn about later. </p>
70+
71+
<pre><code>#Now lets make our draggon's hp in the storage
72+
storage.dragon_hp = 100
73+
</code></pre>
74+
75+
<p>Here is how we store our variable that we will be accessing later in the program. You can store any variable in storage, it is just like what we do with normal variables, but we can change this variable. </p>
76+
77+
<pre><code>#Now lets make our hero's hit strength
78+
hero_hit = 10
79+
</code></pre>
80+
81+
<p>This is a variable we've seen before. Here is another term: "constant" The hero_hit will not be changed throughout the life of our program, so it is called a "constant variable". </p>
82+
83+
<pre><code>#An attack function
84+
def attack():
85+
#When the hero attacks he takes the dragon's hp
86+
storage.dragon_hp = storage.dragon_hp - hero_hit
87+
</code></pre>
88+
89+
<p>Here is what seperates a constant from a variable in storage. The variable in storage can be added to or subtracted or changed completely where as the constant must stay the same. Go ahead and try to make <code>dragon_hp</code> a constant and run the program. Do you see that error? It is saying that there is no variable named <code>dragon_hp</code> even though you wrote it right above. This is because there is a thing in python called a "namespace". This means that a file has a namespace and a function has its own namespace. The function can see the file's namespace, but only on a read-only basis. The file can't see the function's namespace at all. <br />
90+
This is to help you keep your program organized and easy to read. It also allows for great functionality when you start writing your game in many files (what we will go over later). </p>
91+
92+
<pre><code>#Now lets make a way for our hero to attack
93+
def logic(actions):
94+
key = actions['key']
95+
if key == "space" and storage.dragon_hp &gt; 0:
96+
attack()
97+
spk("You, the hero of our tail swing your sword. You hit the dragon for %s damage! Now our poor dragon has %s hp left" % (hero_hit, storage.dragon_hp))
98+
if storage.dragon_hp &lt;= 0:
99+
spk("You, the hero of our story killed the dragon. The whole town thanks you!")
100+
spk("Press escape to go back home")
101+
</code></pre>
102+
103+
<p>Here we are using 2 if statements. 1 will remove hp and run the attack function if the dragon's hp is above 0. The other if statement will run only if the dragon's hp is 0 or below. </p>
104+
105+
<pre><code>MyApp.logic = logic
106+
MyApp.run()
107+
</code></pre>
108+
109+
<p>Our last two magic statements. But notice that MyApp.logic, MyApp.run() and pyaudiogame.cash look the same. They are all methods of storage. You can save functions into storage by just treating them like variables and using them without the (). So go ahead and change it so that all the code accesses attack from storage.attack(). Look at these last two lines if you don't know how to get attack into storage. </p>
110+
111+
<h2>Changing variables</h2>
112+
113+
<p>One of the pillars of programming is the ability to change variables. You can do this with several operators: </p>
114+
115+
<table border=1>
116+
<tr>
117+
<td>+</td>
118+
<td>Adds together variables</td>
119+
</tr>
120+
<tr>
121+
<td>-</td>
122+
<td>subtracts variables from one another</td>
123+
</tr>
124+
<tr>
125+
<td>*</td>
126+
<td>multiplies variables together</td>
127+
</tr>
128+
<tr>
129+
<td>/</td>
130+
<td>divides variables by one another</td>
131+
</tr>
132+
<tr>
133+
<td>%</td>
134+
<td>Will give the remaindor of 2 variables divided together</td>
135+
</tr>
136+
<tr>
137+
<td>**</td>
138+
<td>Finds to the power of</td>
139+
</tr>
140+
</table>
141+
142+
<p>Along with the above you can combine = with <code>+-*/</code> to get something that looks like x += y. This is short-hand for writing: <br />
143+
x = x + y <br />
144+
Short-hand: <br />
145+
x += y </p>
146+
147+
<h2>Types</h2>
148+
149+
<p>Variables are classified into several types. There are 4 that people run into all the time: <br />
150+
string (str) <br />
151+
intiger (int) <br />
152+
float (float) <br />
153+
bool (bool) <br />
154+
We looked at strings the first lesson. They are characters like what you are reading now, but between quotes. <br />
155+
string1 = "This is a string" <br />
156+
Intigers are any number that is not in a quote. <br />
157+
num = 10 <br />
158+
floating-point numbers are numbers with a point: <br />
159+
float1 = 2.789 <br />
160+
A bool is what we looked at in the last lesson, it is a True or False value: <br />
161+
bool1 = False <br />
162+
You can use the operators on ints, floats and strings. Ints and floats can be combined to create a float, but strings can only be added to other strings or multiplied by ints. Bools can't be combined with anything. </p>
163+
164+
<h2>string formatting</h2>
165+
166+
<p>Strings have this nice feature called "string formatting" that allows us to insert variables of other types into a string. There is a huge list of string formatting commands, but the one that is most used is: <br />
167+
<code>%s</code> <br />
168+
This converts all variables to a string. Here is an example: </p>
169+
170+
<pre><code>age = 10
171+
weight = 100.35
172+
name = "Fred"
173+
"%s is %s years old and has a weight of %s lbs" % (name, age, weight)
174+
175+
"Hello, I am %s" % name
176+
177+
"If you are %s pounds, I'll pull you out of that bed so we can go walking %s Greggery James!" % (weight, name)
178+
</code></pre>
179+
180+
<p>If you inserted the lines with quotes into spk(), they will run just fine. </p>
181+
182+
<h1>assignment</h1>
183+
184+
<ol>
185+
<li>Change the attack function so it is stored in storage and called out of storage.</li>
186+
<li>Go through the last example and above each variable, write what type it is.</li>
187+
<li>Add a level variable, name variable and hp variable to our hero and anounce them.</li>
188+
<li>Convert all the long-hand addition and subtraction we used in the above code to the short-hand. People never use the long-hand code, so get used to using x -= y.</li>
189+
<li>Go read up on namespaces in python.</li>
190+
</ol>
191+
192+
<h1>Extra credit</h1>
193+
194+
<ol>
195+
<li>Go read about string formatting. See what the difference is between .format and <code>%s</code>. Also see what all the different types are. Also find why people would use the other <code>%</code> characters and not just stick to <code>%s</code> when <code>%s</code> works all the time.</li>
196+
<li>Make the dragon attack and take hp away from our hero.</li>
197+
<li>Add a key to check the health of the hero</li>
198+
<li>Add some messages as the dragon reaches a sertan amount of hp, explaining what the dragon is doing.</li>
199+
</ol>

0 commit comments

Comments
 (0)