Spawn and draw apple

This commit is contained in:
Yash Karandikar 2022-04-30 11:08:08 -05:00
parent ec73756b2c
commit 9b1dfa83eb
Signed by: karx
GPG Key ID: A794DA2529474BA5
4 changed files with 65 additions and 0 deletions

54
Cargo.lock generated
View File

@ -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"

View File

@ -7,4 +7,5 @@ edition = "2021"
[dependencies]
cat-box = "0.1.4"
rand = "0.8.5"
sdl2 = "0.35.2"

BIN
apple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 B

View File

@ -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| {
@ -66,6 +75,7 @@ fn main() {
// 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();