Rust教程:贪吃蛇游戏(第 1/2 部分)

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 updateand this will update all of your dependencies in thecargo.lockfile. If we go to thelockfile, we can search out the two libraries, then copy the number and replace the asterisk back in thetomlfile.
提示:编写依赖项时,可以在引号内使用星号来表示版本号。然后转到终端,输入 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,但有时它确实会发生变化。

  • 24
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老父亲的能量嘎嘣脆

感谢支持,共同成长

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值