-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.tex
184 lines (167 loc) · 5.39 KB
/
Main.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
\documentclass{article}
\usepackage[paperheight=10.5cm,paperwidth=10cm]{geometry}
\pagenumbering{gobble}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usepackage{media9}
\usepackage[absolute,overlay]{textpos}
\usepackage[pdfstartview=Fit]{hyperref}
\input{draw_snake.tex}
\pgfmathsetmacro\gameSize{8}
\pgfmathparse{\gameSize+1}\pgfmathsetmacro\boardSize{\pgfmathresult}
\textblockorigin{0.5cm}{1cm}
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\newcommand{\makeSnakeGame}{
\pdfpageattr{/AA <<
/O << /S/JavaScript /JS (
gameSize = 8;
gameSpeed = 300;
animations = [];
function set_segment(xy,val) {
animations[xy[0]][xy[1]].frameNum = val;
}
segments = [[14, 15, -1, 16], [17, 13, 16, -1], [-1, 18, 14, 17], [18, -1, 15, 13]];
function init() {
anim["intro"].frameNum = 1;
anim["score"].frameNum = 4;
while(animations.length) animations.pop();
for(i = 0; i < gameSize; i++) {
animations.push([]);
for(j = 0; j < gameSize; j++)
animations[i].push(anim["cell" + i + "x" + j]);
}
snake = [[0,0,-1], [1,0,4], [2,0,4], [3,0,4]];
old_direction = 3;
direction = 0;
for(i = 0; i < gameSize; i++)
for(j = 0; j < gameSize; j++)
set_segment([i,j],0);
set_segment(snake[0], 4);
set_segment(snake[1], 13);
set_segment(snake[2], 13);
set_segment(snake[3], 8);
add_food();
alive = true;
}
function update_snake() {
new_head = snake[snake.length - 1].slice(0);
if(direction == 0) new_head[1] += 1;
else if(direction == 1) new_head[0] -= 1;
else if(direction == 2) new_head[1] -= 1;
else if(direction == 3) new_head[0] += 1;
die = Math.min(new_head[0], new_head[1]) < 0 || Math.max(new_head[0], new_head[1]) >= gameSize;
for(i = 0; i < snake.length - 1; i++)
die |= snake[i][0] == new_head[0] && snake[i][1] == new_head[1];
if(die) {
set_segment(snake[snake.length - 1], old_direction+9);
return false;
}
grow = animations[new_head[0]][new_head[1]].frameNum == 19;
new_head[2] = direction+1;
snake.push(new_head);
set_segment(new_head, direction+5);
set_segment(snake[snake.length - 2], segments[direction][old_direction]);
if(grow) {
add_food();
anim["score"].frameNum = snake.length;
} else {
set_segment(snake.shift(), 0);
set_segment(snake[0], snake[1][2]);
}
old_direction = direction;
return true;
}
function add_food() {
outer:
while(true) {
x = Math.floor(gameSize*Math.random());
y = Math.floor(gameSize*Math.random());
for(i = 0; i < snake.length; i++)
if(snake[i][0] == x && snake[i][1] == y)
continue outer;
set_segment([x, y], 19);
return;
}
}
function set_direction(dir) {
if(Math.abs(direction - dir) != 2)
next_direction = direction = dir;
}
function parse_input(input) {
key = input[input.length - 1];
if(key == "d") set_direction(3);
else if(key == "w") set_direction(0);
else if(key == "a") set_direction(1);
else if(key == "s") set_direction(2);
else if(key == " ") init();
}
function update() {
if(alive)
alive = update_snake();
}
alive = false;
this.getField("input").setFocus();
interval = app.setInterval("update()", gameSpeed);
) >>
>>}
}
\makeSnakeGame{}{}
\begin{document}
% The intro is draw behind the background, which gets overlayed when the game
% starts. Doing it this way around prevents the intro glitching out and not
% showing up. When the game starts everything gets refreshed and shows up.
\begin{textblock}{\boardSize}[0,0](0,0)
\drawIntro
\end{textblock}
\begin{textblock}{\boardSize}[0,0](0,0)
\begin{animateinline}[nomouse,label=intro]{1}
\tikz{\path[use as bounding box] (0,0) rectangle (\gameSize,\gameSize);}
\newframe \drawBackground
\end{animateinline}
\end{textblock}
\foreach \i in {0,...,\gameSize-1}{
\foreach \j in {0,...,\gameSize-1}{
\animateCell{\i}{\j}
}
}
% Initially the focus gets set to the first input field. When a key is pressed,
% the focus gets set to the second field, thereby updating the value attribute
% of the first field. The value is processed and the focus is set back to the
% first field.
\begin{textblock}{2}[0,12](0,0)
\begin{Form}
\TextField[width=1cm,name=input,keystroke={%
this.getField("tmp").setFocus();
}]{~}~%
\TextField[width=1cm,name=tmp,onfocus={%
parse_input(this.getField("input").value);
this.getField("input").setFocus();
this.dirty=false;
}]{~}
\end{Form}
\end{textblock}
% Click game area to set focus to first input field.
\begin{textblock}{\boardSize}[0,0](0,0)
\mediabutton[jsaction={
this.getField("input").setFocus();
}]{%
\begin{tikzpicture}
\path[use as bounding box] (0,0) rectangle (\gameSize,\gameSize);
\end{tikzpicture}
}
\end{textblock}
\begin{textblock}{2}[-3.5,-8](0,0)
\newcounter{score}
\makeatletter
\begin{animateinline}[nomouse,label=score]{1}
\tikz{\path[use as bounding box] (0,0) rectangle (1,1) node[midway] {\huge ~};}
\@whilenum\value{score}<64\do{\stepcounter{score}\newframe%
\tikz{\path[use as bounding box] (0,0) rectangle (1,1)
node[midway] {\huge \thescore};}%
}
\end{animateinline}
\makeatother
\end{textblock}
\end{document}