In this article, we’ll finish the snake.rs
file, and also continue with the rest of the files (main.rs, draw.rs, game.rs).
欢迎来到本教程的第二部分,在本文中,我们将完成 snake.rs
文件,并继续处理其余文件(main.rs、draw.rs、game.rs)。
查看第一部分:
snake.rs
As a reminder from the [1st part], we had finished working with the functions draw
, head_position
and move_forward
in the snake.rs file.
作为[第 1 部分] 的提醒,我们已经完成了 Snake.rs 文件中的函数 draw
、 head_position
和 move_forward
的使用。
Functions: head_direction
, next_head
, restore_tail
and overlap_tail
函数: head_direction
、 next_head
、 restore_tail
和 overlap_tail
Time to create a new function that will allow us to take in our snake or a reference to our snake and then get a direction.
是时候创建一个新函数了,它允许我们接收蛇或对蛇的引用,然后获得方向。
pub fn head_direction(&self) -> Direction {
self.direction
}
Alright, so we want another method, I’m going to call it next_head
. This will take in a reference to &self
and an Option<Direction>
, and then it will output a tuple of i32
. So we'll say let (head_x, head_y): (i32, i 32)
and then we'll get the head_position
using our head_position
method.
好吧,我们想要另一种方法,我将其命名为 next_head
。这将接受对 &self
和 Option<Direction>
的引用,然后输出 i32
的元组。因此,我们会说 let (head_x, head_y): (i32, i 32)
,然后我们将使用 head_position
方法获取 head_position
。
pub fn next_head(&self, dir: Option<Direction>) -> (i32, i32) {
let (head_x, head_y): (i32, i32) = self.head_position();
let mut moving_dir = self.direction;
match dir {
Some(d) => moving_dir = d,
None => {}
}
match moving_dir {
Direction::Up => (head_x, head_y - 1),
Direction::Down => (head_x, head_y + 1),
Direction::Left => (head_x - 1, head_y),
Direction::Right => (head_x + 1, head_y),
}
}
We’ll get the snake direction with the mutable moving direction let mut moving_dir = self.direction;
and then we're going to match
on the direction that we're passing into the method.
我们将通过可变的移动方向 let mut moving_dir = self.direction;
获得蛇的方向,然后我们将在传递给方法的方向上 match
。
Then we’re going to match
again on this new moving_dir
, this will help with accuracy.
然后我们将在这个新的 moving_dir
上再次 match
,这将有助于提高准确性。
Finally, we have two more methods we want to create. Create another public function called restore_tail
. It will take in a reference to our mutable Snake. We'll also create a block which will be based on our tail. Then we're going to push_back
our cloned tail into the back of our body.
最后,我们还有两个要创建的方法。创建另一个名为 restore_tail
的公共函数。它将引用我们可变的 Snake。我们还将创建一个基于我们的尾巴的块。然后我们将 push_back
我们克隆的尾巴放入我们的身体后部。
Basically, as you know the tail doesn’t get rendered unless we eat an apple. So if we eat an apple this method will be run and the tail will be pushed into our linked list body. This is how our snake is growing in size.
基本上,如你所知,除非我们吃苹果,否则尾巴不会被渲染。因此,如果我们吃一个苹果,该方法将运行,并且尾部将被推入我们的链表主体中。我们的蛇就是这样长大的。
pub fn restore_tail(&mut self) {
let blk = self.tail.clone().unwrap();
self.body.push_back(blk);
}
Last but not least, we have our last method for this file. Let’s call this method overlap_tail
. It will take in our Snake an x
and a y
, then we will pass back a boolean
.Let's also create a mutable value and set it to equal to zero. We'll iterate through our snake body and we'll check to see if x
equals block.x
and if y
equals block.x
. So in other words:
最后但并非最不重要的一点是,我们有这个文件的最后一个方法。我们将此方法称为 overlap_tail
。它将接受我们的 Snake 一个 x
和一个 y
,然后我们将传回一个 boolean
。我们还创建一个可变值并将其设置为等于零。我们将迭代我们的蛇体,并检查 x
是否等于 block.x
以及 y
是否等于 block.x
。换句话说:
- If our snake is overlapping with any other part of its actual body then we’ll
return true
.
如果我们的蛇与其实际身体的任何其他部分重叠,那么我们将return true
。 - Otherwise, we’re going to increment
ch
.
否则,我们将增加ch
。
Then we’re going to check if ch
equals =&