fcn从头开始_从头开始:简单游戏系列1-抓鱼

fcn从头开始

Recently, I’ve decided to take a break from coding on IDE’s and go back to the basics of exploring the logic behind programming. Scratch came to my mind. Scratch is such a wonderful yet simple application for those who want to grasp the main ideas of programming.

最近,我决定在IDE的编码上稍作休息,回到探索编程背后逻辑的基础知识。 划痕浮现在我的脑海。 对于那些想掌握编程主要思想的人来说,Scratch是一个奇妙而简单的应用程序。

In this Simple Games Series, you will learn about the process of creating simple games with Scratch from scratch. I hope you find it educational, fun, and engaging. Let’s get started!

在本“ 简单游戏系列”中 ,您将学习从头开始使用Scratch创建简单游戏的过程。 希望您发现它具有教育意义,娱乐性和吸引力。 让我们开始吧!

游戏总结 (Game Summary)

The goal of this game is to catch as much fish as you can within a one-minute time frame. The player controls the movement of the penguin with the arrow keys.

该游戏的目标是在一分钟的时间内捕获尽可能多的鱼。 播放器使用箭头键控制企鹅的移动。

Full game can be played here.

可以在这里玩完整游戏。

组件 (Components)

This game is fairly simple. It can be roughly divided into three parts:

这个游戏很简单。 它可以大致分为三个部分:

  1. Sprites (characters) and backdrops: Speech bubbles, controlling the movements of the penguin and the fish, as well as changing orientations.

    雪碧(人物)和背景:泡泡,控制企鹅和鱼的运动,以及改变方向。
  2. Variables: Keeping the score and the time.

    变量:保留分数和时间。
  3. Game Flow Control: Starting, initializing/resetting, and ending the game, as well as changing backdrops.

    游戏流程控制:开始,初始化/重置和结束游戏,以及更改背景。

1.精灵 (1. Sprites)

In this game, we are only using two sprites and two backdrops. The sprites are the penguin and fish. The backdrops are the arctic scene and the underwater scene. Credits to https://www.freepik.com/ for our fish and underwater backdrop.

在此游戏中,我们仅使用两个精灵和两个背景。 精灵是企鹅和鱼。 背景是北极场景和水下场景。 感谢 我们的鱼类和水下背景 https://www.freepik.com/

企鹅的运动 (Movement of the penguin)

We control the penguin with our arrow keys. Having the penguin selected in the sprites pane, we drag the when __ arrow key pressed Events block to our code. For each arrow, we would like to move left, right, up, and down correspondingly.

我们用箭头键控制企鹅。 在Sprites窗格中选中企鹅后,我们将按下__箭头键的 Events块拖动到了我们的代码上。 对于每个箭头,我们想分别向左,向右,向上和向下移动。

Image for post
Image 1 — Arrow key controls for the penguin
图像1 —企鹅的箭头键控件

Because we want the penguin to be facing left/right as it moves left/right, we create two different costumes (one facing left and one facing right), switching costumes as the player controls the penguin with the left and right arrow keys (the switch costume to ___ block does the trick).

因为我们希望企鹅在向左/向右移动时朝左/向右,所以我们创建了两种不同的服装(一种朝向左,另一种朝向右),在玩家使用左右箭头键控制企鹅时切换服装。 将服装换成___块就可以了。

鱼的运动 (Movement of the fish)

In this game, the fish bounces off the edges and moves in random directions in the water. The idea behind this is that the fish picks a random direction to travel. As it hits the edge, it bounces off and picks a random direction to move again.

在这个游戏中,鱼从边缘弹起并在水中向随机方向移动。 这背后的想法是鱼会随机选择一个方向行驶。 当它撞到边缘时,会弹起并随机选择一个方向再次移动。

Image for post
Image 2 — Fish movement logic
图片2-鱼运动逻辑

抓鱼 (Catching the fish)

When the penguin touches the fish, the fish should disappear and the score should increase by 1.

当企鹅碰到鱼时,鱼应消失并且分数应增加1。

We first create a variable, Score. Then we set a condition on the penguin: if it touches the fish, then we add 1 to the score and broadcasts a signal, caught.

我们首先创建一个变量Score 。 然后,我们在企鹅上设置条件:如果企鹅碰到了鱼,则将分数加1并广播信号catch

Image for post
Image 3 — Logic for the penguin to catch the fish
图3 —企鹅捉鱼的逻辑

For the fish, we ask it to hide itself when it is touched by the penguin, then go to a random position. This is to create the illusion that it is eaten by the penguin, and a new fish has appeared.

对于鱼,我们要求它在被企鹅触摸时隐藏自身,然后移至随机位置。 这是给人一种幻想,它被企鹅吃掉了,并且出现了一条新鱼。

Image for post
Image 4 — When the fish is caught
图片4 —抓鱼时

After hiding for 1.5 seconds, it can then be displayed again using the movement logic in Image 2.

隐藏1.5秒后,可以使用图像2中的移动逻辑再次显示它。

2.变量 (2. Variables)

We will discuss mostly about the timer variable as we have already talked about the Score variable earlier.

由于我们之前已经讨论了Score变量,因此我们将主要讨论有关timer变量的内容。

Even though Scratch has a built-in timer, we are going to create our own timer because it is easier to control. We will be adding 1 to the timer variable for every 1 second that has passed until it reaches 60 (one-minute time limit).

即使Scratch具有内置计时器,我们仍将创建自己的计时器,因为它更易于控制。 我们将在每经过1秒的计时器变量中加1,直到达到60(一分钟的时间限制)为止。

Image for post
Image 5 — Keeping track of the timer
图片5 —跟踪计时器

3.游戏流程控制 (3. Game Flow Control)

For any game set up, we need to be able to “initialize/reset the game”, “start the game”, and “end the game”.

对于任何设置的游戏,我们都需要能够“初始化/重置游戏”,“开始游戏”和“结束游戏”。

For more information on the types of Scratch Blocks, this page is a good reference.

有关暂存块类型的更多信息, 此页面是不错的参考。

初始化精灵的位置,背景和变量 (Initializing the sprite position, backdrop, and variables)

“Initializing/Resetting the game” simply means to set the game to its initial condition, so that the player starts with the same condition every time. In this case, we would like the game to start with setting both the timer and score to 0. We would also want to ensure that the timer is stopped before the player starts playing the game.

“初始化/重置游戏”只是意味着将游戏设置为初始状态,以便玩家每次都以相同的条件开始。 在这种情况下,我们希望游戏从将计时器和得分都设置为0开始。我们还希望确保在玩家开始玩游戏之前停止计时器。

Image for post
Image 6 — Triggering initialization
图像6-触发初始化

The event that triggers the initialization is when the green flag is clicked. We make a signal, init, to tell the program that we need to set our game to the initial state. This can be done as shown in the image above.

触发初始化的事件 是单击绿色标志时 我们发出一个信号init 告诉程序我们需要将游戏设置为初始状态。 如上图所示,这可以完成。

The image below is a visualization of how our game starts. We are using the penguin sprite and the arctic backdrop provided by Scratch.

下图是我们的游戏开始方式的可视化视图。 我们正在使用Scratch提供的企鹅精灵和北极背景。

Starting screen
Image 7 — Starting Screen (graphic elements from Scratch)
图7 —启动屏幕(从头开始的图形元素)

Initialize the penguin

初始化企鹅

For the penguin, when it receives the init signal, it will switch its costume, go to the initial position (x:30 y:20), and stand upright (in the direction of 90), just as shown in Image 7. Image 8 is what it looks like in Scratch code.

对于企鹅,当它收到初始化信号时,它将转换其服装,转到初始位置(x:30 y:20),并直立(沿90方向),如图7所示。 8是Scratch代码中的样子。

Image for post
Image 8 — Penguin init code
图8 —企鹅初始化代码

Initialize the fish

初始化鱼

We want to hide the fish when the game is not yet started. To achieve this, we simply construct the logic like below.

我们想在游戏尚未开始时将鱼藏起来。 为了实现这一点,我们只需构造如下的逻辑。

Image for post
Image 9 — Hiding the fish
图片9 —隐藏鱼

Initialize the backdrop

初始化背景

The backdrop is initialized to the arctic scene.

背景被初始化为北极场景。

Image for post
Image 10 — Initialize backdrop
图片10-初始化背景

Initialize the variables

初始化变量

The variables are all set to 0 before the player starts playing.

在播放器开始播放之前,所有变量都设置为0。

Image for post
Image 11 — initialize variables
图11 —初始化变量

开始游戏 (Starting the game)

The game starts when the players “press the space key”. When the game receives the space key signal, the backdrop will switch from the arctic scene to the underwater scene, and the penguin will switch costumes. Additionally, we will also show the timer and the Score variables on the screen so the players can keep track of their progress.

当玩家“按下空格键”时,游戏开始。 当游戏收到空格键信号时,背景将从北极场景切换到水下场景,而企鹅将切换服装。 此外,我们还将在屏幕上显示计时器Score变量,以便玩家跟踪自己的进度。

Image for post
Image 12— Starting the game
图12 —开始游戏

Then, the penguin can start catching the fish! All we need to do is grab the block that we made earlier in Catching the fish (Image 3) and put it in a forever block.

然后,企鹅就可以开始钓鱼了! 我们需要做的就是抓住我们先前在“捉鱼” (图3)中制作的方块,并将其放置在永久的方块中。

Image for post
Image 13— Connecting Image 3 and Image 12
图13 —连接图3和图12

结束游戏 (Ending the game)

Now it works perfectly — the penguin can catch the fish and we can keep track of the Score. But how do we end the game? (Hint: what is the timer for?) There is only one situation that this game would end, that is, when the timer hits 1 minute.

现在它可以完美工作了-企鹅可以钓到鱼,并且我们可以跟踪得分 。 但是,我们如何结束游戏呢? ( 提示:计时器的作用是什么?)只有一种情况,该游戏将结束,即计时器达到1分钟时。

To set the “end game condition”, we need an if…then/else block nested in the forever block. When the timer reaches 60, we would set the timer to 0, and the penguin would tell the player his/her score. Finally, we will stop the game with the stop all block.

要设置“最终游戏条件”,我们需要一个if ... then / else块嵌套在永远块中。 当计时器达到60时,我们将计时器设置为0,企鹅会告诉玩家他/她的得分。 最后,我们将使用stop all块停止游戏。

Image for post
forever 永远的 block 区块中添加“最终游戏条件”

And there you have it, a simple game made with Scratch!

有了Scratch制作的简单游戏!

您可以建立的东西… (Things you can build upon…)

  1. Make the fish move smarter. For example, moving away from the penguin instead of choosing a random direction to move.

    使鱼变得更聪明。 例如,远离企鹅而不是选择随机方向移动。
  2. Add game levels.

    添加游戏级别。
  3. Set a threshold score that the player should achieve in one minute.

    设置玩家在一分钟内应达到的阈值分数。
  4. ANYTHING ELSE!!

    还要别的吗!!

See my next post on Scratch: Simple Game Series 2 — Guess the Instrument!

请参阅我的下一篇关于Scratch:简单游戏系列2-猜猜乐器的信息!

翻译自: https://medium.com/swlh/scratch-simple-game-series-1-catch-the-fish-69265bcae84c

fcn从头开始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值