making the snake example "complete"
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 27 Nov 2021 21:42:14 +0000 (15:42 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 27 Nov 2021 21:42:14 +0000 (15:42 -0600)
tests/snake.onyx

index 66da1f53a8b374feb3e471072dddade8c765a174..46583eb7d43203c07c39e26bfae6492db78ac162 100644 (file)
@@ -97,6 +97,7 @@ Main_Menu_Options :: struct {
     option_1 := () {
         active_menu = .{void};
         state = .Playing;
+        reset_game();
     };
 
     ["Quit"]
@@ -108,16 +109,15 @@ Main_Menu_Options :: struct {
 Lost_Options :: struct {
     ["Play again"]
     option_1 := () {
-
+        active_menu = .{void};
+        state = .Playing;
+        reset_game();
     };
 
     ["Main menu"]
     option_2 := () {
         state = .MainMenu;
         active_menu = .{ Main_Menu_Options };
-
-        the_snake = snake_make(.{ 0, 0 });
-        the_food = .{ 10, 10 };
     };
 }
 
@@ -125,16 +125,22 @@ active_menu: Menu;
 
 load :: () {
     random.set_seed(clock.time());
-    hb.window.setTitle("Snake 🐍");
 
+    hb.window.setTitle("Snake 🐍");
     hb.utils.load_assets(^assets);
 
-    the_snake = snake_make(.{ 0, 0 });
-    the_food = .{ 10, 10 };
-
     active_menu = .{ Main_Menu_Options };
 }
 
+reset_game :: () {
+    if the_snake.body.data != null {
+        array.free(^the_snake.body);
+    }
+
+    the_snake = snake_make(.{ 0, 0 });
+    the_food = .{ random.between(0, 15), random.between(0, 10) };
+}
+
 cell_contains_snake_body :: (p: Vec2i) => {
     for ^it: the_snake.body {
         if *it == p do return true;