./.
This commit is contained in:
parent
cc9a59784d
commit
da847a73b2
|
@ -16,9 +16,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cat-box"
|
name = "cat-box"
|
||||||
version = "0.1.6"
|
version = "0.1.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "24bda0d6fb9dc5a286c9afe6286b1de1f3fe7175310b4b0c2c34174fccafbd19"
|
checksum = "eea5a8bcf51d0820a0935f7dd1ad9496a43aa7dbdc9ce075272cd9d46942d726"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"sdl2",
|
"sdl2",
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,6 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cat-box = "0.1.6"
|
cat-box = "0.1.7"
|
||||||
sdl2 = "0.35.2"
|
sdl2 = "0.35.2"
|
||||||
timer = "0.1.3"
|
timer = "0.1.3"
|
57
src/main.rs
57
src/main.rs
|
@ -1,24 +1,28 @@
|
||||||
use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text};
|
use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text};
|
||||||
use std::{thread, time::{Instant, SystemTime}};
|
use std::{time::{Instant}};
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::Scancode;
|
||||||
|
|
||||||
struct State {
|
/*
|
||||||
running: bool,
|
keep track of accel, vel and pos
|
||||||
jumping: bool
|
every tick:
|
||||||
}
|
1. pos += vel
|
||||||
|
2. vel += accel
|
||||||
|
3. accel = (calculate from arrow keys) + (0, -9.8)
|
||||||
|
*/
|
||||||
fn main() {
|
fn main() {
|
||||||
let game = Game::new("Running", 500, 500);
|
let game = Game::new("Running", 500, 500);
|
||||||
|
|
||||||
//main sprite introductions
|
//main sprite introductions
|
||||||
let mut i = 255u8;
|
let mut i = 255u8;
|
||||||
//gravity setup
|
//gravity setup
|
||||||
let meter = 13;
|
//let meter = 13;
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let mut jumped = 0;
|
|
||||||
|
|
||||||
|
//let (mut pos, mut vel, mut accel) = (20.0,0.0,-9.8);
|
||||||
|
|
||||||
let mut background = SpriteCollection::new();
|
let mut background = SpriteCollection::new();
|
||||||
let mut run1 = Sprite::new("runner_0.png", 200, 250).unwrap();
|
let mut run1 = Sprite::new("runner_0.png", 200, 320).unwrap();
|
||||||
let sun = Sprite::new("sun.png", 50, 50).unwrap();
|
let sun = Sprite::new("sun.png", 50, 50).unwrap();
|
||||||
let grass = Sprite::new("grass.png", 250, 450).unwrap();
|
let grass = Sprite::new("grass.png", 250, 450).unwrap();
|
||||||
|
|
||||||
|
@ -27,18 +31,20 @@ fn main() {
|
||||||
background.push(grass);
|
background.push(grass);
|
||||||
/*
|
/*
|
||||||
sprites.push(stand);
|
sprites.push(stand);
|
||||||
*/
|
|
||||||
let _guy = State {
|
let _guy = State {
|
||||||
running: false,
|
running: false,
|
||||||
jumping: false
|
jumping: false
|
||||||
};
|
};*/
|
||||||
|
|
||||||
//sleeper agent
|
//sleeper agent
|
||||||
let sleep = thread::sleep;
|
//let sleep = thread::sleep;
|
||||||
//main game loop
|
//main game loop
|
||||||
game.run(|ctx|{
|
game.run(|ctx|{
|
||||||
let keys = get_keyboard_state(ctx).keys;
|
let keys = get_keyboard_state(ctx).keys;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if i<200{
|
if i<200{
|
||||||
i = i + 10;
|
i = i + 10;
|
||||||
}else if i >= 255{
|
}else if i >= 255{
|
||||||
|
@ -54,15 +60,13 @@ fn main() {
|
||||||
} else if x > 500 {
|
} else if x > 500 {
|
||||||
run1.translate((500-x-5,0));
|
run1.translate((500-x-5,0));
|
||||||
}
|
}
|
||||||
|
|
||||||
if y < 0{
|
if y < 0{
|
||||||
run1.translate((0,y*1-5))
|
run1.translate((0,y - run1.rect.height() as i32))
|
||||||
}else if y > 320 {
|
}else if y > 320 {
|
||||||
run1.translate((0,5));
|
run1.translate((0,5));
|
||||||
}
|
}
|
||||||
while y != 320 && y < 325 {
|
|
||||||
run1.translate((0,-1));
|
|
||||||
break
|
|
||||||
}
|
|
||||||
draw_text(
|
draw_text(
|
||||||
ctx,
|
ctx,
|
||||||
format!("x is {} and y is {}, {:?} Seconds have passed", x, y, now.elapsed().as_secs()),
|
format!("x is {} and y is {}, {:?} Seconds have passed", x, y, now.elapsed().as_secs()),
|
||||||
|
@ -74,7 +78,6 @@ fn main() {
|
||||||
background: (0, 0, 0),
|
background: (0, 0, 0),
|
||||||
},
|
},
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
|
|
||||||
for key in keys {
|
for key in keys {
|
||||||
let offset = match key {
|
let offset = match key {
|
||||||
|
@ -82,27 +85,15 @@ fn main() {
|
||||||
game.terminate();
|
game.terminate();
|
||||||
(0, 0)
|
(0, 0)
|
||||||
}
|
}
|
||||||
|
//Scancode::W | Scancode::Up => (0, 5),
|
||||||
Scancode::S | Scancode::Down => (0, -5),
|
Scancode::S | Scancode::Down => (0, -5),
|
||||||
Scancode::A | Scancode::Left => (-5, 0),
|
Scancode::A | Scancode::Left => (-5, 0),
|
||||||
Scancode::D | Scancode::Right => (5, 0),
|
Scancode::D | Scancode::Right => (5, 0),
|
||||||
_ => (0, 0),
|
_ => (0, 0),
|
||||||
};
|
};
|
||||||
run1.translate(offset);
|
run1.translate((offset.0,offset.1 ));
|
||||||
|
|
||||||
let jumpy = match key {
|
|
||||||
Scancode::W | Scancode::Up => (0, 120),
|
|
||||||
_ => (0,0),
|
|
||||||
};
|
|
||||||
|
|
||||||
while jumped == 0{
|
|
||||||
run1.translate(jumpy);
|
|
||||||
|
|
||||||
jumped = 1;
|
|
||||||
}
|
|
||||||
if y == 320 {
|
|
||||||
jumped = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue