It's excited to show you how to code the “Snake Game” in Rust! I feel that Snake is the perfect program to showcase your skills. This Snake game has multiple different components.
本文将向您展示如何用 Rust 编写“贪吃蛇游戏”!我觉得贪吃蛇游戏是展示你技能的完美程序,它由多个不同的组件组成。
Basic information about the Components of the Game
有关游戏组件的基本信息
🐍 The Snake can move in all directions. If our Snake eats an apple, it will grow by one “point”. When we push an arrow button, the head will move up or down or left or right. The Snake cannot touch itself. For example, if it’s going to the left and I push the right key, it won’t be able to go backward, this would cause the game to enter a fail state because the Snake cannot actually touch itself. If the Snake touches itself, it dies and we have to restart the game. Also, if the Snake hits the wall, it dies. Those are the two main failure states for the game.
🐍 蛇可以向各个方向移动。如果我们的蛇吃了一个苹果,它就会增长一个“点”。当我们按下箭头按钮时,头部会向上、向下、向左或向右移动。蛇无法触及自己。例如,如果它向左移动,而我按了右键,它就无法后退,这会导致游戏进入失败状态,因为蛇实际上无法触及自己。如果蛇碰到了自己,它就会死,我们必须重新开始游戏。另外,如果蛇撞到墙上,它就会死。这是游戏的两个主要失败状态。
🍎 The second component is the Apple. In most Snake games, the Apple appears at random places. With this game, you’ll notice that the Apple appears at the same place every time we start the game. The same with the Snake. The Snake will appear at a fixed place every time we restart it.
🍎 第二个组件是Apple。在大多数贪吃蛇游戏中,苹果出现在随机位置。在这个游戏中,你会注意到每次我们启动游戏时苹果都会出现在同一个地方。与蛇相同。每次我们重新启动时,蛇都会出现在固定的位置。
🧱 The third component is the walls. The walls will be represented as a rectangle that goes around the entire border of our game board. The Snake cannot pass through the walls.
🧱 第三个组成部分是墙壁。墙壁将表示为围绕游戏板整个边界的矩形。蛇不能穿过墙壁。
🕹️ Finally, we have the game board itself. This will be a 2D plane that the Snake moves along and that the Apple spawns in.
🕹️最后,我们有了游戏板本身。这将是一个 2D 平面,蛇会沿着该平面移动,苹果会在其中生成。
You’ll need to create these 4 files:
您需要创建这 4 个文件:
- main.rs
- draw.rs
- snake.rs
- game.rs
Dependencies 依赖关系
Cargo.toml
In the Cargo file, we want to add two dependencies.
在 Cargo 文件中,我们要添加两个依赖项。
rand = "0.8.5"
piston_window = "0.131.0"
The first dependency is rand
(for random), this is a library that will allow us to deal with the random numbers for our Apple.
第一个依赖项是 rand
(随机),这是一个库,允许我们处理 Apple 的随机数。
The second dependency is piston_window
. This will allow us to render our elements with a UI as well as deal with some of the game logic.
第二个依赖项是 piston_window
。这将使我们能够使用 UI 渲染元素并处理一些游戏逻辑。
Tip: When you write the dependencies you can use inside the quotes an asterisk for the version numbers. Then go to the terminal, typecargo update
and this will update all of your dependencies in thecargo.lock
file. If we go to thelock
file, we can search out the two libraries, then copy the number and replace the asterisk back in thetoml
file.
提示:编写依赖项时,可以在引号内使用星号来表示版本号。然后转到终端,输入 cargo update
,这将更新 cargo.lock
文件中的所有依赖项。如果我们转到 lock
文件,我们可以搜索出这两个库,然后复制数字并将星号替换回 toml
文件中。
rand = "*"
piston_window = "*"
The reason it’s important to use static versions is just in case the library actually changes. If the syntax changes, then the game will not work properly anymore because we will be behind the API. Normally, libraries try to keep a consistent API, but sometimes it does change.
使用静态版本很重要的原因是为了防止库实际发生变化。如果语法发生变化,那么游戏将无法正常运行,因为我们将落后于 API。通常,库会尝试保持一致的 API,但有时它确实会发生变化。