snake game work
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 17:50:36 +0000 (11:50 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 17:50:36 +0000 (11:50 -0600)
tests/snake.onyx

index d6c42caba608f7e08aee9e4761c68d4c3d89c83a..23788e3fec089ddab7813bd7ad652538ec4aaf8a 100644 (file)
@@ -73,6 +73,9 @@ snake_draw :: (use this: ^Snake, cell_size: f32) {
 }
 
 
+GameState :: enum { MainMenu; Playing; Paused; Lost; }
+state := GameState.Playing;
+
 the_snake: Snake;
 the_food: Vec2i;
 
@@ -83,11 +86,20 @@ load :: () {
     the_food = .{ 10, 10 };
 }
 
+cell_contains_snake_body :: (p: Vec2i) => {
+    for ^it: the_snake.body {
+        if *it == p do return true;
+    }
+    return false;
+}
+
 update :: (dt: f32) {
     if hb.input.keyIsDown(.Escape) {
         hb.window.setShouldClose(true);
     }
 
+    if state != .Playing do return;
+
     #persist last_left_down := false;
     #persist last_right_down := false;
 
@@ -110,25 +122,70 @@ update :: (dt: f32) {
         snake_move(^the_snake);
 
         if the_snake.head == the_food {
-            the_food = .{ random.between(0, 10), random.between(0, 10) };
+            the_food = the_snake.body[0];
+            while cell_contains_snake_body(the_food) {
+                the_food = .{ random.between(0, 15), random.between(0, 10) };
+            } 
+
             snake_grow(^the_snake, 2);
         }
+
+        if cell_contains_snake_body(the_snake.head) {
+            state = .Lost;
+        }
     }
 }
 
 draw :: () {
-    hb.graphics.setClearColor(0.1, 0.1, 0.1);
+    hb.graphics.setClearColor(0.9, 0.9, 0.9);
     hb.graphics.clear();
 
-    cell_size :: 32.0f;
+    ww, wh := hb.window.getDimensions();
+
+    switch state {
+        case .MainMenu {
+            title := "Snake!";
+            title_width := hb.graphics.getTextWidth(title);
+            hb.graphics.print(title, (~~ww - title_width) / 2, 100);
+        }
+
+        case .Playing {
+            draw_game();
+
+            #if false {
+                hb.graphics.setColor(1, 0, 0);
+                fps_buffer: [20] u8;
+                fps_str := conv.str_format(fps_buffer, "FPS: {}", cast(i32) hb.timer.getFPS());
+                hb.graphics.print(fps_str, ~~ww - hb.graphics.getTextWidth(fps_str), 30);
+
+                watermark := conv.str_format(fps_buffer, "{b16}", alloc.heap.get_watermark());
+                hb.graphics.print(watermark, ~~ww - hb.graphics.getTextWidth(watermark), 60);
+
+                freed := conv.str_format(fps_buffer, "{b16}", alloc.heap.get_freed_size());
+                hb.graphics.print(freed, ~~ww - hb.graphics.getTextWidth(freed), 90);
+            }
+        }
+
+        case .Lost {
+            draw_game();
+            hb.graphics.setColor(0, 0, 0, 0.4);
+            hb.graphics.rectangle(.Fill, 0, 0, ~~ww, ~~wh);
+
+            message := "You Lost!";
+            message_width := hb.graphics.getTextWidth(message);
+            hb.graphics.print(title, (~~ww - message_width) / 2, 100);
+        }
+    }
+}
+
+draw_game :: () {
+    cell_size :: 24.0f;
 
     snake_draw(^the_snake, cell_size);
 
     hb.graphics.setColor(0.8, 0.9, 0.1);
     hb.graphics.circle(.Fill, cell_size * ~~the_food.x + cell_size / 2, cell_size * ~~the_food.y + cell_size / 2, cell_size / 2);
-
-    hb.graphics.setColor(1, 0, 0);
-    fps_buffer: [20] u8;
-    fps_str := conv.str_format(fps_buffer, "FPS: {}", cast(i32) hb.timer.getFPS());
-    hb.graphics.print(fps_str, ~~hb.window.getWidth() - hb.graphics.getTextWidth(fps_str), 30);
-}
+    hb.graphics.setLineWidth(2);
+    hb.graphics.setColor(0.2, 0.25, 0.03);
+    hb.graphics.circle(.Line, cell_size * ~~the_food.x + cell_size / 2, cell_size * ~~the_food.y + cell_size / 2, cell_size / 2);
+}
\ No newline at end of file