我刚刚使用这个工具在 Rust 中为游戏分配了一个小的 POC 级别。
const LEVEL_0: &'static [&'static [i32]] = &[
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 9, 0, 0, 0, 2, 0, 0, 3, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];
并使用以下函数加载
pub fn load_stage(&mut self, ctx: &mut Context, level: usize) {
let levels = vec![LEVEL_0];
for (i, row) in levels[level].iter().enumerate() {
for (j, col) in row.iter().enumerate() {
if *col == 1 {
self.board.add_block(
ctx,
Vector2::<f32>::new(j as f32, i as f32),
self.cell_size,
);
}
注意:上面的代码略有不完整,仅为解答提供一些背景信息。
对于字符串,您可以使用类似以下的内容:
const ENEMIES: &'static [&'static str] = &["monster", "ghost"];