forked from jrmhaig/Code_Club_Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace_invaders.tex
426 lines (337 loc) · 15.5 KB
/
space_invaders.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Space Invaders
%
% (c) 2014 Joseph Haig
%
% A worksheet for beginner Python programmers to build a Space Invaders game
% intended for use with a Raspberry Pi, although this is not required.
%
% For the latest version, please see:
%
% https://github.com/jrmhaig/rpi_projects/SpaceInvaders
%
% Template modified from the 'Programming/Coding Assignment' by Ted Pavlic, as
% found on http://www.latextemplates.com (thanks). Original comments from the
% templates follow:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Programming/Coding Assignment
% LaTeX Template
%
% This template has been downloaded from:
% http://www.latextemplates.com
%
% Original author:
% Ted Pavlic (http://www.tedpavlic.com)
%
% Note:
% The \lipsum[#] commands throughout this template generate dummy text
% to fill the template out. These commands should all be removed when
% writing assignment content.
%
% This template uses a Perl script as an example snippet of code, most other
% languages are also usable. Configure them in the "CODE INCLUSION
% CONFIGURATION" section.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass[12pt]{article}
\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{framed}
\usepackage{wasysym}
\usepackage{tcolorbox}
\usepackage{needspace}
% Margins
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in
\linespread{1.1} % Line spacing
% Set up the header and footer
\pagestyle{fancy}
%\lhead{\hmwkAuthorName} % Top left header
%\chead{\hmwkClass\ (\hmwkClassInstructor\ \hmwkClassTime): \hmwkTitle} % Top center head
\chead{\hmwkClass: \hmwkTitle} % Top center head
%\rhead{\firstxmark} % Top right header
\rhead{} % Top right header
\lfoot{\lastxmark} % Bottom left footer
\cfoot{} % Bottom center footer
\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}} % Bottom right footer
\renewcommand\headrulewidth{0.4pt} % Size of the header rule
\renewcommand\footrulewidth{0.4pt} % Size of the footer rule
\setlength\parindent{0pt} % Removes all indentation from paragraphs
%----------------------------------------------------------------------------------------
% CODE INCLUSION CONFIGURATION
%----------------------------------------------------------------------------------------
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0} % This is the color used for comments
\lstloadlanguages{Python} % Load Python syntax for listings, for a list of other languages supported see: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf
\lstset{language=Python, % Use Python in this example
frame=single, % Single frame around code
basicstyle=\small\ttfamily, % Use small true type font
keywordstyle=[1]\color{Blue}\bf, % Python functions bold and blue
keywordstyle=[2]\color{Purple}, % Python function arguments purple
keywordstyle=[3]\color{Blue}\underbar, % Custom functions underlined and blue
identifierstyle=, % Nothing special about identifiers
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small, % Comments small dark green courier font
stringstyle=\color{Purple}, % Strings are purple
showstringspaces=false, % Don't put marks in string spaces
tabsize=5, % 5 spaces per tab
%
% Put standard Python functions not included in the default language here
morekeywords={rand},
%
% Put Python function parameters here
morekeywords=[2]{on, off, interp},
%
% Put user defined functions here
morekeywords=[3]{test},
%
morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment
numbers=left, % Line numbers on left
firstnumber=1, % Line numbers start with line 1
numberstyle=\tiny\color{Blue}, % Line numbers are blue and small
stepnumber=5 % Line numbers go in steps of 5
}
% Create new commands to include python and bash scripts. The first parameter
% is the filename of the script without .py or .sh and the second parameter
% is the caption.
\newcommand{\pythonscript}[2]{
\needspace{.125\textheight}
\begin{itemize}
\item[]\lstinputlisting[caption=#2,label=#1]{snippets/#1.py}
\end{itemize}
}
\newcommand{\bashscript}[2]{
\begin{itemize}
\item[]\lstinputlisting[caption=#2,label=#1]{snippets/#1.sh}
\end{itemize}
}
%----------------------------------------------------------------------------------------
% DOCUMENT STRUCTURE COMMANDS
% Skip this unless you know what you're doing
%----------------------------------------------------------------------------------------
% Header and footer for when a page split occurs within a problem environment
\newcommand{\enterProblemHeader}[1]{
\nobreak\extramarks{#1}{#1 continued on next page\ldots}\nobreak
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
}
% Header and footer for when a page split occurs between problem environments
\newcommand{\exitProblemHeader}[1]{
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
\nobreak\extramarks{#1}{}\nobreak
}
\setcounter{secnumdepth}{0} % Removes default section numbers
\newcounter{homeworkProblemCounter} % Creates a counter to keep track of the number of problems
\newcommand{\challengeStepName}{}
\newenvironment{challengeStep}[1][Step \arabic{homeworkProblemCounter}]{ % Makes a new environment called homeworkProblem which takes 1 argument (custom name) but the default is "Problem #"
\stepcounter{homeworkProblemCounter} % Increase counter for number of problems
\renewcommand{\challengeStepName}{#1} % Assign \homeworkProblemName the name of the problem
\section{\challengeStepName} % Make a section in the document with the custom problem count
\enterProblemHeader{\challengeStepName} % Header and footer within the environment
}{
\exitProblemHeader{\challengeStepName} % Header and footer after the environment
}
\newcommand{\problemAnswer}[1]{ % Defines the problem answer command with the content as the only argument
\noindent\framebox[\columnwidth][c]{\begin{minipage}{0.98\columnwidth}#1\end{minipage}} % Makes the box around the problem answer and puts the content inside
}
\newcommand{\homeworkSectionName}{}
\newenvironment{homeworkSection}[1]{ % New environment for sections within homework problems, takes 1 argument - the name of the section
\renewcommand{\homeworkSectionName}{#1} % Assign \homeworkSectionName to the name of the section from the environment argument
\subsection{\homeworkSectionName} % Make a subsection with the custom name of the subsection
\enterProblemHeader{\challengeStepName\ [\homeworkSectionName]} % Header and footer within the environment
}{
\enterProblemHeader{\challengeStepName} % Header and footer after the environment
}
%----------------------------------------------------------------------------------------
% NAME AND CLASS SECTION
%----------------------------------------------------------------------------------------
\newcommand{\hmwkTitle}{Space Invaders (Part 1)} % Assignment title
\newcommand{\hmwkDueDate}{Monday,\ January\ 1,\ 2012} % Due date
\newcommand{\hmwkClass}{Code Club Challenge} % Course/class
\newcommand{\hmwkClassTime}{10:30am} % Class/lecture time
\newcommand{\hmwkClassInstructor}{Jones} % Teacher/lecturer
\newcommand{\hmwkAuthorName}{John Smith} % Your name
%\newenvironment{hint}{
% \begin{framed}
% \textbf{Hint}
%
%}{\end{framed}} % TODO put this in a box
\definecolor{mycol}{rgb}{0.122, 0.435, 0.698}
\newenvironment{hint}{
\begin{tcolorbox}[colback=green!5,colframe=green!40!black,title=Note]}
{\end{tcolorbox}}
%----------------------------------------------------------------------------------------
% TITLE PAGE
%----------------------------------------------------------------------------------------
\title{
\vspace{2in}
\textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
%\normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate}\\
%\vspace{0.1in}\large{\textit{\hmwkClassInstructor\ \hmwkClassTime}}
\vspace{3in}
}
%\author{\textbf{\hmwkAuthorName}}
\date{} % Insert date here if you want it to appear below your name
%----------------------------------------------------------------------------------------
\begin{document}
\maketitle
%----------------------------------------------------------------------------------------
% TABLE OF CONTENTS
%----------------------------------------------------------------------------------------
%\setcounter{tocdepth}{1} % Uncomment this line if you don't want subsections listed in the ToC
\newpage
%\tableofcontents
%\newpage
%----------------------------------------------------------------------------------------
% PROBLEM 1
%----------------------------------------------------------------------------------------
\section{Setting up}
One of the very first computer games was called ``Space Invaders''\footnote{It
was apparently originally created in 1978, which makes it almost as old as me!}
and we are going to recreate it in Python.
To get started, open LXTerminal and use the following command to download the
start of the project:
\bashscript{setup}{Get the the starting point}
This should create a new directory called \texttt{space\_invaders} containing
a Python program and some icons. Open the program with:
\bashscript{openscript}{Opening the starting point}
\begin{hint}
We need to use idle (for Python version 2) instead of idle3 (for Python
version 3) because the PyGame module only works for Python version 2 on the
Raspberry Pi at the moment.
You could also launch Idle from the desktop icon and then browse to the
correct directory to open the file.
\end{hint}
\section{Controlling the ship}
The first thing to do is to add the ship that you (the player) will control.
This will move left and right along the bottom of the screen while the aliens,
which you have to shoot, will go at the top. Look at the script below and see
what you need to change.
\begin{hint}
Can you identify the parts that:
\begin{itemize}
\item Finds the picture file to use for the ship?
\item Tells the computer where to put the ship?
\item Finds out if you are pressing the left or right buttons?
\end{itemize}
\end{hint}
\pythonscript{controls}{Controlling the ship}
\section{Add a bit of \emph{class}}
Now we have managed to get the ship moving more easily but do you see what
happens when you go to the edge of the window? We will solve this later.
First, we are going to introduce a new
programming idea; \emph{Classes} and \emph{Objects.}
So far, we have just a single thing in our game -- a ship that you can control
with the arrow keys. Later we will also have a number of aliens as well as
bullets, and all of these have very similar information about them and actions.
For example, they all have a position (x and y) and they all need to move.
With a Class we can write code for them all once and only once.
\begin{hint}
Can you see how I stopped the ship going off the side of the window?
Can you work out how to make it move faster or slower?
\end{hint}
\pythonscript{classes}{Adding classes}
\section{Explanation}
In the previous section we added a \emph{Class} called \texttt{GamePiece}.
By itself, this does not do anything to it provides a pattern from which
we created an \emph{Object} for the ship. Later, we will add another object
for an alien and our code will contain:
\pythonscript{explanation1}{Object creation example}
Each of these is separate from the other and has its own x and y coordinates.
They also both know how to move and draw themselves in the correct position
in the window if we call the functions:
\pythonscript{explanation2}{Function call example}
and we can even set their speeds with:
\pythonscript{explanation3}{Setting object variables}
\begin{hint}
This may be the answer to one of the questions in the last section!
\end{hint}
As well as the \texttt{move} and \texttt{draw} functions you will see another
function called \texttt{\_\_init\_\_} (that is a double `\_' before and after
the word `init'. This is a special function that is always run once when the
object is created with \texttt{ship = GamePiece(150, 260, ship\_image)}.
This makes sure that all the variables have the correct values at the start.
\section{Add an alien}
The real Space Invaders game has lots of aliens in rows but for the moment we
will start with just one. As I said in the last section, a lot of the work is
already done for us as we can use the \texttt{GamePiece} class.
First, make some changes to the \texttt{GamePiece} class to let the alien
know which way it is going and then ``bounce'' when it reaches the side of the
screen.
\pythonscript{bounce}{Changes to the class}
Next, this is all that is needed to create the alien.
\pythonscript{makealien}{Create the alien object}
\pythonscript{usealien}{Use the alien object}
\section{Shoot the alien}
The next thing to do is to let you try to shoot the alien. For this, we need
to load an image of a bullet with this line (try to find the correct place to
put it):
\pythonscript{loadbullet}{Load the bullet image}
Now the bullet needs to move up the screen, rather than left or right, and
then disappear when it reaches the top. For this we need to add a new
variable to the \texttt{\_\_init\_\_} function:
\pythonscript{bulletinit}{New variables}
and add a new part to the \texttt{move} function:
\pythonscript{bulletmove}{New move function}
At the start of the game, the bullet doesn't exist and we can indicate this
with:
\pythonscript{initialisebullet}{Initialise bullet}
And finally, the bullet should appear when we press the `SPACE' bar.
\pythonscript{fire}{Fire!}
\section{Hit the alien}
You may have noticed that the bullet currently just goes straight through the
alien. This isn't very good! In the \texttt{GamePiece} class we need a new
function to detect if it has been hit.
\pythonscript{detecthit}{Detect a hit}
At the moment there is only one alien so when it is hit we will put it back
at the top and make it move faster. Later, we can have several rows of aliens
and try to keep a score.
\pythonscript{hitalien}{Hit the alien}
\section{Next steps}
There is still some more thing to do to get the full Space Invaders game:
\begin{itemize}
\item Several aliens in a number of rows
\item Let the aliens fire bullets as well as the player
\item Keep a score
\item Add some sound
\end{itemize}
%\section{More aliens}
%
%Now we will make a line of 5 aliens and store them in an array.
%
%\pythonscript{fivealiens}{Line of 5 aliens}
%
%Get them all to move and get hit by bullets, and you lose the game if they
%reach the bottom.
%
%\pythonscript{moveallaliens}{Line of 5 aliens}
%
%\section{Score}
%
%It is about time we kept the score. For the moment, we will give 10 points
%for each hit.
%
%Make variable for the score and prepare the type font.
%
%\pythonscript{scorevariable}{Score variable}
%
%Add 10 points for each alien hit and display the score.
%
%\pythonscript{scorehit}{Score a hit}
%
%\section{To do}
%
%Alien bullets, more levels.
\section{Licensing}
This worksheet is licensed under the Creative Commons Attribution-ShareAlike
4.0 International License (http://creativecommons.org/licenses/by-sa/4.0/).
\end{document}