KS3计算机 Scratch编程 积木 循环 变量 项目
很多学生在 KS3 阶段第一次接触编程,用的就是 Scratch。它看起来像游戏,实际上是一门完整的编程语言:变量、循环、条件、事件、消息传递,一个都不少。区别只在于,代码不是打出来的,而是拼出来的。这篇文章按学习的真实顺序,把 Scratch 的核心积木讲清楚,并在最后带你做一个完整的接苹果小游戏,把前面所有知识点串起来。写完这个项目,你就具备了进入 Python 阶段的基础。
Most students meet programming for the first time in KS3 through Scratch. It looks like a game, but it is a complete programming language: variables, loops, conditions, events and message passing are all there. The only difference is that code is snapped together instead of typed. This article follows the order in which the ideas actually need to be learned, and finishes with a small but complete game so that every concept connects to something real.
1. 舞台、角色与坐标系 / Stage, Sprites and Coordinates
一个 Scratch 项目只有三个区域:舞台(Stage)、角色列表(Sprite Pane)和脚本区(Scripts)。舞台是程序运行的地方,宽 480 像素、高 360 像素;角色是舞台上会动的对象;脚本区是你给每个角色写指令的地方。舞台的坐标原点在正中央,横轴 x 从 -240 到 240,纵轴 y 从 -180 到 180。这一点必须记牢,因为后面做游戏时,判断角色是否碰到边缘、是否掉出屏幕,全部依赖坐标。点击角色面板里的造型标签,还可以给同一个角色画多个造型,运动时切换造型就能产生动画效果。
A Scratch project has exactly three areas: the Stage, the Sprite Pane and the Scripts area. The Stage is where the program runs, 480 pixels wide and 360 pixels tall. Sprites are the objects that move and react, and the Scripts area is where each sprite gets its own instructions. The centre of the Stage is the origin of the coordinate system: x runs from -240 to 240, and y runs from -180 to 180. Remember this, because every later rule about touching the edge or falling off the screen is expressed in coordinates. Each sprite can also hold several costumes, and switching costume during motion is how animation is produced.
2. 事件积木:程序从哪里开始 / Hat Blocks: Where a Program Begins
Scratch 是事件驱动的。脚本不会自动从上往下执行,而是等待某个事件发生才开始运行。最常用的三个帽形积木是:当绿旗被点击、当按下空格键、当角色被点击。凡是头顶不是圆角帽子的积木,单独放在脚本区是不会执行的,这是初学者最常见的困惑。举个典型例子:一个角色有两段脚本,一段以「当绿旗被点击」开头负责初始化位置,另一段以「当按下空格键」开头负责跳跃。两段脚本互不干扰,各自等自己的事件。如果需要在一开始时做统一设置,就用「当绿旗被点击」加一段初始化脚本,把位置、大小、变量全部重置。
Scratch is event-driven, which means a script does not run from top to bottom on its own. It waits for an event to happen first. The three hat blocks you will use most are when green flag clicked, when space key pressed and when this sprite clicked. Blocks without a rounded hat never run on their own, and that single fact explains most early confusion. A typical sprite holds two separate scripts: one starting with when green flag clicked that sets the starting position, and one starting with when space key pressed that makes it jump. The two scripts ignore each other and each waits for its own trigger. Use the green flag script as a reset routine that puts position, size and every variable back to a known state.
3. 运动与外观积木 / Motion and Looks Blocks
运动积木控制角色在舞台上的位置和朝向,常用的有「移动 10 步」「移到 x: y:」「将 x 坐标增加 10」「面向 90 方向」。这里有两个高频错误。第一,方向 90 表示向右,不是向上;向上是 0 度。第二,角色默认的旋转方式是「任意旋转」,当它面向左边时整个造型会上下颠倒,解决办法是在角色信息栏把旋转方式改成「左右翻转」。外观积木则负责视觉表现:说、想、切换造型、改变大小、显示与隐藏。做对话场景时,用「说 2 秒」按顺序排列即可;做动画时,用「下一个造型」加「等待 0.1 秒」放进循环,让造型连续切换。
Motion blocks control where a sprite sits and which way it faces. The ones you need constantly are move 10 steps, go to x y, change x by 10 and point in direction 90. Two mistakes appear again and again. First, direction 90 means right, not up; up is 0 degrees. Second, the default rotation style is all around, so a sprite facing left appears upside down. Fix it by choosing left-right flip in the sprite settings. Looks blocks handle everything visible: say, think, next costume, change size, show and hide. For dialogue, stack say blocks for 2 seconds in order. For animation, put next costume and wait 0.1 seconds inside a loop so costumes change continuously.
4. 循环:重复执行的三兄弟 / Loops: Repeat, Forever, Repeat Until
循环是 Scratch 里最重要的概念,没有它,程序会写成一堆重复的积木。Scratch 提供三种循环,用途完全不同。「重复 10 次」用于次数已知的情况,比如画一个正方形就是重复 4 次移动加转向。「重复执行」永不停止,适合游戏主循环,比如一直检测是否碰到边缘、一直更新计时器。「重复执行直到」用于次数未知但条件明确的场景,例如重复执行直到 y 坐标小于 -170,也就是一直往下掉直到落地。选错循环会让程序卡死或者看起来毫无反应,判断标准只有一个:你事先知不知道要重复多少次?知道就用重复 N 次,不知道就用重复执行直到。
Loops matter more than anything else in Scratch. Without them, a program grows into a pile of identical blocks. Scratch gives you three loops with genuinely different jobs. Repeat 10 times suits a known count, such as drawing a square with four moves and four turns. Forever never stops and belongs in a game main loop that checks the edge and updates a timer on every frame. Repeat until suits an unknown count with a clear stopping condition, for example repeat until y position is less than -170, which means falling until the sprite lands. Picking the wrong loop makes a program freeze or appear to do nothing, and the test is simple: do you know the number of repetitions in advance? If yes, use repeat N times. If no, use repeat until.
5. 条件判断:如果与如果否则 / Conditions: If and If-Else
条件积木让程序学会做决定。「如果…那么」只在条件成立时执行一次内部积木;「如果…那么…否则」在条件不成立时走另一条分支。做碰撞检测时,把「碰到 苹果」放进条件测试槽里,内部放加分和隐藏苹果的积木,就一定不会出现碰到却掉分的错误。更进阶的用法是把条件放进「重复执行」里,让它每一帧都检测一次。注意一个常见陷阱:两个独立的「如果」会同时成立,而「如果否则」只会执行其中一支。判断得分等级时(大于 100 是优秀,否则是继续努力),必须用如果否则,不能用两个如果,否则两句话会同时显示出来。
Condition blocks let a program make decisions. The if then block runs its contents once, only when the test is true, while if then else picks between two branches. For collision detection, drop the touching Apple block into the hexagonal test slot and put the score change and hide blocks inside, and you cannot accidentally score for a miss. A more advanced pattern is to place conditions inside a forever loop so the test runs on every frame. Watch one trap: two separate if blocks can both fire, whereas if else runs exactly one branch. When you grade a score, above 100 means excellent and anything else means keep practising, so you must use if else. Two plain if blocks would both be true at the same time and show both messages.
6. 变量与计分系统 / Variables and the Scoring System
变量是存放数据的盒子。在 Scratch 里新建变量时,会问你是「适用于所有角色」还是「仅适用于当前角色」。计分、生命值、计时器这类全局数据必须选适用于所有角色,否则每次克隆体都会各算各的,分数永远不动。变量的四个基本操作是:设为、增加、显示、隐藏。做计分系统的标准写法是:绿旗点击时把「分数」设为 0;苹果被接住时把「分数」增加 1。除了自己建的变量,Scratch 还自带两个只读变量:x 坐标、y 坐标和方向,它们不能手动修改,但可以用来做判断,比如判断角色是否已经贴着右边缘。变量与列表配合,还能做排行榜、错题记录这类功能。
A variable is a labelled box that stores a value. When you create one, Scratch asks whether it should apply to all sprites or only to this sprite. Global data such as the score, lives and a timer must apply to all sprites. If you choose only this sprite, every clone keeps a private copy and the score never moves. The four basic operations are set, change, show and hide. A correct scoring system looks like this: when the green flag is clicked, set score to 0; when the apple is caught, change score by 1. Scratch also provides read-only reporters for x position, y position and direction. You cannot set them by hand, but you can test them, for example to check whether a sprite has reached the right edge. Combined with lists, variables also support leaderboards and mistake logs.
7. 广播与克隆:让多个角色协作 / Broadcasting and Cloning
当项目里有多个角色时,它们需要互相通知。广播就是角色之间的喊话机制:一个角色发出「广播 开始游戏」,所有收到「当接收到 开始游戏」脚本的角色会同时行动。用广播做游戏状态切换,比每个角色各自检测按键要整洁得多。克隆则用于数量不确定的重复对象,比如不断落下的苹果。用「当作为克隆体启动时」加上「删除此克隆体」可以精确控制每一个克隆的生命周期。这里有一个必须记住的坑:克隆体会继承母体的属性,如果在母体里修改了分数,所有克隆体看到的是同一个全局变量;但如果修改的是位置和大小这类角色属性,每个克隆体各有一份,互不影响。
Once a project has several sprites, they need to notify each other. Broadcasting is how one sprite calls out to the rest: one sprite sends broadcast start game, and every sprite holding a when I receive start game script reacts at the same moment. Using broadcasts to switch game state is far cleaner than having every sprite check the keyboard separately. Cloning handles repeated objects whose number is not fixed, such as apples that keep falling. The pair when I start as a clone and delete this clone gives you precise control over each clone's lifetime. One trap is worth memorising: a clone inherits its parent's properties. A global variable such as the score is shared by all clones, but sprite properties such as position and size are copied and change independently in each clone.
8. 项目实战:制作接苹果小游戏 / Project: Build an Apple Catcher Game
现在把上面所有积木组合成一个完整项目。第一步,画一个篮子作为玩家角色,一根苹果作为掉落物。第二步,篮子脚本:当绿旗被点击时移到 x 0 y -120,然后重复执行「将 x 坐标设为 鼠标的 x 坐标」,让篮子跟随鼠标。第三步,苹果脚本:当绿旗被点击时把分数设为 0,然后重复执行以下动作:移到 x 随机取 -220 到 220、y 设为 180,重复执行直到碰到篮子或者 y 坐标小于 -170,每次把 y 增加 -5。第四步,在循环内部加条件:如果碰到篮子,则把分数增加 1 并且隐藏。第五步,用广播「游戏结束」结束所有脚本,并让另一个角色显示最终分数。这个项目完整覆盖了事件、坐标、循环、条件、变量和克隆六大知识点,做完它,你就完成了 KS3 计算思维的核心训练。
Now put every block above into one working project. First, draw a basket as the player sprite and an apple as the falling object. Second, the basket script: when the green flag is clicked, go to x 0 y -120, then use forever to set x to the mouse x position so the basket follows the pointer. Third, the apple script: set score to 0, then forever go to a random x between -220 and 220 with y set to 180, and repeat until touching the basket or y position is less than -170, changing y by -5 each time. Fourth, add a condition inside that loop: if touching the basket, change score by 1 and hide the apple. Fifth, broadcast game over to stop the scripts and have a third sprite display the final score. The project covers events, coordinates, loops, conditions, variables and clones, which is the core of KS3 computational thinking.
9. 常见错误与调试方法 / Common Bugs and Debugging
Scratch 项目出问题时,九成是下面五个原因。一是脚本没有帽子积木,静静地躺在那儿不执行。二是循环用成了「重复执行」,导致角色一帧移动几百步,看起来像瞬间消失。三是变量选成了「仅适用于当前角色」,分数永远停在 0。四是两个条件写成两个独立的「如果」,两句提示同时出现。五是克隆体没有删除,几千个克隆体拖着程序变卡。调试的正确顺序是:先看脚本是否被触发,再看循环次数,最后检查变量作用域。有一个非常实用的技巧:把「说 分数」放在循环里,让程序把内部状态实时显示出来,你立刻就能看到数据在第几步开始出错。
When a Scratch project misbehaves, nine times out of ten it is one of five causes. The script has no hat block and simply sits there. A forever loop moves a sprite several hundred steps per frame, so the sprite appears to vanish. A variable was created for this sprite only, so the score stays at 0 forever. Two conditions were written as separate if blocks and both messages appear at once. Clones are never deleted, so thousands of them slow the project down. Debug in a fixed order: confirm the script is triggered, check the loop count, then check variable scope. One technique is genuinely useful: place a say score block inside the loop so the program reports its own internal state, and you can immediately see the exact step where the data first goes wrong.
10. KS3 评估要点与进阶方向 / KS3 Assessment and Next Steps
KS3 的计算课程评估通常不看你做了多漂亮的动画,而是看你能否说明程序为什么这样写。批改标准一般分三层:能否使用序列、选择、循环三种基本结构;能否用变量保存和更新数据;能否发现并修正自己程序中的错误。所以,写完之后请用三句话讲清你的项目:程序的输入是什么,处理过程做了什么,输出又是什么。掌握 Scratch 之后,进阶路径非常自然:变量与列表对应 Python 的变量与列表,循环与条件在两种语言里写法几乎一致,事件与广播对应 Python 里的函数调用。可以说,Scratch 学得扎实的学生,转到文本编程时省下的时间,远远超过当初拼积木花掉的时间。
Assessment in KS3 Computing rarely rewards a pretty animation. It rewards whether you can explain why a program is written the way it is. The usual marks sit in three layers: can you use sequence, selection and iteration, can you store and update data in variables, and can you find and fix errors in your own work. Before submitting, describe the project in three sentences: what the input is, what the processing does and what the output is. The path onward is natural. Variables and lists in Scratch map onto variables and lists in Python, loops and conditions look almost the same in both languages, and events and broadcasts correspond to function calls. Students who learn Scratch properly save far more time later than they spend snapping blocks now.
戴睿(dere)Neo优选系列 创意双触摸屏高性能笔记本电脑大学生AI学习商务办公设计炒股娱乐手提轻薄本32/1TB¥4586.0📱 去京东购买京东精选需要一对一辅导?Need One-on-One Tutoring?
KS3 / GCSE / A-Level / IB 全科辅导,编程与计算机科学课程可预约试听

或截图后用「扫一扫 → 相册」选择本图