-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.txt
181 lines (172 loc) · 3.21 KB
/
snake.txt
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
var snake_x;
var snake_y;
var snake_direction;
var food_x;
var food_y;
var score;
var snake_dead;
def snake_advance();
def check_collision();
def draw_game();
def draw_score();
def pick_new_x();
def game_tick() {
if (!snake_dead) {
var x = vector_get(snake_x, 0);
var y = vector_get(snake_y, 0);
if (food_x == x && food_y == y) {
vector_push(snake_x, 0);
vector_push(snake_y, 0);
score = score + 1;
draw_score();
pick_new_x();
}
snake_advance();
check_collision();
if (!snake_dead) {
draw_game();
}
}
asm {
#uninterrupt;
};
}
def do_stuff() {
var ch = asm {
#unkey r13;
};
if (ch == 'w' && !(snake_direction == 3)) {
snake_direction = 1;
} else if (ch == 'a' && !(snake_direction == 0)) {
snake_direction = 2;
} else if (ch == 's' && !(snake_direction == 1)) {
snake_direction = 3;
} else if (ch == 'd' && !(snake_direction == 2)) {
snake_direction = 0;
}
asm {
#uninterrupt;
};
}
def pick_new_x() {
var do_break = 0;
while (!do_break) {
food_x = abs(rand()) % 52;
food_y = abs(rand()) % 26;
var i = 0;
var ok = 1;
while (i < vector_size(snake_x)) {
if (food_x == vector_get(snake_x, i) && food_y == vector_get(snake_y, i)) {
ok = 0;
}
i = i + 1;
}
if (ok) {
do_break = 1;
}
}
}
def check_collision() {
var x = vector_get(snake_x, 0);
var y = vector_get(snake_y, 0);
if (x < 0 || x >= 52) {
snake_dead = 1;
} else if (y < 0 || y >= 26) {
snake_dead = 1;
} else {
var i = 1;
while (i < vector_size(snake_x) && !snake_dead) {
if (x == vector_get(snake_x, i) && y == vector_get(snake_y, i)) {
snake_dead = 1;
}
i = i + 1;
}
}
}
def snake_advance() {
var i = vector_size(snake_x) - 1;
while (i > 0) {
vector_set(snake_x, i, vector_get(snake_x, i - 1));
vector_set(snake_y, i, vector_get(snake_y, i - 1));
i = i - 1;
}
var x = vector_get(snake_x, 0);
var y = vector_get(snake_y, 0);
if (snake_direction == 0) {
vector_set(snake_x, 0, x + 1);
vector_set(snake_y, 0, y);
} else if (snake_direction == 1) {
vector_set(snake_x, 0, x);
vector_set(snake_y, 0, y - 1);
} else if (snake_direction == 2) {
vector_set(snake_x, 0, x - 1);
vector_set(snake_y, 0, y);
} else {
vector_set(snake_x, 0, x);
vector_set(snake_y, 0, y + 1);
}
}
def draw_score() {
var s = score;
asm {
#load 1 r0;
#literal 47 r1;
#literal 0 r2;
#printdec r0 r1 r2;
};
}
def draw_game() {
var i = 0;
while (i < 52) {
io_draw('h', i, 1);
var j = 2;
while (j < 28) {
io_draw(' ', i, j);
j = j + 1;
}
i = i + 1;
}
io_draw('x', food_x, food_y + 2);
i = 0;
while (i < vector_size(snake_x)) {
io_draw('o', vector_get(snake_x, i), vector_get(snake_y, i) + 2);
i = i + 1;
}
}
def main() {
echo "snake game";
snake_x = vector_new(4);
snake_y = vector_new(4);
score = 0;
snake_dead = 0;
draw_score();
var i = 0;
while (i < 4) {
vector_push(snake_x, 4 - i - 1);
vector_push(snake_y, 0);
i = i + 1;
}
snake_direction = 0;
food_x = 8;
food_y = 8;
var address = :game_tick;
asm {
#load 1 r0;
#interrupt 0 r0;
};
address = :do_stuff;
asm {
#load 1 r0;
#interrupt 1 r0;
};
while (!snake_dead) {
1;
}
asm {
#literal 0 r0;
#interrupt 0 r0;
};
free(snake_x);
free(snake_y);
echo " game over";
}