3个不稳定版本
0.2.1 | 2022年11月22日 |
---|---|
0.2.0 | 2019年6月30日 |
0.1.0 | 2019年6月16日 |
#220 in 游戏
50KB
1.5K SLoC
tetris_core
简单的俄罗斯方块游戏逻辑(无界面)
警告:该项目是在学习RUST的早期阶段创建的。它可能缺少许多良好的实践。
用法
- 使用任何绘图库。(例如: piston_window)
- 使用任何随机数生成库或方法(例如: rand)
作为实现示例,您可以查看 https://github.com/etoledom/rust_practice/blob/master/07_tetris/src/main.rs
实现 Randomizer
特征
struct Rand;
impl Randomizer for Rand {
fn random_between(&self, lower: i32, higher: i32) -> i32 {
let mut rng = rand::thread_rng();
return rng.gen_range(lower, higher);
}
}
使用随机化实例或您的随机化结构体以及所需的板子大小实例化俄罗斯方块游戏实例
let game_size = Size {
height: 20,
width: 10,
};
let mut game = Game::new(&game_size, Box::new(rand));
更新(&mut self,delta_time: f64)
在每次游戏循环中调用 game.update(delta_time);
绘制(&self) -> Vec<Block>
获取要绘制的板子模型
let game_blocks = game.draw();
一个块是指定块的位置、大小和颜色的结构体,位置和大小是单位性的,您可以给出所需的单位和大小。
struct Block {
pub rect: Rect,
pub color: Color,
}
执行(&mut self,动作:动作)
执行移动和旋转动作
game.perform(Action::Rotate);
is_game_over(&self) -> bool
检查游戏是否结束。
get_score(&self) -> u64
获取当前得分。