summaryrefslogtreecommitdiff
path: root/docs/firstbot.tex
blob: 7e95b857b3f1779a5c052d377dfdf29603790f11 (about) (plain)
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
\section{Your First Bot}
\label{sec:firstbot}
\fasttrack{Always move up.}

Alright, let’s get started.
If you think back to when you started programming, chances are the first program
you ever wrote was one which printed out the immortal phrase “Hello World”.
Well we can’t print stuff here, but our first bot is going to be almost as
useless as that: our bot is just going to continually move up.

Let’s have a look at the code:
\pythonfile{firstbot.py}

Pretty simple, huh?
It’s a function takes as input two parameters, and returns a string.
We’ll have a look at \texttt{board} and \texttt{position} later,
but the important thing here is that the returned value says which direction the
snake should move in. Each time the snake is allowed to make a move, the
function is called, it returns one of \py|'U', 'D', 'L', 'R'|
(indicating up, down, left and right, respectively), and the snake is moved in
that direction.

\subsection{Running the code}

Depending on how you have installed SnakeGame, there are a few different ways to
run the code. If you’re in some kind of programming class, ask your instructor
which method to use.

\subsubsection{Method A: CLI interface}

If you installed from the repository (using \texttt{pip}), this is the method
you should use.
Assuming you’ve put the \texttt{up\_bot} function in a file called
\texttt{mybot.py}, you can run this command:

\begin{shell}
$ snakegame mybot:up_bot
\end{shell}

To use different viewers, you can supply the \texttt{-v VIEWER} argument:
\begin{shell}
$ snakegame -v pyglet mybot:up_bot
$ snakegame -v pygame mybot:up_bot
$ snakegame -v curses mybot:up_bot
\end{shell}

You can specify multiple bots, and also control the width, height and number of
apples on the board:
\begin{shell}
$ snakegame -w 4 -h 20 -a 30 mybot:up_bot mybot:up_bot mybot:up_bot
\end{shell}

\subsubsection{Method B: Pyglet / Pygame}

You can also add some code to the file containing your bot so that you can run
that file as a normal Python program, which will run the game.
At the end of the file, add this:
\begin{pythoncode}
if __name__ == '__main__':
    from snakegame.engine import Engine
    from snakegame.viewers.pyglet import Viewer
    engine = Engine(10, 10, 25)
    engine.add_bot(up_bot)
    viewer = Viewer(engine)
    viewer.run()
\end{pythoncode}

If you want to use pygame instead, change \texttt{snakegame.viewers.pyglet} to
\texttt{snakegame.viewers.pygame}.

If neither of these work, there is also a console viewer, which works if you’re
in a terminal (it will not work in IDLE!):
use \texttt{snakegame.viewers.curses}.

\subsection{Got it running?}

Great, you should see a nice big board with some apples scattered over it,
and a snake continually moving upwards.

Once you’re ready, we’ll move on to something a little more interesting.