自宅で始めるScratch(https://dk.cheerup.jp/scratch-1st-step/)で簡単に説明しましたが、さらに詳しいことを学んで頂きたいので、Scratchの連載を開始します。
前回は、音ブロックの説明を行いました。
今回は制御ブロック(プログラム)を紹介しながら説明を行います。
制御ブロック
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control.png)
1秒待つ
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_03.png)
一秒待ってから処理を行います。
下記のウィンドウでは、3秒に設定してあり、タイマーで計測しています。
緑の旗を押してから3秒後にスプライトが動き出しています。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/delay_prog.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/delay.gif)
10回繰り返す、ずっと
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_06.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_08.png)
こちらのブロックは、決められた回数、もしくは処理を繰り返します。
下記は、どこかの場所へ行くブロックを「ずっと」処理しています。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/loop_prog.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/loop.gif)
もし なら、もし なら でなければ
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_11.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_12.png)
こちらのブロックは、後述の「調べるブロック」と「演算ブロック」の結果を元に処理をする制御ブロックです。
例として、下記のブロックを使用します。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_18.png)
このブロックを使用して、下記の様なプログラムを作成しました。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/spacekey_prog.png)
スペースキーを押しながら実行すると、
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/w_space_key.gif)
スペースキーを押さずに実行すると、
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/wo_space_key.gif)
のように状況に応じて制御できるブロックになります。
まで待つ、まで繰り返す
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_13.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_15.png)
下記のプログラムは、スペースキーが押されるまでどこかの場所に動き回る例です。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/loop2.png)
実行途中にスペースキーを押すと、動きが止まります。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/loop2_prog.gif)
すべてを止める
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_18.png)
このブロックは、繰り返されている処理を止めることが出来ます。
止められる種類は、すべてを止める、このスクリプトを止める、スプライトの他のスクリプトを止める、の三種類があります。
クローンされたとき、自分自身のクローンを作る、このクローンを削除する
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_21.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_23.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/control_blocks_25.png)
このブロックは特殊なブロックになっており、スプライトをプログラム上で作ることができます。
下記のプログラムでは、上向きの矢印が押されると、自分自身のクローンを作ります。
クローンとはスプライトの分身と考えておいて構いません。
クローンが出来たらさらにプログラムを作って、クローンが動き回るプログラムを作っても良いと思います。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/clone_prog.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/clone.gif)
制御ブロックに使える「調べるブロック」
調べるブロックは別の機会に詳しく説明しますので、今回は簡単に紹介します。
マウスのポインターに触れた、色に触れた、色が色に触れた
下記のブロックは、何かに触れた時に真(True)と判定されるブロックです。
触れていない場合は、偽(False)と判定されます。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_03.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_05.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_07.png)
スペースキーが押された、マウスが押された
下記のブロックは、スペースキーやマウスが押された時に真(True)と判定されるブロックです。 押されていない場合は、偽(False)と判定されます。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_18.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/sensing_blocks_20.png)
制御ブロックに使える「演算ブロック」
下記のブロックは演算(計算)の結果、それが正しければ真(True)と判定されるブロックです。正しくない場合は、偽(False)と判定されます。
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_16.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_18.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_19.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_21.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_23.png)
![control](https://dk.cheerup.jp/wp-content/uploads/2021/12/operator_blocks_25.png)
まとめ
今回は、制御ブロックをご紹介しました。
制御ブロックはプログラムには欠かせないループやIF文などの分岐構造を作ることができます。IF文を作成するためには、真偽の作り方を理解する必要があります。
調べるブロックは比較的低学年でも理解できますが、演算ブロックは算数の要素が入りますので、ここで算数も学んでしまいましょう!
次回は、「調べるブロック」を取り上げる予定です。