← 返回资讯屏轩教育 · 行业资讯

Year 7 WJEC Computer Science Case Study: From Algorithm to Scratch Game | Year 7 WJEC 计算机:从算法到Scratch游戏案例分析实战演练

2026-07-16 20:22 · 本文部分内容由AI辅助创作

📚 Year 7 WJEC Computer Science Case Study: From Algorithm to Scratch Game | 从算法到Scratch游戏案例分析实战演练

This hands-on case study will guide you through the entire process of designing and coding a simple 'Catch the Star' game using Scratch. You will learn how to apply computational thinking skills — decomposition, pattern recognition, abstraction, and algorithm design — to solve a real problem. By the end, you will have a working game and a deeper understanding of how programmers plan before they code.

本实战案例将带你完整经历使用Scratch设计与编写一个“接星星”小游戏的全过程。你将学习如何运用计算思维——分解、模式识别、抽象和算法设计——来解决真实的问题。完成本案例后,你将拥有一个可以运行的游戏,也会更深刻地理解程序员在动手编码前是如何进行规划的。


1. Understanding the Problem | 理解问题

We need to create a game where the player controls a character (e.g., a cat) using the mouse. Coloured stars fall from the top of the screen at random positions. The cat must catch them. Each star caught increases the score by 1. If a star reaches the bottom without being caught, the player loses one life. The game starts with 3 lives and lasts 60 seconds. The game ends when all lives are lost or the timer reaches zero.

我们需要制作一个游戏:玩家用鼠标控制一个角色(如小猫);彩色的星星从屏幕顶部随机位置下落,小猫需要接住它们。每接住一颗星星,得分增加1分;若星星未被接住而触底,则失去一条生命。游戏初始拥有3条生命,限时60秒。生命耗尽或计时归零时游戏结束。


2. Decomposition | 分解问题

Breaking the game into smaller, manageable parts makes it easier to build. The main components are:

将游戏拆分成更小、更容易管理的部分,会让搭建过程变得简单。主要组件有:


3. Pattern Recognition | 模式识别

Looking at the game, we can spot repeating patterns. Every star behaves in the same way: it appears at a random x-position near the top, glides downward at a steady speed, and disappears when it hits the bottom or touches the cat. The cat’s movement is another pattern — it constantly updates its position to match the mouse pointer. Recognising these patterns helps us write code once and reuse it for all stars using clones.

观察游戏,我们可以发现重复的模式。每颗星星的行为都一样:出现在顶部附近随机的x坐标处,以固定速度向下滑动,碰到底部或碰到小猫后消失。小猫的移动也是一个模式——它不断更新自己的位置以匹配鼠标指针。识别这些模式有助于我们编写一次代码,并通过克隆体在所有的星星上重复使用。


4. Abstraction | 抽象

Abstraction means focusing on the important details and ignoring the rest. For our game, we don’t care about the exact colour of each star or the expression on the cat’s face. The key information is the star’s x and y coordinates, its falling speed, and whether it is touching the player. The score and lives are just numbers we display. This simplified model lets us concentrate on the logic without getting distracted by visual details.

抽象意味着聚焦于重要细节而忽略其余部分。对我们的游戏来说,我们并不在意每颗星星具体的颜色或小猫脸上的表情。关键信息是星星的x和y坐标、它的下落速度,以及它是否碰到玩家。得分和生命只是我们显示的数字。这个简化的模型让我们可以专注于逻辑,而不会被视觉细节干扰。


5. Algorithm Design with Pseudocode | 用伪代码设计算法

Before touching Scratch, we write down the steps in plain English (pseudocode). This acts as a blueprint for our program.

在接触Scratch之前,我们先用通俗的英语写下步骤(伪代码)。这将作为我们程序的蓝图。

Main program:

when green flag clicked
set score ← 0
set lives ← 3
set timer ← 60
forever
go to mouse pointer
create a clone of star every 1 second
change timer by -1 every second
if lives = 0 or timer = 0 then
stop all

Star clone behaviour:

when I start as a clone
go to x: random between -220 and 220 y: 170
repeat until y < -170
change y by -5
if touching cat then
change score by 1
delete this clone
end
end
if y < -170 then
change lives by -1
delete this clone
end

主程序伪代码:

点击绿旗
得分设为 0
生命设为 3
计时器设为 60
无限循环
移到鼠标指针
每隔1秒创建一个星星的克隆体
每秒将计时器减少1
如果生命 = 0 或 计时器 = 0 那么
停止全部

星星克隆体行为:

当作为克隆体启动时
移到 x: -220 到 220 之间的随机数 y: 170
重复执行直到 y < -170
将 y 增加 -5
如果碰到小猫那么
得分增加 1
删除本克隆体
结束
结束
如果 y < -170 那么
生命增加 -1
删除本克隆体
结束

The pseudocode uses left arrow (←) to show assignment, which is common in computing. It helps us think through the sequence before we build the actual blocks.

伪代码使用左箭头(←)表示赋值,这在计算机领域很常见。它帮助我们在搭建实际积木前理清流程。


6. Flowchart Representation | 流程图表示

A flowchart turns the pseudocode into a visual diagram. Key symbols used:

流程图把伪代码转变成可视化图表。使用的关键符号:

SymbolMeaning
OvalStart/End
RectangleProcess (e.g. set score to 0)
DiamondDecision (e.g. lives = 0?)
ArrowFlow of control
符号含义
椭圆开始/结束
矩形过程(如将得分设为0)
菱形判断(如生命 = 0?)
箭头控制流

The flowchart for the star clone would start with an oval 'Start as clone', move to a rectangle 'Go to random x, y=170', then a decision diamond 'Touching cat?'. If yes, arrow to 'Change score by 1' then 'Delete clone'. If no, arrow to decision 'y < -170?'. If yes, 'Change lives by -1' then 'Delete clone'; if no, loop back to 'Change y by -5'. Drawing this helps spot missing logic.

星星克隆体的流程图会从一个椭圆“作为克隆体启动”开始,进入矩形“移到随机 x, y=170”,然后是一个菱形判断“碰到小猫?”。如果是,箭头指向“得分增加1”然后“删除克隆体”。如果否,箭头指向判断“y < -170?”。如果是,箭头指向“生命增加-1”然后“删除克隆体”;如果否,循环回到“将y增加-5”。绘制流程图有助于发现遗漏的逻辑。


广告 / Sponsored

7. Setting Up the Stage in Scratch | 在Scratch中设置舞台

Open Scratch and create a new project. Click the Stage icon and go to the Backdrops tab. Choose a backdrop that looks like space, e.g. 'Stars' from the library. This gives the game a clear theme. There is no coding needed on the stage itself, but we will create variables here that all sprites can use.

打开Scratch并新建项目。点击舞台图标,进入“背景”选项卡。从素材库中选择一个看起来像太空的背景,例如“Stars”。这赋予了游戏明确的主题。舞台本身不需要编写代码,但我们会在此创建所有角色都能使用的变量。

In the Variables category, create three variables: 'score', 'lives', and 'timer'. Make sure they are set to 'For all sprites' so every sprite can access and change them.

在“变量”分类中,创建三个变量:“score”、“lives”和“timer”。务必把它们设置为“适用于所有角色”,这样每个角色都能读取和修改它们。


8. Coding the Player Sprite | 编写玩家角色代码

The cat sprite is already in the project by default. We want it to follow the mouse pointer throughout the game. The code for the cat is very simple:

小猫角色默认已经在项目中。我们希望它在整个游戏过程中跟随鼠标指针。小猫的代码非常简单:

when green flag clicked
forever
go to mouse-pointer

点击绿旗
无限循环
移到鼠标指针

You can add a tiny offset if you want the cat to sit slightly above the pointer, but the basic version just snaps to the exact mouse position. Test it by clicking the green flag and wiggling your mouse — the cat should follow.

如果你想让小猫的位置略高于指针,可以添加一个小小的偏移量,但基本版本只需直接定位到鼠标的准确位置。点击绿旗并晃动鼠标进行测试——小猫应该会跟随。


9. Coding the Falling Star | 编写下落的星星代码

The star sprite needs to be chosen or drawn. Pick a star from the sprite library. The star itself will be invisible but clones will do all the work. Here is the core logic for the star sprite:

星星角色需要从角色库中选择或自己绘制。星星本体设为隐藏,由克隆体完成所有工作。以下是星星角色的核心逻辑:

when green flag clicked
hide
forever
create clone of myself
wait 1 seconds

点击绿旗
隐藏
无限循环
创建自身的克隆体
等待1秒

For the clone behaviour:

克隆体的行为:

when I start as a clone
show
go to x: pick random -220 to 220 y: 170
repeat until y < -170
change y by -5
if touching cat? then
change score by 1
delete this clone
end
end
if y < -170 then
change lives by -1
delete this clone
end

当作为克隆体启动时
显示
移到 x: 随机取 -220 到 220 y: 170
重复执行直到 y < -170
将 y 增加 -5
如果碰到小猫?那么
得分增加 1
删除本克隆体
结束
结束
如果 y < -170 那么
生命增加 -1
删除本克隆体
结束

Be careful to place 'touching cat?' inside the repeat loop so that the check happens every time the star moves. The second check after the loop catches the case where the star reaches the bottom without being caught.

注意要把“碰到小猫?”放在重复循环内部,这样每次星星移动时都会检查。循环后的第二个判断用于捕捉星星触底而未被接住的情况。


10. Adding Score, Lives and Timer | 添加分数、生命和计时器

We already created the variables. Now we need to make the timer count down each second and check the game-over condition. The cleanest way is to handle timer and game-over in the stage, but you can also put it in the cat or star sprite. Let’s use the stage backdrop for global control:

我们已经创建了变量。现在需要让计时器每秒倒数,并检查游戏结束条件。最简洁的方式是在舞台中处理计时器和游戏结束判断,但也可以放在小猫或星星角色中。我们使用舞台背景进行全局控制:

when green flag clicked
set score to 0
set lives to 3
set timer to 60
repeat until timer = 0 or lives = 0
wait 1 seconds
change timer by -1
end
stop all

点击绿旗
将 score 设为 0
将 lives 设为 3
将 timer 设为 60
重复执行直到 timer = 0 或 lives = 0
等待1秒
将 timer 增加 -1
结束
停止全部

Setting the initial values in the stage ensures they are ready before any stars or the cat start moving. The repeat-until loop exits as soon as one of the conditions is false, stopping the game as required.

在舞台中设定初始值可以确保在任何星星或小猫开始移动之前,变量就已准备好。重复直到循环会在任一条件满足时立即退出,按要求结束游戏。


11. Testing and Debugging | 测试与调试

Run your game and observe carefully. Common bugs include: stars not being deleted after hitting the cat, the timer counting down too fast, or the cat not catching stars because the collision area is too small. Debug each issue systematically.

运行游戏并仔细观察。常见的错误包括:星星碰到小猫后没被删除,计时器走得太快,或者因为碰撞区域太小导致小猫接不到星星。请系统地排查每个问题。

If the timer counts down twice per second, check that you do not have a second 'change timer by -1' loop somewhere else. If the star vanishes instantly, perhaps you accidentally placed 'delete this clone' before the movement. Use 'wait 0 seconds' blocks to slow things down temporarily and watch what happens.

如果计时器每秒倒数两次,检查是否在其他地方还有第二个“将timer增加-1”的循环。如果星星瞬间消失,可能你不小心把“删除本克隆体”放在了移动代码之前。使用“等待0秒”积木可以临时放慢节奏,观察究竟发生了什么。


12. Evaluation and Reflection | 评估与反思

After the game works, ask yourself: does it meet the original requirements? The score increases when a star is caught, lives decrease when a star is missed, and the game stops after 60 seconds or when lives reach 0. If yes, the project is a success.

游戏运行后,问问自己:它满足最初的需求了吗?得分在接住星星时增加,生命在漏接时减少,游戏在60秒后或生命归零时停止。如果满足,那这个项目就是成功的。

Think about what could be improved. You could add a 'You Win' screen when time runs out with a high score, make stars fall faster as the score climbs, or add sound effects. These refinements all follow the same computational thinking steps: decompose the new feature, spot patterns, abstract details, and design an algorithm.

想想还有什么可以改进。你可以添加一个“你赢了”的画面并显示最高分,让星星随着分数升高而下落得更快,或者添加音效。这些优化都遵循同样的计算思维步骤:分解新特性、识别模式、抽象细节、设计算法。

Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
Linux高级程序设计(高等院校计算机技术十二五规划教材)/刘加海/季江民/浙江大学出版社
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥55.6

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao