not working, for external reference :)

This commit is contained in:
gallant 2023-01-21 14:26:46 -06:00
parent 4014c51fcc
commit 2f67f84388
3 changed files with 45 additions and 26 deletions

7
Cargo.lock generated
View file

@ -38,6 +38,12 @@ dependencies = [
"cc",
]
[[package]]
name = "dyn_vec"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2a0d94db0de011b713cd3a0c9d273ca2df5d9f23c3a55ea43c1fe876faa61f0"
[[package]]
name = "getrandom"
version = "0.2.8"
@ -126,6 +132,7 @@ name = "snake"
version = "0.1.0"
dependencies = [
"cat-box",
"dyn_vec",
"rand",
"sdl2",
]

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
cat-box = "0.1.6"
dyn_vec = "0.1.0"
rand = "0.8.5"
sdl2 = "0.35.2"

View file

@ -2,7 +2,8 @@ use cat_box::{draw_text, get_keyboard_state, Game, Sprite, SpriteCollection};
use rand::thread_rng;
use rand::Rng;
use sdl2::keyboard::Scancode;
use std::time::{Duration, Instant};
use std::time::Instant;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Direction {
@ -13,9 +14,10 @@ enum Direction {
}
macro_rules! set_if_not_opp {
($i:ident, $e:expr, $opp:expr) => {
($i:ident, $e:expr, $opp:expr, $v:expr) => {
if $i != $opp {
$i = $e
$v.push(&$e);
$i = $e;
}
};
}
@ -42,8 +44,8 @@ fn main() {
//RANDOM APPLE SPAWNING
let mut apple = {
let x = thread_rng().gen_range(1..=26);
let y = thread_rng().gen_range(1..=26);
let x = thread_rng().gen_range(2..=26);
let y = thread_rng().gen_range(2..=26);
Sprite::from_bytes(apple_bytes, x * 37, y * 37).unwrap()
};
@ -80,7 +82,7 @@ fn main() {
//DEFAULT DIRECTION
let mut dir = Direction::Left;
let mut queue: Vec<&Direction> = Vec::with_capacity(3);
let mut score = 0u64;
let mut now = Instant::now();
@ -95,10 +97,10 @@ fn main() {
println!("Your score was: {}", score);
game.terminate();
}
Scancode::W | Scancode::Up => set_if_not_opp!(dir, Up, Down),
Scancode::A | Scancode::Left => set_if_not_opp!(dir, Left, Right),
Scancode::S | Scancode::Down => set_if_not_opp!(dir, Down, Up),
Scancode::D | Scancode::Right => set_if_not_opp!(dir, Right, Left),
Scancode::W | Scancode::Up => set_if_not_opp!(dir, Up, Down, queue),
Scancode::A | Scancode::Left => set_if_not_opp!(dir, Left, Right, queue),
Scancode::S | Scancode::Down => set_if_not_opp!(dir, Down, Up, queue),
Scancode::D | Scancode::Right => set_if_not_opp!(dir, Right, Left, queue),
_ => (),
};
}
@ -116,20 +118,27 @@ fn main() {
}
}
// The snake head needs to be moved after the body or else the body will just collapse into the head
match dir {
Direction::Up => {
snake[0].translate((0, 37));
}
Direction::Left => {
snake[0].translate((-37, 0));
}
Direction::Down => {
snake[0].translate((0, -37));
}
Direction::Right => {
snake[0].translate((37, 0));
}
};
match *queue[0] {
Direction::Up => {
snake[0].translate((0, 37));
}
Direction::Left => {
snake[0].translate((-37, 0));
}
Direction::Down => {
snake[0].translate((0, -37));
}
Direction::Right => {
snake[0].translate((37, 0));
}
};
let _ = &mut queue.remove(0);
{
let hitted_self =
@ -145,8 +154,8 @@ fn main() {
}
if !cat_box::physics::check_for_collision_with_collection(&apple, &snake).is_empty() {
let x = thread_rng().gen_range(1..=26) * 37;
let y = thread_rng().gen_range(1..=26) * 37;
let x = thread_rng().gen_range(2..=26) * 37;
let y = thread_rng().gen_range(2..=26) * 37;
let (currx, curry) = apple.position();
let (xdiff, ydiff) = (x - currx, curry - y);
@ -168,8 +177,10 @@ fn main() {
score += 1;
}
now = Instant::now();
}
queue.push(&dir);
// So that the snake doesn't move at super speed
//std::thread::sleep(Duration::from_millis(125));