demo/snake/snake.c

changeset 273
966bfca56b9d
parent 272
6ed9fb9662c0
--- a/demo/snake/snake.c	Wed Aug 20 23:51:40 2025 +0200
+++ b/demo/snake/snake.c	Thu Aug 21 22:13:51 2025 +0200
@@ -30,6 +30,7 @@
 #include <ascension/sprite.h>
 #include <ascension/2d.h>
 #include <ascension/shader.h>
+#include <ascension/util.h>
 
 #include <cx/printf.h>
 #include <cx/linked_list.h>
@@ -368,7 +369,6 @@
     // remark: some calculations are repeated here, but they are cheap enough
     {
         const asc_vec2u tile_coords = game_field_tile_at_position(node->position);
-        // TODO: player should have been destroyed before leaving the field
         if (tile_coords.x > GAME_FIELD_SIZE || tile_coords.y > GAME_FIELD_SIZE) return;
         if (game_field_tile_chown(tile_coords, player)) {
             // new owner of the tile!
@@ -416,6 +416,15 @@
     pl->reset_position = true;
 }
 
+static void player_position_random(Player *pl) {
+    // TODO: check if the spawn location is viable when there is more action on the board
+    player_position(
+        pl,
+        4 + asc_util_rand(GAME_FIELD_SIZE - 8),
+        4 + asc_util_rand(GAME_FIELD_SIZE - 8)
+    );
+}
+
 static void player_destroy(CxAllocator *allocator, Player *player) {
     cxListFree(player->trace);
     cxFree(allocator, player);
@@ -435,7 +444,7 @@
     );
     asc_scene_add_node(MAIN_SCENE, node);
     Player *player = asc_scene_node_allocate_data(node, sizeof(Player));
-    player_position(player, 12, 8);
+    player_position_random(player);
     player->speed = 3.f; // start with 3 tiles/sec
     player->number = 1;
     player->health = 100;
@@ -572,7 +581,7 @@
             asc_scene_node_show(text_game_over);
             if (asc_key_pressed(ASC_KEY(R))) {
                 // TODO: re-load the "level"
-                player_position(game.players[0], 12, 8);
+                player_position_random(game.players[0]);
                 player_set_health(game.players[0], 100);
                 game.state = GAME_STATE_PLAYING;
                 asc_scene_node_hide(text_game_over);

mercurial