-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.clla
147 lines (118 loc) · 3.38 KB
/
example.clla
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
import rl "opt/raylib"
import "fmt"
TICK_RATE ::= 0.13;
WINDOW_SIZE ::= 1000;
GRID_LENGTH ::= 20;
CELL_SIZE ::= 16;
MAX_SNAKE_LENGTH ::= GRID_LENGTH * GRID_LENGTH;
CANVAS_SIZE :: GRID_LENGTH * CELL_SIZE;
alias Vec2i: [2]int;
snake: [MAX_SNAKE_LENGTH]Vec2i;
snake_len: uint;
move_dir: Vec2i = [0, 1];
tick_timer: float = TICK_RATE;
game_over: bool = false;
food_pos: Vec2i;
func place_food() {
occupied: [GRID_LENGTH][GRID_LENGTH]bool;
for (i in 0..<snake_len) {
occupied[snake[i][0]][snake[i][1]] = true;
}
free_cells: [^]Vec2i = make [^]Vec2i;
defer destroy free_cells;
for (x in 0..<GRID_LENGTH) {
for (y in 0..<GRID_LENGTH) {
if (not occupied[x][y])
append(&free_cells, Vec2i { x, y });
}
}
if (free_cells.len > 0) {
random_cell_idx := rl::get_random_value(0, int32(len(free_cells) - 1));
food_pos = free_cells[random_cell_idx];
}
}
func restart() {
start_head_pos := Vec2i { GRID_LENGTH / 2, GRID_LENGTH / 2 };
snake[0] = tstart_head_pos;
snake[1] = start_head_pos - [0, 1];
snake[2] = start_head_pos - [0, 2];
snake_len = 3;
move_dir = [0, 1];
game_over = false;
place_food();
}
func main() : int {
rl::SetConfigFlags(rl::Flag.VSYNC_HINT);
rl::InitWindow(WINDOW_SIZE, WINDOW_SIZE, "Snake");
restart();
while (not rl::window_should_close()) {
if (rl::is_key_down(rl::Key.UP)) {
move_dir = [0, -1];
} else if (rl::is_key_down(rl::Key.DOWN)) {
move_dir = [0, 1];
} else if (rl::is_key_down(rl::Key.LEFT)) {
move_dir = [-1, 0];
} else if (rl::is_key_down(rl::Key.RIGHT)) {
move_dir = [1, 0];
}
if (game_over) {
if (rl::is_key_down(rl::Key.ENTER))
restart();
} else {
tick_timer -= rl::GetFrameTime();
}
if (tick_timer <= 0) {
next_part_pos := snake[0];
snake[0] = snake[0] - move_dir;
head_pos := snake[0];
if (head_pos[0] < 0 or head_pos[1] < 0 or head_pos[0] >= GRID_LENGTH or head_pos[1] >= GRID_LENGTH) {
game_over = true;
}
for (i in 1..<snake_len) {
curr_pos := snake[i];
if (curr_pos == head_pos) game_over = true;
snake[i] = next_part_pos;
next_part_pos = curr_pos;
}
if (head_pos == food_pos) {
snake_len += 1;
snake[snake_len - 1] = next_part_pos;
place_food();
}
tick_timer += TICK_RATE
}
camera := rl::Camera2D {
zoom = float(WINDOW_SIZE) / CANVAS_SIZE,
};
rl::BeginDrawing();
rl::ClearBackground(rl::Color { 76, 53, 83, 255 });
rl::BeginMode2D(camera);
food_rect := rl::Rectangle {
float(food_pos[0]) * CELL_SIZE,
float(food_pos[1]) * CELL_SIZE,
CELL_SIZE,
CELL_SIZE,
};
rl::DrawRectangleRec(food_rect, rl::RED);
for (i in 0..<snake_len) {
snake_rect := rl::Rectangle {
float(snake[i][0] * CELL_SIZE),
float(snake[i][1] * CELL_SIZE),
CELL_SIZE,
CELL_SIZE,
};
rl::DrawRectangleRec(snake_rect, rl::WHITE);
}
if (game_over) {
rl::DrawText("Game over", 4, 4, 25, rl::RED);
rl::DrawText("Press ENTER to play again", 4, 30, 15, rl::BLACK);
}
score := snake_len - 3;
score_msg: ^string = fmt::alloc_sprintf("Score %v", score)!;
defer free score_msg;
rl::DrawText(score_msg.char_ptr, 4, CANVAS_SIZE - 14, 10, rl::GRAY);
rl::EndMode2D();
rl::EndDrawing();
}
return 0;
}