99 lines
2.4 KiB
Rust
99 lines
2.4 KiB
Rust
use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text};
|
|
use std::{thread};
|
|
use sdl2::keyboard::Scancode;
|
|
|
|
struct State {
|
|
running: bool,
|
|
jumping: bool
|
|
}
|
|
|
|
fn main() {
|
|
let game = Game::new("Running", 500, 500);
|
|
|
|
//main sprite introductions
|
|
let mut i = 255u8;
|
|
let mut background = SpriteCollection::new();
|
|
let mut run1 = Sprite::new("runner_0.png", 200, 250).unwrap();
|
|
let sun = Sprite::new("sun.png", 50, 50).unwrap();
|
|
let grass = Sprite::new("grass.png", 250, 450).unwrap();
|
|
|
|
background.push(sun);
|
|
|
|
background.push(grass);
|
|
/*
|
|
sprites.push(stand);
|
|
*/
|
|
let _guy = State {
|
|
running: false,
|
|
jumping: false
|
|
};
|
|
|
|
//sleeper agent
|
|
let _sleep = thread::sleep;
|
|
//main game loop
|
|
game.run(|ctx|{
|
|
let keys = get_keyboard_state(ctx).keys;
|
|
|
|
if i<200{
|
|
i = i + 10;
|
|
}else if i >= 255{
|
|
i=200;
|
|
}
|
|
|
|
|
|
let (x,y) = run1.position();
|
|
|
|
|
|
if x < 0 {
|
|
run1.translate((x*-1+5,0));
|
|
} else if x > 500 {
|
|
run1.translate((500-x-5,0));
|
|
}
|
|
if y < 0{
|
|
run1.translate((0,y*1-5))
|
|
}else if y > 320 {
|
|
run1.translate((0,5));
|
|
}
|
|
draw_text(
|
|
ctx,
|
|
format!("x is {} and y is {}", x, y),
|
|
"MesloLGS.ttf",
|
|
30,
|
|
(100,100),
|
|
cat_box::TextMode::Shaded {
|
|
foreground: (255, 255, 255),
|
|
background: (0, 0, 0),
|
|
},
|
|
).unwrap();
|
|
|
|
|
|
for key in keys {
|
|
let state = match key {
|
|
Scancode::Left | Scancode::Right => 0,
|
|
_ => 0,
|
|
};
|
|
let offset = match key {
|
|
Scancode::Escape | Scancode::Q => {
|
|
game.terminate();
|
|
(0, 0)
|
|
}
|
|
Scancode::W | Scancode::Up => (0, 5),
|
|
Scancode::S | Scancode::Down => (0, -5),
|
|
Scancode::A | Scancode::Left => (-5, 0),
|
|
Scancode::D | Scancode::Right => (5, 0),
|
|
_ => (0, 0),
|
|
};
|
|
|
|
|
|
run1.translate(offset);
|
|
|
|
}
|
|
|
|
ctx.set_background_colour(129, 201, i);
|
|
background.draw(ctx).unwrap();
|
|
run1.draw(ctx).unwrap();
|
|
|
|
|
|
}).unwrap();
|
|
}
|