这一讲,我们将以猫猫侠识别颜色为例,讲解控制模块“如果…..那么”的用法,以及综合运用侦测模块“碰到颜色”脚本的用法。
第1步:在角色区域,选择“绘制新角色”按钮,分别绘制“红色、绿色、紫色和蓝色”4个长方形的颜色柱,并根据它们的颜色命名。如图所示:

2步:调整猫猫侠的初始大小和初始位置。假设我们给猫猫侠的初始大小设定为50,初始值为x:-207,y:-22,如图所示:

第3步:当定义好猫猫侠的初始位置后,我们再调整各个颜色柱的位置,如图所示:

第4步:我们假设猫猫侠如果运动了最右边,那么它就返回初始位置重新运动。此时我们就需要用到“如果…….那么…….否则”命令。如图所示:

第5步:我们假设当猫猫侠经过红色柱形图时,说出“这是红色”;当经过绿色柱形图时,说出“这是绿色”;当经过紫色柱形图时,说出“这是紫色”;当经过蓝色柱形图时,说出“这是蓝色”。此时我们就需要用到“如果…….那么”命令。其代码如下:

其完整代码如下:

用另一种代码也可以完成相同的功能,大家可以看看下面的这段代码,与上面的功能完全一致。

网友评论