Compare commits

...

4 commits

6 changed files with 164 additions and 122 deletions

45
Cargo.lock generated
View file

@ -10,18 +10,18 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cat-box"
version = "0.1.6"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24bda0d6fb9dc5a286c9afe6286b1de1f3fe7175310b4b0c2c34174fccafbd19"
checksum = "eea5a8bcf51d0820a0935f7dd1ad9496a43aa7dbdc9ce075272cd9d46942d726"
dependencies = [
"sdl2",
]
[[package]]
name = "cc"
version = "1.0.73"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d"
[[package]]
name = "cfg-if"
@ -31,18 +31,24 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cmake"
version = "0.1.48"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a"
checksum = "db34956e100b30725f2eb215f90d4871051239535632f84fea3bc92722c66b7c"
dependencies = [
"cc",
]
[[package]]
name = "getrandom"
version = "0.2.6"
name = "dyn_vec"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
checksum = "f2a0d94db0de011b713cd3a0c9d273ca2df5d9f23c3a55ea43c1fe876faa61f0"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
@ -57,15 +63,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.125"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
@ -90,9 +96,9 @@ dependencies = [
[[package]]
name = "rand_core"
version = "0.6.3"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
@ -126,18 +132,19 @@ name = "snake"
version = "0.1.0"
dependencies = [
"cat-box",
"dyn_vec",
"rand",
"sdl2",
]
[[package]]
name = "version-compare"
version = "0.1.0"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

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"

BIN
grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
grid2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -2,7 +2,7 @@ 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;
use std::time::Instant;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Direction {
@ -13,47 +13,80 @@ 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;
}
};
}
fn main() {
let game = Game::new("Snake", 1000, 1000);
let snake_boxes: Vec<(i32, i32)> = vec![(13, 13), (14, 13)];
//hardcoding size to make this game as inconvient as you can
let game = Game::new("Snake", 1036, 1036);
//NO NEED FOR LOCAL RUNNING, ALL IN EXECUTABLE
let snake_bytes = include_bytes!("../snakecell.png");
let apple_bytes = include_bytes!("../apple.png");
let wall_bytes = include_bytes!("../wall.png");
let grid_bytes = include_bytes!("../grid2.png");
//INPROG GRID SYSTEM INSTEAD OF DIRECT POSITION MANAGEMENT
//SNAKE TRAIN
let snake_boxes: Vec<(i32, i32)> = vec![(13, 13), (14, 13)];
let mut snake = SpriteCollection::with_capacity(snake_boxes.len());
for (x, y) in snake_boxes {
let s = Sprite::new("snakecell.png", x * 37, y * 37).unwrap();
let s = Sprite::from_bytes(snake_bytes, x * 37, y * 37).unwrap();
snake.push(s);
}
//RANDOM APPLE SPAWNING
let mut apple = {
let x = thread_rng().gen_range(0..=27);
let y = thread_rng().gen_range(0..=27);
let x = thread_rng().gen_range(2..=26);
let y = thread_rng().gen_range(2..=26);
Sprite::new("apple.png", x * 37, y * 37).unwrap()
Sprite::from_bytes(apple_bytes, x * 37, y * 37).unwrap()
};
let mut dir = Direction::Left;
//I am lazy don't @ me and it WORKS
let mut walls = {
let mut wall_boxes = SpriteCollection::with_capacity(27);
let mut num = 999;
while num >= 1 {
let wall = Sprite::from_bytes(wall_bytes, num, 37).unwrap();
wall_boxes.push(wall);
num -= 37;
}
while num < 999 {
num += 37;
let wall = Sprite::from_bytes(wall_bytes, num, 999).unwrap();
wall_boxes.push(wall);
}
while num >= 1 {
let wall = Sprite::from_bytes(wall_bytes, 37, num).unwrap();
wall_boxes.push(wall);
num -= 37;
}
while num < 999 {
num += 37;
let wall = Sprite::from_bytes(wall_bytes, 999, num).unwrap();
wall_boxes.push(wall);
}
wall_boxes
};
//GRID IMAGE
let mut grid = Sprite::from_bytes(grid_bytes, 499 + (37 / 2), 499 + (37 / 2)).unwrap();
//DEFAULT DIRECTION
let mut dir = Direction::Left;
let mut dir2 = Direction::Left;
let mut queue: Vec<Direction> = Vec::new();
let mut score = 0u64;
let mut now = Instant::now();
game.run(|ctx| {
draw_text(
ctx,
format!("Score: {}", score),
"ibm_bios-2y.ttf",
36,
(100, 100),
cat_box::TextMode::Transparent {
colour: (255, 255, 255),
},
)
.unwrap();
let keys = get_keyboard_state(ctx).keys;
for key in keys {
@ -64,105 +97,106 @@ 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),
_ => (),
};
}
{
let mut last_part = snake[0].position();
if now.elapsed().as_millis() >= 125 {
dir2 = dir.clone();
{
let mut last_part = snake[0].position();
for s in snake.iter().skip(1) {
let (lastx, lasty) = last_part;
let (x, y) = s.position();
let (xdiff, ydiff) = (lastx - x, y - lasty);
last_part = s.position();
s.translate((xdiff, ydiff));
for s in snake.iter().skip(1) {
let (lastx, lasty) = last_part;
let (x, y) = s.position();
let (xdiff, ydiff) = (lastx - x, y - lasty);
last_part = s.position();
s.translate((xdiff, ydiff));
}
}
}
// The snake head needs to be moved after the body or else the body will just collapse into the head
// 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));
}
};
{
let hitted = cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
if hitted.len() > 1 {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
}
}
if !cat_box::physics::check_for_collision_with_collection(&apple, &snake).is_empty() {
let x = thread_rng().gen_range(0..=27) * 37;
let y = thread_rng().gen_range(0..=27) * 37;
let (currx, curry) = apple.position();
let (xdiff, ydiff) = (x - currx, curry - y);
apple.translate((xdiff, ydiff));
let second_to_last = snake[snake.len() - 2].position();
let last = snake[snake.len() - 1].position();
let direc = check_direction(last, second_to_last);
let (newx, newy) = match direc {
Direction::Left => (last.0 - 37, last.1),
Direction::Right => (last.0 + 37, last.1),
Direction::Up => (last.0, last.1 - 37),
Direction::Down => (last.0, last.1 + 37),
match dir2 {
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));
}
};
//queue.remove(0);
let s = Sprite::new("snakecell.png", newx, newy).unwrap();
snake.push(s);
{
let hitted_self =
cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
let hitted_wall =
cat_box::physics::check_for_collision_with_collection(&snake[0], &walls);
score += 1;
}
{
let (mut x, mut y) = snake[0].position();
x /= 37;
y /= 37;
if dir == Direction::Left || dir == Direction::Right {
if x <= 0 {
let diff = (27 * 37) - (x * 37);
snake[0].translate((diff, 0));
} else if x >= 27 {
let diff = 0 - (x * 37);
snake[0].translate((diff, 0));
if hitted_self.len() > 1 || hitted_wall.len() > 0 {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
}
}
if dir == Direction::Up || dir == Direction::Down {
if y <= 0 {
let diff = (y * 37) - (27 * 37);
snake[0].translate((0, diff));
} else if y >= 27 {
snake[0].translate((0, (y * 37)));
}
if !cat_box::physics::check_for_collision_with_collection(&apple, &snake).is_empty() {
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);
apple.translate((xdiff, ydiff));
let second_to_last = snake[snake.len() - 2].position();
let last = snake[snake.len() - 1].position();
let direc = check_direction(last, second_to_last);
let (newx, newy) = match direc {
Direction::Left => (last.0 - 37, last.1),
Direction::Right => (last.0 + 37, last.1),
Direction::Up => (last.0, last.1 - 37),
Direction::Down => (last.0, last.1 + 37),
};
let s = Sprite::new("snakecell.png", newx, newy).unwrap();
snake.push(s);
score += 1;
}
now = Instant::now();
}
// So that the snake doesn't move at super speed
std::thread::sleep(Duration::from_millis(125));
apple.draw(ctx).unwrap();
//std::thread::sleep(Duration::from_millis(125));
snake.draw(ctx).unwrap();
walls.draw(ctx).unwrap();
grid.draw(ctx).unwrap();
apple.draw(ctx).unwrap();
//draw score above all else
draw_text(
ctx,
format!("Score: {} Dir: {:?}", score, dir),
"ibm_bios-2y.ttf",
36,
(100, 100),
cat_box::TextMode::Transparent {
colour: (213, 123, 212),
},
)
.unwrap();
})
.unwrap();
}

BIN
wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB