summaryrefslogtreecommitdiff
path: root/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/render.rs b/src/render.rs
new file mode 100644
index 0000000..80b06f7
--- /dev/null
+++ b/src/render.rs
@@ -0,0 +1,131 @@
+use alloc::string::ToString;
+use tortuise_script::{
+ buffer::{Cell, Color},
+ ScriptContext, TortuiseScript,
+};
+
+use crate::{APPLE_LOCATION, GRID_SIZE, SNAKE_CELLS};
+
+fn draw_box(cx: &mut ScriptContext, char: char, x: u16, y: u16) {
+ for i in 1..5 {
+ cx.add_cell(Cell {
+ character: '-',
+ x: x + i,
+ y,
+ ..Default::default()
+ });
+ cx.add_cell(Cell {
+ character: '-',
+ x: x + i,
+ y: y + 2,
+ ..Default::default()
+ });
+ }
+ cx.add_cell(Cell {
+ character: '|',
+ x,
+ y: y + 1,
+ ..Default::default()
+ });
+ cx.add_cell(Cell {
+ character: '|',
+ x: x + 5,
+ y: y + 1,
+ ..Default::default()
+ });
+ cx.add_cell(Cell {
+ character: char,
+ x: x + 3,
+ y: y + 1,
+ ..Default::default()
+ });
+}
+
+pub const RENDER: TortuiseScript = TortuiseScript {
+ update: |mut context: ScriptContext| {
+ let apple_location = APPLE_LOCATION.get(&context);
+ let snake_cells = SNAKE_CELLS.get(&context);
+ let window_size = context.window_size();
+ let offset_x = window_size.columns.saturating_sub(GRID_SIZE) / 2;
+ let offset_y = window_size.rows.saturating_sub(GRID_SIZE) / 2;
+
+ for i in 0..GRID_SIZE {
+ context.add_cell(Cell {
+ character: '|',
+ x: offset_x.saturating_sub(1),
+ y: offset_y + i,
+ z: 0,
+ ..Default::default()
+ });
+ context.add_cell(Cell {
+ character: '|',
+ x: offset_x + GRID_SIZE,
+ y: offset_y + i,
+ z: 0,
+ ..Default::default()
+ });
+ context.add_cell(Cell {
+ character: '-',
+ x: offset_x + i,
+ y: offset_y.saturating_sub(1),
+ z: 0,
+ ..Default::default()
+ });
+ context.add_cell(Cell {
+ character: '-',
+ x: offset_x + i,
+ y: offset_y + GRID_SIZE,
+ z: 0,
+ ..Default::default()
+ });
+ }
+
+ for (i, char) in "Score:".chars().enumerate() {
+ context.add_cell(Cell {
+ character: char,
+ x: offset_x + GRID_SIZE - 10 + i as u16,
+ y: offset_y.saturating_sub(2),
+ ..Default::default()
+ });
+ }
+
+ draw_box(&mut context, '<', 4, 4);
+ draw_box(&mut context, '>', 16, 4);
+ draw_box(&mut context, '^', 10, 1);
+ draw_box(&mut context, 'v', 10, 7);
+
+ for (i, char) in (snake_cells.len() - 2)
+ .to_string()
+ .chars()
+ .rev()
+ .enumerate()
+ {
+ context.add_cell(Cell {
+ character: char,
+ x: offset_x + GRID_SIZE - i as u16,
+ y: offset_y.saturating_sub(2),
+ ..Default::default()
+ });
+ }
+
+ context.add_cell(Cell {
+ character: '●',
+ foreground_color: Color::Red,
+ x: apple_location.x.unsigned_abs() + offset_x,
+ y: apple_location.y.unsigned_abs() + offset_y,
+ z: 0,
+ ..Default::default()
+ });
+ for cell in &*snake_cells {
+ context.add_cell(Cell {
+ character: '█',
+ foreground_color: Color::Green,
+ x: cell.x.unsigned_abs() + offset_x,
+ y: cell.y.unsigned_abs() + offset_y,
+ z: 1,
+ ..Default::default()
+ });
+ }
+ },
+ ..TortuiseScript::empty()
+};