diff options
| author | dam <dam@gudinoff> | 2024-04-16 10:09:50 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2024-04-16 10:09:50 +0100 |
| commit | bc297bfa329b31f252cc115c6a4342548da2aeaf (patch) | |
| tree | 1e6b524924ba96fdab310e852ef1232d94add871 /snake.jai | |
| parent | bb1d53daa526d734fb2e33c2090f8396f87544ec (diff) | |
| download | task-time-tracker-bc297bfa329b31f252cc115c6a4342548da2aeaf.tar.zst task-time-tracker-bc297bfa329b31f252cc115c6a4342548da2aeaf.zip | |
Small improvements on snake example.
Diffstat (limited to 'snake.jai')
| -rw-r--r-- | snake.jai | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -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(); |
