Scratch programming examples for beginners.

Let’s continue our journey.  On this occasion, we will guide you through Scratch, programming examples for beginners to construct an exciting game.

If you have previous knowledge, you can follow this step-by-step guide. If not, we recommend trying our previous post:

Then, let’s move on and learn new programming concepts.

Loops:

Sometimes, we must repeat an instruction block two, three, or thousands of times.  But we don’t need to include these blocks N times.  To get that, programming languages provide a tool called loop or iteration.

What exactly loop is? The loop is a sequence of instructions continually repeated until a certain condition is reached.

Where are the loops available in Scratch?

Open your Scratch and look at the left-hand side.

On the code palette, we can see a variety of code categories. The first is motion, the second is the looks category, and the fifth is control. In controls, we can see various loop types and conditional statements, so the first two sequences are a repeat loop and a forever loop.

Repeat loop:

Suppose you want to repeat the code block a fixed number of times. That time, we can use the repeat. Repeat Loop helps us to put limitations on the repetition of the instruction. By default, it is a reproduction of 10. So, the program will execute the ten repetitions. If you want a process to repeat only 3 to 4 times, we can change this 10 number to our number, 3 or  4,  so we need to pick the instructions and place them in the code area. After clicking inside, you can change the number.

Instead of using a constant value, we have many options, like using a variable that stores a number, using a math operation, picking a random number between a range, etc.  We’ll be explaining this in another post.

Forever loop:

Forever loop is the loop that repeats/runs the code inside it unlimited times. That’s why it is called forever, which means there will be no limit to the repetitions. It’s forever.

How to import the Sprite?

For importing the Sprite externally:

  1. Download the image which you want to use in our game. I am downloading chick and egg images from Google.
  2. Open the scratch application. We can see some options for Sprite and the backdrop on the right-hand side.
  3. For importing Sprite, we need to hover over the cursor on the Sprite, and there will be four options:
    1. Choose
    2. Paint
    3. Surprise
    4. Upload.
  4. Click on Upload.  A file explorer window will open.
  5. Choose the Sprite you want to import or upload.

That’s all.  You are now able to see your Sprite.

You must follow these steps to import the backdrop of your choice. Here is a reference video.

(Process of uploading Sprite and backdrop)

How to make a variable?

  1. Open Scratch.
  2. Variables are on the left-hand side, on the code palate penultimate option.
  3. Click on variables
  4. Click make a variable.
  5. Give a name to your variable
  6. Click ok.

You can see your variable on the stage.

Let’s make a jumping game in Scratch 3.0.

To make it simple to understand, let’s divide the game into some steps. First, follow these steps to make the game:

  • First, choose a chick and egg sprite from the scratch gallery or upload it externally.
  • Choose or upload the backdrop of your choice.
Let’s make the chick jump.

Section 1:

  1. 1. Take the flag event from the events category of the code Palette.
  2.  Pick the “show” instruction from the Looks category and join it after the flag event.
  3.  Use go-to x:0 y:0 instruction from the Motion category.
  4.  Let’s make a variable that will show the score and name it Score.
  5. Pick the “set my variable to 0″ instruction. Next, click on the dropdown menu and select your variable name.
  6. It is connected to the go-to instruction. So, initially, the score will always be zero.

This coding is for fixing the variable and position of our Sprite.

Section 2:

  1. Get the when space key pressed event from the event category.
  2. Then, connect the repeat loop from the controls category to the event.
  3. Now we want to move our chick up. First, we need to change its y position.  Pick the instruction change y by ten from the motion category.
  4. Connect it inside the repeat loop.
  5. Now our chick is changing y position upside. So we want the babe to come down for that.
  6. .Take another repeat loop and connect after the first repeat loop.
    Get the change y by ten instruction from the Motion category and change y by -10 because it must come down.
  7. Click on a Variable category and get change my variable by 1
  8. Connected after the second repeat loop.
  9. Next, click on the dropdown menu and select our variable name.
  10. That’s all. Now the chick can jump after pressing the space key.
Scratch programming examples for beginners: The Chick Code
Let’s make our egg move toward the chick.
  1. Place the egg parallel to the position of the chick on the right side.
  2. Grab the flag event.
  3. Pick go to x: y: instruction. Be sure the egg is on the right side of the stage. Then, connect it to the event.
  4. Placing the chick on the left side means the final position while moving.
  5.  Pick glide 1 sec to x, y instruction, and connect it to the previous education. You can change the timing inside the glide.
  6.  Put this code inside the forever loop. Get a forever loop from controls, connect it to the flag event, and place the go-to and glide instructions inside it.

That’s all for making the egg glide.

Condition:

Now we need to check if the chick is touching the egg. If it feels, then the game should stop. So let’s check this condition. This condition comes in the sensing category. On the left side of Scratch. You can see the sensing block.

Again go on the egg’s code.

  1. We want to check this condition again and again throughout the game. So, pick the flag event from the Event category, and place it in the code area.
  2. Get a forever loop from the Control category and connect it to the Flag event.
  3. Get the if statement block from the Control category.  Connect it inside the forever loop. This will check the condition.
  4. Go to the Sensing category, and pick Touching instruction, which is the first instruction. Place it inside the if statement.
  5. Click on the dropdown of this instruction and select the sprite name. So the condition will be like if touching a chick?
  6.  Get broadcast message instructions and connect it inside If.
  7.  Get stop all from control and click it after broadcast instruction inside the if statement.
  8. After touching chick to egg, We need a game over the Sprite. So the game will finish.
Scratch programming examples for beginners: Egg Code
Create your Sprite:

We can create the sprites by painting on canvas.
1. Go to the sprite area and click on paint.
2. Select the text. Select the color of the text and text style. Click on the canvas and write “Game Over.”
3. That’s all game- over Sprite is ready. You can rename it.

For example:

As this is a game-over sprite, it must display when the game is over.
And it should not be visible while starting the game.

  1. Get Flag event.
  2.  Connect Hideinstruction after the event.
  3. Get when I receive the broadcasted message instruction from control.
  4. Get show instructions from looks and connect them to the event.
Game Over sprite code

Congratulations🎉, your game is ready.

Remember, you can find more exciting Scratch programming examples for beginners at https://scratch.mit.edu

Comments

6 responses to “Scratch programming examples for beginners.”

  1. […] You can find more projects on http://www.scratch.mit.edu. or try our next post: Scratch programming examples for beginners. […]

  2. […] Scratch programming examples for beginners. […]

  3. […] you wrote within the last four chapters helped you learn essential Scratch programming skills and Scratch programming examples for beginners. However, they lacked many key elements of a large-scale application. In addition, more complex […]

  4. […] 3: Scratch Projects for kids: Condition, loop… […]

  5. […] Scratch programming examples for beginners. […]