#pixel-art #game-engine #2d-game #terminal #sdl2 #graphical #mode

rust_pixel

2D像素风格游戏引擎及快速原型工具,支持终端、sdl2和Web

12个版本

0.4.6 2024年8月24日
0.4.5 2024年8月15日
0.4.2 2024年7月30日
0.3.3 2024年7月6日
0.2.0 2024年6月20日

#272 in 游戏开发

Download history 177/week @ 2024-06-17 296/week @ 2024-06-24 291/week @ 2024-07-01 40/week @ 2024-07-08 111/week @ 2024-07-15 136/week @ 2024-07-22 301/week @ 2024-07-29 2/week @ 2024-08-05 240/week @ 2024-08-12

699次每月下载
用于 2 工具包

Apache-2.0

270KB
7K SLoC

logo

License Latest Version Downloads API Documentation MSRV

变更日志   |   编码   |   常见问题解答   |   待办事项

RustPixel是一个 2D游戏引擎快速原型工具,支持文本和图形渲染模式。
适用于创建 2D像素风格游戏 和开发 终端应用程序
它可以编译为FFI用于前端和后端,也可以编译为WASM用于Web项目。

  • 文本模式:使用crossterm构建,在终端运行,使用ASCII和Unicode表情符号进行绘图。
  • 图形模式(SDL2):使用SDL2构建,使用PETSCII和自定义图形符号进行渲染。
  • 图形模式(Web):类似于SDL2模式,但核心逻辑编译为WASM,使用WebGL和JavaScript进行渲染(请参考rust-pixel/web-template/pixel.js)。

RustPixel实现了游戏循环、Model/Render通用模式和一个消息机制来支持小型游戏的构建。它还包括一些常见的游戏算法和工具模块。此外,RustPixel还附带了一些小型游戏,如俄罗斯方块、塔楼和扑克牌,可以作为创建您自己的游戏和终端应用程序的参考。它还包括将核心游戏算法包装到FFI和WASM中的示例。

安装指南

安装过程的主要步骤如下

每个操作系统的详细步骤:   MacOS   |   Linux   |   Windows

接下来,您应该克隆 RustPixel 并部署 cargo-pixel 工具

git clone https://github.com/zipxing/rust_pixel
cd rust_pixel
cargo install --path tools/cargo-pixel --root ~/.cargo

使用说明

cd rust_pixel
cargo pixel run snake term            #Run the snake game in terminal mode
cargo pixel r snake t                 #Run the snake game in terminal mode - shorthand
cargo pixel r tetris s                #Run the Tetris game in SDL window mode
cargo pixel r tower w                 #Run tower in web,visit https://127.0.0.1:8080/ in your browser
cargo pixel r tower w --webport 8081  #Change web server port
cargo pixel r tower w -r              #Run with release mode

您也可以使用 cargo pixel 创建自己的游戏或应用

cargo pixel c games mygame           #Create mygame in ./games using games/template as a template
cargo pixel c apps myapp             #Create myapp in ./apps using games/template as a template

在某个目录中创建一个独立的应用

cargo pixel c .. myapp --standalone  #Create a standalone crate in ../myapp 
cd ../myapp 
cargo pixel r myapp t
cargo pixel r myapp s

RustPixel 还包括一些工具

  1. palette:一个终端-ui工具,用于生成、分析、转换和操作颜色。
cargo pixel r palette t -r

palette

  1. tedit:用于编辑字符艺术资产,示例
#term mode
cargo pixel r tedit term assets/screen-shot/tedit.txt

#graphical mode
cargo pixel r tedit sdl assets/screen-shot/tedit.pix 

tedit_t tedit_s

  1. tpetii:用于将常规图像转换为 PETSCII 字符艺术,示例
cargo pixel r tpetii t assets/fire.png 40 40 -r > assets/fire.pix
# -r : build tpetii in release mode for high performance
  1. 脚本,用于自动将 gif 图像转换为 PETSCII 动画(ssf)
cargo pixel cg assets/sdq/fire.gif assets/sdq/fire.ssf 40 25 

演示游戏

  1. snake:一个带有酷炫 PETSCII 动画的蛇游戏
#graphical mode
cargo pixel r snake s -r

graphical mode

#term mode
cargo pixel r snake t -r
#web mode
cargo pixel r snake w -r
#and visit http://localhost:8080/ in your browser
  1. tetris:一个您可以与 AI 玩的俄罗斯方块游戏
#term mode
cargo pixel r tetris t -r

term mode

#graphical mode
cargo pixel r tetris s -r

graphical mode

#web mode
cargo pixel r tetris w -r
#and visit http://localhost:8080/ in your browser

web mode

  1. poker:包括德州扑克和金花的核心算法
cargo pixel r poker t -r
cargo pixel r gin_rummy t -r

gin_rummy red_black

poker/ffi 目录演示如何将 Rust 算法包装成 CFFI 以供其他语言使用,展示了 C++ 和 Python 调用 poker_ffi

cd games/poker/ffi
make run

poker/wasm 目录演示如何将 Rust 算法包装成 wasm 以供 JS 调用

cd games/poker/wasm
make run
  1. tower:一个塔防游戏原型,展示了使用 objpool 和 pixel_sprite 进行像素级精灵移动的使用方法
#graphical mode
cargo pixel r tower s -r

#web mode
cargo pixel r tower w -r
#and visit http://localhost:8080/ in your browser

tower

等等...

依赖项

~14–48MB
~768K SLoC