1个不稳定版本
| 0.1.0 |  | 
|---|---|
| 0.0.1 | 2024年6月18日 | 
#109 in #2d-game
61 个月下载量
16KB
245 代码行
figs
一个简单的使用Rust制作2D游戏的框架。
功能
- 通过 minifb实现图形和基本输入管理
- 实体管理,包括动画和碰撞
- 通过 tar和regex从文件系统或tar包加载资源
- 通过 png支持图像
- 通过 cpal和hound播放音频
使用方法
将以下内容添加到您的 Cargo.toml
figs = "0.0.1"
快速开始
use figs::prelude::*;
fn main() -> FigResult<()> {
    // Initialize the canvas and input
    let mut canvas = Canvas::new("simple example", (320, 320), 60)?;
    let mut input = InputManager::new();
    // Load assets from assets.tgz
    let mut assets = AssetLoader::from_tar("examples/assets.tgz");
    // You can also load assets from the assets directory
    // let mut assets = AssetLoader::from_dir("examples/assets");
    // Create some entities, using assets from the tarball
    let mut blob = Entity::new((100.0, 100.0), assets.load_png_dir("blob")?, (8, 8), 20);
    // We want to control blob with WASD or arrow keys
    input.wasd();
    input.arrows();
    // We would also like to exit when ESC is pressed
    input.add(Key::Escape);
    // Main event loop (do something more interesting here)
    while canvas.is_open() {
        // Handle keypress events
        for key in &input.keys {
            if canvas.key_down(*key) {
                match key {
                    Key::W | Key::Up => blob.mov(0.0, -1.0),
                    Key::A | Key::Left => blob.mov(-1.0, 0.0),
                    Key::S | Key::Down => blob.mov(0.0, 1.0),
                    Key::D | Key::Right => blob.mov(1.0, 0.0),
                    Key::Escape => return Ok(()),
                    _ => (),
                }
            }
        }
        // Update entities (advance animation, etc)
        blob.update();
        // Update the canvas with the entities
        canvas.clear();
        canvas.draw(&blob);
        canvas.update()?;
    }
    Ok(())
}
有关更详细的示例,请参阅 examples/
贡献
欢迎提交问题和拉取请求。
依赖关系
~6–36MB
~573K SLoC