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

138 lines
3.6 KiB
Rust
Raw Normal View History

2022-05-25 22:05:24 -05:00
use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text,physics::*,vec2::*};
use std::{time};
2022-05-21 13:08:14 -05:00
use sdl2::keyboard::Scancode;
2022-05-21 10:57:13 -05:00
2022-05-25 08:16:39 -05:00
2022-05-23 07:09:33 -05:00
/*
keep track of accel, vel and pos
every tick:
1. pos += vel
2. vel += accel
3. accel = (calculate from arrow keys) + (0, -9.8)
*/
2022-05-20 19:29:16 -05:00
fn main() {
let game = Game::new("Running", 500, 500);
//main sprite introductions
let mut i = 255u8;
2022-05-22 14:29:16 -05:00
//gravity setup
2022-05-23 07:09:33 -05:00
//let meter = 13;
2022-05-25 22:05:24 -05:00
let now = time::Instant::now();
let mut pos = Vec2::new(0.0,0.0);
let mut vel = Vec2::new(0.0,0.0);
let mut acc = Vec2::new(0.0,0.0);
2022-05-25 08:16:39 -05:00
2022-05-22 07:02:13 -05:00
let mut background = SpriteCollection::new();
2022-05-23 07:09:33 -05:00
let mut run1 = Sprite::new("runner_0.png", 200, 320).unwrap();
2022-05-22 07:02:13 -05:00
let sun = Sprite::new("sun.png", 50, 50).unwrap();
let grass = Sprite::new("grass.png", 250, 450).unwrap();
2022-05-25 08:16:39 -05:00
/*
let mut grav = Gravity::new(2.0);
grav.set(0.0, 3.0);
*/
2022-05-22 07:02:13 -05:00
background.push(sun);
background.push(grass);
/*
2022-05-21 10:57:13 -05:00
sprites.push(stand);
2022-05-23 07:09:33 -05:00
2022-05-22 07:02:13 -05:00
let _guy = State {
2022-05-21 10:57:13 -05:00
running: false,
jumping: false
2022-05-23 07:09:33 -05:00
};*/
2022-05-21 10:57:13 -05:00
2022-05-20 19:29:16 -05:00
//sleeper agent
2022-05-23 07:09:33 -05:00
//let sleep = thread::sleep;
2022-05-25 22:05:24 -05:00
//let step = time::Duration::from_millis;
2022-05-20 19:29:16 -05:00
//main game loop
game.run(|ctx|{
2022-05-21 10:57:13 -05:00
let keys = get_keyboard_state(ctx).keys;
2022-05-22 07:02:13 -05:00
2022-05-23 07:09:33 -05:00
2022-05-20 19:29:16 -05:00
if i<200{
i = i + 10;
}else if i >= 255{
i=200;
}
2022-05-21 10:57:13 -05:00
2022-05-22 07:02:13 -05:00
let (x,y) = run1.position();
2022-05-25 22:05:24 -05:00
let check = check_for_collision(&run1, &background.get(1).unwrap());
2022-05-22 07:02:13 -05:00
if x < 0 {
run1.translate((x*-1+5,0));
} else if x > 500 {
run1.translate((500-x-5,0));
}
2022-05-25 08:16:39 -05:00
//- run1.rect.height() as i32
2022-05-22 07:02:13 -05:00
if y < 0{
2022-05-23 07:09:33 -05:00
run1.translate((0,y - run1.rect.height() as i32))
2022-05-22 07:02:13 -05:00
}else if y > 320 {
run1.translate((0,5));
}
2022-05-25 22:05:24 -05:00
if !check {
run1.translate((0,-2));
}
2022-05-25 08:16:39 -05:00
2022-05-23 07:09:33 -05:00
2022-05-22 07:02:13 -05:00
draw_text(
ctx,
2022-05-22 14:29:16 -05:00
format!("x is {} and y is {}, {:?} Seconds have passed", x, y, now.elapsed().as_secs()),
2022-05-22 07:02:13 -05:00
"MesloLGS.ttf",
2022-05-22 14:29:16 -05:00
15,
(250,100),
2022-05-22 07:02:13 -05:00
cat_box::TextMode::Shaded {
foreground: (255, 255, 255),
background: (0, 0, 0),
},
).unwrap();
2022-05-25 22:05:24 -05:00
draw_text(ctx,
format!("tf: {:?}", check),
"MesloLGS.ttf",
15,
(250 , 200),
cat_box::TextMode::Shaded {
foreground: (255, 255, 255),
background: (0, 0, 0),
},
).unwrap();
2022-05-22 07:02:13 -05:00
2022-05-21 13:08:14 -05:00
for key in keys {
2022-05-25 22:05:24 -05:00
/*let jumper = match key {
Scancode::Up => (0,150,true),
_ => (0,0,false),
};*/
2022-05-21 10:57:13 -05:00
let offset = match key {
2022-05-22 07:02:13 -05:00
Scancode::Escape | Scancode::Q => {
2022-05-21 10:57:13 -05:00
game.terminate();
(0, 0)
2022-05-25 22:05:24 -05:00
}
Scancode::W | Scancode::Up => (0,-5),
Scancode::S | Scancode::Down => (0, 5),
2022-05-21 10:57:13 -05:00
Scancode::A | Scancode::Left => (-5, 0),
Scancode::D | Scancode::Right => (5, 0),
_ => (0, 0),
2022-05-22 14:29:16 -05:00
};
2022-05-25 22:05:24 -05:00
let offsetvec = Vec2::new(offset.0 as f32, offset.1 as f32);
pos += vel;
vel += acc;
acc = Vec2::new(0.0, -9.8) + offsetvec;
//run1.translate((pos.0))
run1.translate((pos.x as i32,pos.y as i32));
2022-05-25 08:16:39 -05:00
2022-05-22 14:29:16 -05:00
2022-05-23 07:09:33 -05:00
2022-05-22 07:02:13 -05:00
2022-05-21 13:08:14 -05:00
}
2022-05-22 07:02:13 -05:00
ctx.set_background_colour(129, 201, i);
background.draw(ctx).unwrap();
run1.draw(ctx).unwrap();
2022-05-20 19:29:16 -05:00
}).unwrap();
}