initial commit, add grid and walls, currently trying to figure out good apple spawning and good time management

This commit is contained in:
gallant 2023-01-19 14:18:57 -06:00
parent c2cb3b4915
commit e1eb3fea3a
5 changed files with 116 additions and 84 deletions

36
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,18 @@ 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"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
@ -57,15 +57,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 +90,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",
]
@ -132,12 +132,12 @@ dependencies = [
[[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"

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::{Duration, Instant};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Direction {
@ -21,39 +21,70 @@ macro_rules! set_if_not_opp {
}
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(1..=26);
let y = thread_rng().gen_range(1..=26);
Sprite::new("apple.png", x * 37, y * 37).unwrap()
Sprite::from_bytes(apple_bytes, x * 37, y * 37).unwrap()
};
//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 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 {
@ -83,26 +114,35 @@ fn main() {
s.translate((xdiff, ydiff));
}
}
let check = Instant::now();
println!("{:?}",check.duration_since(now).as_millis());
if check.duration_since(now).as_millis() >= 125 as u128 {
// 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));
}
};
// 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));
}
};
now = Instant::now();
}
{
let hitted = cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
if hitted.len() > 1 {
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);
if hitted_self.len() > 1 || hitted_wall.len() > 0 {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
@ -110,8 +150,8 @@ fn main() {
}
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 x = thread_rng().gen_range(1..=26) * 37;
let y = thread_rng().gen_range(1..=26) * 37;
let (currx, curry) = apple.position();
let (xdiff, ydiff) = (x - currx, curry - y);
@ -134,35 +174,27 @@ fn main() {
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 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)));
}
}
}
// 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: {}", score),
"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