This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elmariomomento/src/main.rs
2022-05-21 14:01:50 -05:00

70 lines
1.8 KiB
Rust

use cat_box::{draw_text, Game, Sprite, SpriteCollection, get_keyboard_state};
use std::{thread, time};
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 sprites = SpriteCollection::new();
let mut run1 = Sprite::new("runner_0.png", 200, 250).unwrap();
let mut run2 = Sprite::new("runner_1.png", 300, 250).unwrap();
let mut stand = Sprite::new("runner_s.png", 400, 250).unwrap();
sprites.push(run1);
sprites.push(run2);
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;
}
for key in keys {
let state = match key {
Scancode::Left | Scancode::Right => 0,
_ => 2,
};
let offset = match key {
Scancode::Escape => {
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),
};
let spriteindex = sprites.get_mut(state).unwrap();
spriteindex.translate(offset);
}
ctx.set_background_colour( 129, 201, i);
sprites.draw(ctx).unwrap();
}).unwrap();
}