This commit is contained in:
gallant 2022-05-22 07:02:13 -05:00
parent 1b890d73eb
commit 6a08149216
5 changed files with 51 additions and 22 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
MesloLGS.ttf Normal file

Binary file not shown.

BIN
grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -1,52 +1,79 @@
use cat_box::{draw_text, Game, Sprite, SpriteCollection, get_keyboard_state}; use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text};
use std::{thread, time}; use std::{thread};
use sdl2::keyboard::Scancode; use sdl2::keyboard::Scancode;
struct state { struct State {
running: bool, running: bool,
jumping: bool jumping: bool
} }
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;
let mut sprites = 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, 250).unwrap();
let mut run2 = Sprite::new("runner_1.png", 300, 250).unwrap(); let sun = Sprite::new("sun.png", 50, 50).unwrap();
let mut stand = Sprite::new("runner_s.png", 400, 250).unwrap(); let grass = Sprite::new("grass.png", 250, 450).unwrap();
sprites.push(run1); background.push(sun);
sprites.push(run2);
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{
i=200; 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 { for key in keys {
let state = match key { let state = match key {
Scancode::Left | Scancode::Right => 0, Scancode::Left | Scancode::Right => 0,
_ => 2, _ => 0,
}; };
let offset = match key { let offset = match key {
Scancode::Escape => { Scancode::Escape | Scancode::Q => {
game.terminate(); game.terminate();
(0, 0) (0, 0)
} }
@ -57,13 +84,15 @@ fn main() {
_ => (0, 0), _ => (0, 0),
}; };
let spriteindex = sprites.get_mut(state).unwrap();
spriteindex.translate(offset); run1.translate(offset);
} }
ctx.set_background_colour( 129, 201, i); ctx.set_background_colour(129, 201, i);
sprites.draw(ctx).unwrap(); background.draw(ctx).unwrap();
run1.draw(ctx).unwrap();
}).unwrap(); }).unwrap();
} }

BIN
sun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 B