10个版本 (1个稳定版)
22.6.21 | 2022年6月21日 |
---|---|
0.1.8 | 2022年5月28日 |
0.1.6 | 2022年4月30日 |
0.1.4 | 2022年3月31日 |
#344 在 图形API 中
每月31次下载
755KB
766 代码行
cat-box
正在开发中的游戏引擎,灵感来自 arcade。
入门
将 cat-box
添加到您的 Cargo.toml
,然后按照以下示例操作。阅读 文档 获取更多信息。
use cat_box::{draw_text, Game, Sprite, SpriteCollection, get_mouse_state, get_keyboard_state};
use sdl2::keyboard::Scancode;
fn main() {
let game = Game::new("catbox demo", 1000, 800);
let mut i = 0u8;
let mut s = Sprite::new("duck.png", 500, 400).unwrap();
let mut s2 = Sprite::new("duck.png", 400, 500).unwrap();
let mut coll = SpriteCollection::new();
for n in 0..10 {
for o in 0..8 {
let x = Sprite::new("duck.png", n * 100, o * 100).unwrap();
coll.push(x);
}
}
game.run(|ctx| {
i = (i + 1) % 255;
ctx.set_background_colour(i as u8, 64, 255);
draw_text(
ctx,
format!("i is {}", i),
"MesloLGS NF Regular.ttf",
72,
(300, 300),
cat_box::TextMode::Shaded {
foreground: (255, 255, 255),
background: (0, 0, 0),
},
)
.unwrap();
let (start_x, start_y) = s.position();
let m = get_mouse_state(ctx);
let x_diff = m.x - start_x;
let y_diff = m.y - start_y;
let angle = (y_diff as f64).atan2(x_diff as f64);
s.set_angle(angle.to_degrees());
for spr in coll.iter() {
let (start_x, start_y) = spr.position();
let m = get_mouse_state(ctx);
let x_diff = m.x - start_x;
let y_diff = m.y - start_y;
let angle = (y_diff as f64).atan2(x_diff as f64);
spr.set_angle(angle.to_degrees());
}
let keys = get_keyboard_state(ctx).keys;
for key in keys {
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),
};
s.translate(offset);
for spr in coll.iter() {
spr.translate(offset);
}
}
s2.draw(ctx).unwrap();
s.draw(ctx).unwrap();
coll.draw(ctx).unwrap();
})
.unwrap();
}
依赖项
~16MB
~345K SLoC