Compare commits

...

2 commits

Author SHA1 Message Date
Yash Karandikar 8be294c143 Fix double-press issue 2023-02-28 18:51:41 -06:00
Yash Karandikar d49b4bc683 Include assets directly (except for font) 2023-02-28 18:42:03 -06:00
4 changed files with 29 additions and 19 deletions

5
.cargo/config.toml Normal file
View file

@ -0,0 +1,5 @@
[source]
[source.mirror]
registry = "https://git.karx.xyz/gallant/crates.io-index.git"
[source.crates-io]
replace-with = "mirror"

4
Cargo.lock generated
View file

@ -10,9 +10,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cat-box"
version = "0.1.6"
version = "22.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24bda0d6fb9dc5a286c9afe6286b1de1f3fe7175310b4b0c2c34174fccafbd19"
checksum = "718fced54b0fc65b4c154bad8cbd2826447a91a2c2d3980a94cd7e2beadcb73b"
dependencies = [
"sdl2",
]

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cat-box = "0.1.6"
cat-box = "22.6.21"
rand = "0.8.5"
sdl2 = "0.35.2"

View file

@ -29,7 +29,7 @@ fn main() {
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(include_bytes!("../snakecell.png"), x * 37, y * 37).unwrap();
snake.push(s);
}
@ -37,7 +37,7 @@ fn main() {
let x = thread_rng().gen_range(0..=27);
let y = thread_rng().gen_range(0..=27);
Sprite::new("apple.png", x * 37, y * 37).unwrap()
Sprite::from_bytes(include_bytes!("../apple.png"), x * 37, y * 37).unwrap()
};
let mut dir = Direction::Left;
@ -46,6 +46,8 @@ fn main() {
let mut time = Instant::now();
let mut last_pressed = Scancode::A;
game.run(|ctx| {
draw_text(
ctx,
@ -62,28 +64,31 @@ fn main() {
let keys = get_keyboard_state(ctx).keys;
for key in keys {
use Direction::*;
match key {
Scancode::Q | Scancode::Escape => {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
}
s => last_pressed = s,
};
}
if time.elapsed().as_millis() >= 125 {
use Direction::*;
match last_pressed {
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),
_ => (),
};
}
if time.elapsed().as_millis() >= 125 {
}
{
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 (lastx, lasty) = last_part.into();
let (x, y) = s.position().into();
let (xdiff, ydiff) = (lastx - x, y - lasty);
last_part = s.position();
s.translate((xdiff, ydiff));
@ -120,19 +125,19 @@ fn main() {
let x = thread_rng().gen_range(0..=27) * 37;
let y = thread_rng().gen_range(0..=27) * 37;
let (currx, curry) = apple.position();
let (currx, curry) = apple.position().into();
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 direc = check_direction(last.into(), second_to_last.into());
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),
Direction::Left => (last.x - 37, last.y),
Direction::Right => (last.x + 37, last.y),
Direction::Up => (last.x, last.y - 37),
Direction::Down => (last.x, last.y + 37),
};
let s = Sprite::new("snakecell.png", newx, newy).unwrap();
@ -142,7 +147,7 @@ fn main() {
}
{
let (mut x, mut y) = snake[0].position();
let (mut x, mut y) = snake[0].position().into();
x /= 37;
y /= 37;