玩Scratch创作系列教程,旨在通过完成一个个Scratch编程实例,教Scratch编程。在使用Scratch的过程中,了解Scratch的特点、编程思维,掌握编程的方法、技巧,学习编程的一般思路。
01 小猫散步
学习任务
我们来用Scratch编写一个程序,通过指挥屏幕空白区域的这只小猫(图1),让它听我们的命令:散步。

打开Scratch在线编辑器
在浏览器的地址栏输入 scratch.mit.edu ,打开Scratch官方网站。

这是Scratch官方网站(如图2)。网站的默认语言是英语。拖动右侧的滚动条,在页面最底端,选择语言“简体中文”,网站的框架就会自动显示为中文。
简单介绍一下:Scratch是麻省理工学院网站的一个子站点。这个软件的开发团队称为“终身幼儿园团队”。

再点击页面顶端的“创建”,进入“创建”页面(如图1),就打开了Scratch在线编辑器。
现在,我们可以在这里进行在线编程了。
让小猫走起来
记得我们要创建什么程序吗?

对。我们先让小猫走几步。
看这里,有“积木”一样的东西,这些都是指令。例如:

按住鼠标左键,拖拽到脚本区(就是图1中右边的灰色区域),松开鼠标左键。

就被添加进来了。可小猫有反应吗?

再拖拽一次,观察小猫。

没有动就对了。因为我们还没有让它执行命令。鼠标单击拖进来的“积木”,观察小猫,动了没有?再单击一次,试试。

对。小猫每次移动了多少呢?10步。你怎么知道?指令上写的是“移动10步”。能移动多一点吗?怎么做?

不错。我们可以把“10”改成别的数字。试试改成“100”,

是什么效果?

想让小猫移动慢一点,又能看到小猫走了一段不短的路,怎么办?我们要用到另一块“积木”——

将这块“重复执行”指令积木,移到

附近,就会出现白色的拼接提示:

松开鼠标,就变成

这样,两块“积木”就拼在一起啦。点击这两块积木中的一块,看看小猫是怎样动的?拽住小猫的尾巴,拖出来,试试看。

观察脚本区:

脚本一直处于高亮状态,说明程序一直在运行这段脚本。
怎样让小猫停下来?按舞台区域右上角的红色按钮,即可。(如图3)


别急,有一块“积木”,

将“碰到边缘就反弹”这条指令拖到

里面来。什么时候松开鼠标呢?在有白色连接提示的时候,就可以松开鼠标。

完成以后,是这样的:

在这些积木块上,点击鼠标左键,让它运行看看。


很好,发现问题,我们就来解决。在程序编写过程中,我们要经常这样:运行程序,看看有什么不符合我们的设想的,这个过程很重要。
有一条指令能解决小猫倒着走的问题。你能发现吗?
在“动作”模块区域中,有“将旋转模式设定为左右翻转”指令积木块:

将它拖进来,放到“重复执行”的上面,与“重复执行”连接起来。这样,脚本就变成了:

在这些积木块上,点击鼠标左键,看看程序运行的效果。

还可以更炫。虽然小猫现在在跑,可只是在移动。添加一个指令,可以使动画效果更好。在“动作”的旁边,有“外观”模块,与外观有关的指令都在这里。点“外观”,你会发现,供我们选择的指令都变了。

选择其中的”下一个造型“指令积木块:

拖拽到“重复执行”里,只要放到“重复执行”里,具体位置不限,习惯上,我们放到”碰到边缘就反弹“的下面,这时,程序变成了:

这段程序是什么意思呢?将旋转模式设定为左右翻转,然后重复执行这几条指令:移动10步;碰到边缘就反弹;下一个造型。单击这段程序指令积木块,运行一下,看看。

当然可以。有什么指令,让小猫跑慢一点呢?这条指令在程序设计中经常用到,就是

这条指令,在“控制”模块里。先点击“动作”右边的“控制”,然后就能找到这条指令了。将它拖到“重复执行”里面。将“1”秒改为 “0.5”秒。现在是这个样子:

再运行,看看舞台区小猫是怎样运动的。

更换背景
好。我们来挑一个好看的背景。在左下方“舞台”这里,点下面的四个按钮,就可以设置舞台了。点第一个按钮,从图库中导入一个舞台背景。沙滩还是蛮不错的,小猫在沙滩上漫步,挺好。选中背景图片,点“确定”按钮。


再点击左下角的小猫,点击脚本,调试一下。怎么样,我们做出了一个令人满意的作品!
事件响应
按舞台区域右上角的红色按钮:

可让小猫停下来。
在红色按钮的旁边,有个绿旗

是用来控制程序的开始的。点击绿旗,小猫没有反应。这又是为什么呢?
我们没有给小猫相应的指令。这条指令应该是:点击绿旗就开始运行。这样的指令在哪儿呢?在“动作”的右边,点“事件”模块,指令区里有”当(绿旗)被点击“积木块:

将它拖到脚本上,放在最上面,与之前的脚本连接起来,如图8:

这时,再点绿旗,小猫就动起来了!
添加角色,练习
我们指挥小猫,让它在沙滩上散步了。在指挥小猫的过程中,我们学习了通过拖动指令来搭建程序,知道了程序是按顺序执行指令的。
你可以通过练习来熟悉今天学习的指令。
比如,我想指挥小狗来散步。从角色库中导入小狗角色:



然后,拖动指令,让小狗走起来吧!
试试指挥别的角色、换个别的背景,练习练习今天学到的创作技巧吧。
保存作品
完成创作以后,要及时保存作品!
如何将作品保存到本地计算机?
点击左上方的“文件”菜单下的“下载到您的计算机”(如图12)。

在打开的保存窗口中,选择文件保存的路径。这里我保存到“D:\玩Scratch创作”文件夹里。命名为“小猫散步.sb2”。(“.sb2”是Scratch 2的文件类型,只能用Scratch 2打开;Scratch 1.4版打不开“.sb2”文件,只能打开“.sb”文件)。

再看Scratch在线编辑器,文件名已改为了“小猫散步”。

当然,你也可以将作品保存到官方网站。保存为在线文件的方法,请点击链接,查看另一篇文章:在线分享你的Scratch作品 。
网友评论