summaryrefslogtreecommitdiff
path: root/src/move_snake.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2026-10-31 12:38:00 -0400
committerMica White <botahamec@outlook.com>2026-10-31 12:38:00 -0400
commit3b39c7561ec71dadb0cb03a153550e9a6e2cddff (patch)
treec4d5dd4b4314371a818053c4c6741209967089b3 /src/move_snake.rs
parent2987e310e8e6eaeaa60546bb92a61d9c3713263a (diff)
OptimizationHEADmain
Diffstat (limited to 'src/move_snake.rs')
-rw-r--r--src/move_snake.rs30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/move_snake.rs b/src/move_snake.rs
index 453e795..4439be5 100644
--- a/src/move_snake.rs
+++ b/src/move_snake.rs
@@ -1,10 +1,11 @@
use core::time::Duration;
-use alloc::vec::Vec;
use rand::RngExt;
use tortuise_script::{random::TortuiseRng, time::Instant, ScriptContext, TortuiseScript};
-use crate::{Position, APPLE_LOCATION, DIRECTION, GRID_SIZE, NEXT_MOVE_TIME, SNAKE_CELLS};
+use crate::{
+ Position, APPLE_LOCATION, DIRECTION, GRID_SIZE, IS_PAUSED, NEXT_MOVE_TIME, SNAKE_CELLS,
+};
pub const MOVE_SNAKE: TortuiseScript = TortuiseScript {
update: |context: ScriptContext| {
@@ -12,26 +13,16 @@ pub const MOVE_SNAKE: TortuiseScript = TortuiseScript {
let mut snake_cells = SNAKE_CELLS.get_mut(&context);
let mut next_move = NEXT_MOVE_TIME.get_mut(&context);
let direction = DIRECTION.get(&context);
-
- let mut available_squares =
- Vec::with_capacity((GRID_SIZE * GRID_SIZE) as usize - snake_cells.len());
- for i in 0..GRID_SIZE {
- for j in 0..GRID_SIZE {
- let position = Position {
- x: i as i16,
- y: j as i16,
- };
- if !snake_cells.contains(&position) {
- available_squares.push(position);
- }
- }
- }
+ let is_paused = IS_PAUSED.get(&context);
let mut rng = TortuiseRng::new();
let time = Instant::now(&context);
- if time > *next_move {
+ if time > *next_move && !*is_paused {
if snake_cells[0] == *apple_location {
- *apple_location = available_squares[rng.random_range(0..available_squares.len())];
+ *apple_location = Position {
+ x: rng.random_range(0..GRID_SIZE as i16),
+ y: rng.random_range(0..GRID_SIZE as i16),
+ };
let new_head = Position {
x: snake_cells[0].x + direction.x,
y: snake_cells[0].y + direction.y,
@@ -45,6 +36,9 @@ pub const MOVE_SNAKE: TortuiseScript = TortuiseScript {
snake_cells[0].x += direction.x;
snake_cells[0].y += direction.y;
}
+ if time > *next_move {
+ *next_move += Duration::from_millis(200);
+ }
},
..TortuiseScript::empty()
};