aboutsummaryrefslogtreecommitdiff
path: root/snake.jai
diff options
context:
space:
mode:
Diffstat (limited to 'snake.jai')
-rw-r--r--snake.jai39
1 files changed, 22 insertions, 17 deletions
diff --git a/snake.jai b/snake.jai
index ea8926f..e3a15f6 100644
--- a/snake.jai
+++ b/snake.jai
@@ -1,6 +1,6 @@
#import "Basic";
#import "Random";
-TUI :: #import "TUI";
+TUI :: #import "TUI"(COLOR_MODE = 8);
Vec2D :: struct {
x: int;
@@ -24,13 +24,6 @@ main :: () {
score := 0;
dir := Vec2D.{1, 0};
food := Vec2D.{5, 5};
-
- random_food :: () -> Vec2D {
- return Vec2D.{
- cast(int)(random_get_zero_to_one_open() * (screen_size_x-3) + 2),
- cast(int)(random_get_zero_to_one_open() * (screen_size_y-3) + 2)
- };
- }
snake_parts: [..] Vec2D;
for 0..13 array_add(*snake_parts, Vec2D.{3, 3});
@@ -51,7 +44,6 @@ main :: () {
TUI.draw_box(1, 1, screen_size_x, screen_size_y);
TUI.set_cursor_position(3, screen_size_y);
write_strings(" ", player_name, " ");
- food = random_food();
case #char "q"; #through;
case #char "Q"; #through;
@@ -71,6 +63,13 @@ main :: () {
if dir != Vec2D.{-1, 0} then dir = Vec2D.{1, 0};
}
+ if screen_size_x < 15 || screen_size_y < 15 {
+ TUI.clear_terminal();
+ TUI.set_cursor_position(1,1);
+ write_string("~ paused : increase window size ~");
+ continue;
+ }
+
last_pos := snake_parts[snake_parts.count-1];
// Update position.
@@ -83,10 +82,12 @@ main :: () {
snake_parts[0].y += dir.y;
// Teleport on borders.
- if snake_parts[0].x < 2 then snake_parts[0].x = screen_size_x - 1;
- if snake_parts[0].x >= screen_size_x then snake_parts[0].x = 2;
- if snake_parts[0].y < 2 then snake_parts[0].y = screen_size_y - 1;
- if snake_parts[0].y >= screen_size_y then snake_parts[0].y = 2;
+ if snake_parts[0].x < 2 then snake_parts[0].x = screen_size_x - 1;
+ if snake_parts[0].x >= screen_size_x then snake_parts[0].x = 2;
+ if snake_parts[0].y < 2 then snake_parts[0].y = screen_size_y - 1;
+ if snake_parts[0].y >= screen_size_y then snake_parts[0].y = 2;
+ food.x = clamp(food.x, 2, screen_size_x-1);
+ food.y = clamp(food.y, 2, screen_size_y-1);
// Check for game-over.
for 1..snake_parts.count-1 {
@@ -99,7 +100,10 @@ main :: () {
if snake_parts[0] == food {
score += 1;
array_add(*snake_parts, snake_parts[snake_parts.count-1]);
- food = random_food();
+ food = Vec2D.{
+ cast(int)(random_get_zero_to_one_open() * (screen_size_x-3) + 2),
+ cast(int)(random_get_zero_to_one_open() * (screen_size_y-3) + 2)
+ };
}
// Wait to match game loop time.
@@ -143,10 +147,11 @@ main :: () {
game_loop();
// Game over screen.
- TUI.draw_box(screen_size_x/3, screen_size_y/2-1, screen_size_x/3, 4);
- TUI.set_cursor_position((screen_size_x-GAME_OVER_TEXT.count)/2, screen_size_y/2);
+ box_size := Vec2D.{19, 4};
+ TUI.draw_box((screen_size_x-box_size.x)/2, (screen_size_y-box_size.y)/2, box_size.x, box_size.y);
+ TUI.set_cursor_position((screen_size_x-GAME_OVER_TEXT.count)/2, (screen_size_y-box_size.y)/2 + 1);
write_string(GAME_OVER_TEXT);
- TUI.set_cursor_position((screen_size_x-GAME_OVER_TEXT.count)/2, screen_size_y/2+1);
+ TUI.set_cursor_position((screen_size_x-GAME_OVER_TEXT.count)/2, (screen_size_y-box_size.y)/2 + 2);
write_string(INSTRUCTIONS_TEXT);
sleep_milliseconds(100);
TUI.flush_input();