Compare commits
4 commits
4473b0330b
...
e39d35d72f
Author | SHA1 | Date | |
---|---|---|---|
Yash Karandikar | e39d35d72f | ||
Yash Karandikar | 45107b2b3c | ||
Yash Karandikar | 9b1dfa83eb | ||
Yash Karandikar | ec73756b2c |
58
Cargo.lock
generated
58
Cargo.lock
generated
|
@ -10,9 +10,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
|||
|
||||
[[package]]
|
||||
name = "cat-box"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3dda647bd577598f5c8c1f2a9753d033c2d26379a97f78489bde0055e2531c2a"
|
||||
checksum = "6f1c7d6dd0dea2e5869d7d63428606d9d789216b9e1230c6f6057aa1b4d68886"
|
||||
dependencies = [
|
||||
"sdl2",
|
||||
]
|
||||
|
@ -23,6 +23,17 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
@ -35,6 +46,42 @@ version = "0.2.125"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sdl2"
|
||||
version = "0.35.2"
|
||||
|
@ -63,6 +110,7 @@ name = "snake"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cat-box",
|
||||
"rand",
|
||||
"sdl2",
|
||||
]
|
||||
|
||||
|
@ -71,3 +119,9 @@ name = "version-compare"
|
|||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||
|
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cat-box = "0.1.4"
|
||||
cat-box = "0.1.5"
|
||||
rand = "0.8.5"
|
||||
sdl2 = "0.35.2"
|
||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -1,4 +1,6 @@
|
|||
use cat_box::{get_keyboard_state, Game, Sprite, SpriteCollection};
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
use sdl2::keyboard::Scancode;
|
||||
use std::time::Duration;
|
||||
|
||||
|
@ -20,6 +22,13 @@ fn main() {
|
|||
snake.push(s);
|
||||
}
|
||||
|
||||
let mut apple = {
|
||||
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()
|
||||
};
|
||||
|
||||
let mut dir = Direction::Left;
|
||||
|
||||
game.run(|ctx| {
|
||||
|
@ -28,14 +37,27 @@ fn main() {
|
|||
for key in keys {
|
||||
match key {
|
||||
Scancode::Q => game.terminate(),
|
||||
Scancode::W => dir = Direction::Up,
|
||||
Scancode::A => dir = Direction::Left,
|
||||
Scancode::S => dir = Direction::Down,
|
||||
Scancode::D => dir = Direction::Right,
|
||||
Scancode::W | Scancode::Up => dir = Direction::Up,
|
||||
Scancode::A | Scancode::Left => dir = Direction::Left,
|
||||
Scancode::S | Scancode::Down => dir = Direction::Down,
|
||||
Scancode::D | Scancode::Right => dir = Direction::Right,
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
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);
|
||||
s.translate((xdiff, ydiff));
|
||||
last_part = s.position();
|
||||
}
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
@ -51,19 +73,18 @@ fn main() {
|
|||
}
|
||||
};
|
||||
|
||||
{
|
||||
let mut last_part = snake[0].position();
|
||||
if cat_box::physics::check_for_collision(&snake[0], &apple) {
|
||||
let x = thread_rng().gen_range(0..=27) * 37;
|
||||
let y = thread_rng().gen_range(0..=27) * 37;
|
||||
|
||||
for s in &mut *snake {
|
||||
let (lastx, lasty) = last_part;
|
||||
let (x, y) = s.position();
|
||||
let (xdiff, ydiff) = (lastx - x, y - lasty);
|
||||
s.translate((xdiff, ydiff));
|
||||
last_part = s.position();
|
||||
}
|
||||
let (currx, curry) = apple.position();
|
||||
let (xdiff, ydiff) = (x - currx, curry - y);
|
||||
apple.translate((xdiff, ydiff));
|
||||
}
|
||||
|
||||
// So that the snake doesn't move at super speed
|
||||
std::thread::sleep(Duration::from_millis(125));
|
||||
apple.draw(ctx).unwrap();
|
||||
snake.draw(ctx).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
|
Loading…
Reference in a new issue