im在rust中编写了一个蛇游戏来学习macroquad引擎,我仍然是一个rust初学者,对语言非常感兴趣
问题是,我似乎无法让蛇朝一个方向前进,比如如果头向上,就不要同时下降
这是蛇头部使用的更新方法
pub fn update(&mut self, dt: f32) {
let v_mv = match (is_key_down(KeyCode::Down), is_key_down(KeyCode::Up)) {
(true, false) => 1f32,
(false, true) => -1f32,
_ => 0f32,
};
let h_mv = match (is_key_down(KeyCode::Right), is_key_down(KeyCode::Left)) {
(true, false) => 1f32,
(false, true) => -1f32,
_ => 0f32,
};
self.player.x += dt * PLAYER_SPEED * h_mv;
self.player.y += dt * PLAYER_SPEED * v_mv;
}
没有什么能阻止蛇同时在x和y中移动。
我将合并两个match
es:
pub fn update(&mut self, dt: f32) {
let (v_mv, h_mv): (f32, f32) = match (
is_key_down(KeyCode::Down),
is_key_down(KeyCode::Up),
is_key_down(KeyCode::Right),
is_key_down(KeyCode::Left),
) {
(true, false, false, false) => (1.0, 0.0),
(false, true, false, false) => (-1.0, 0.0),
(false, false, true, false) => (0.0, 1.0),
(false, false, false, true) => (0.0, -1.0),
_ => (0.0, 0.0),
};
self.player.x += dt * PLAYER_SPEED * h_mv;
self.player.y += dt * PLAYER_SPEED * v_mv;
}