-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.matrixcode
More file actions
145 lines (113 loc) · 3.38 KB
/
snake.matrixcode
File metadata and controls
145 lines (113 loc) · 3.38 KB
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
var snake_x[];
var snake_y[];
var direction = -1;
var loose_state = false;
var food_x = 0;
var food_y = 0;
function reset() {
list::clear(snake_x);
list::clear(snake_y);
list::push_back(snake_x, 1);
list::push_back(snake_x, 2);
list::push_back(snake_x, 3);
list::push_back(snake_y, 5);
list::push_back(snake_y, 5);
list::push_back(snake_y, 5);
food_x = random(0,11);
food_y = random(0,11);
set_controls(0b00011011);
loose_state = false;
direction = -1;
}
// Wird einmal beim Start deines Spiels aufgerufen
function init() {
set_tps(2);
reset();
}
// Wird während deines Spiels mit den Ticks pro Sekunde (tps) aufgerufen
function game_loop() {
if(is_animation_running() || loose_state) {
return;
}
if(direction == 0) {
list::push_back(snake_x, snake_x[list::count(snake_x) - 1]);
list::push_back(snake_y, snake_y[list::count(snake_y) - 1]+1);
}
if(direction == 1) {
list::push_back(snake_x, snake_x[list::count(snake_x) - 1]+1);
list::push_back(snake_y, snake_y[list::count(snake_y) - 1]);
}
if(direction == 2) {
list::push_back(snake_x, snake_x[list::count(snake_x) - 1]);
list::push_back(snake_y, snake_y[list::count(snake_y) - 1] - 1);
}
if(direction == 3) {
list::push_back(snake_x, snake_x[list::count(snake_x) - 1] - 1);
list::push_back(snake_y, snake_y[list::count(snake_y) - 1]);
}
var head_x = snake_x[list::count(snake_x) - 1];
var head_y = snake_y[list::count(snake_y) - 1];
// Check for collision with the body
for (var i = 0; i < list::count(snake_x) - 1; i++) {
if (snake_x[i] == head_x && snake_y[i] == head_y) {
loose_state = true;
set_controls(0b00100000); // Disable controls when loose
run_animation_splash(head_x, head_y, 0xFF0000, true, 1000, 1000); // Show crash animation
return;
}
}
// Check if snake is out of bounds
if (head_x < 0 || head_x > 11 || head_y < 0 || head_y > 11) {
loose_state = true;
set_controls(0b00100000); // Disable controls when loose
run_animation_splash(head_x, head_y, 0xFF0000, true, 1000, 1000); // Show crash animation
return;
}
var food_collision = (head_x == food_x) && (head_y == food_y);
if (direction != -1 && !food_collision)
{
list::pop(snake_x);
list::pop(snake_y);
}
if (food_collision)
{
food_x = random(0, 11);
food_y = random(0, 11);
}
}
// Wird 30 mal pro Sekunde aufgerufen, um dein Spiel zu zeichnen
function draw() {
if(is_animation_running()) {
return;
}
clear();
if(loose_state) {
number(1, 4, list::count(snake_x) - 1, 0xFF0000);
return;
}
for(var i=0;i<list::count(snake_x);i++) {
set(snake_x[i], snake_y[i], 0x00FF00);
}
set(food_x, food_y, 0xFF0000);
}
// Wird beim Beenden deines Spiels aufgerufen
function clean_up() {
}
// Wird bei einer Nutzereingabe aufgerufen
function on_event(event) {
if(event == 0) { //UP
direction = 0;
}
if(event == 1) { //DOWN
direction = 2;
}
if(event == 2) { //LEFT
direction = 3;
}
if(event == 3) { //RIGHT
direction = 1;
}
if(event == 5) {
reset();
}
}