Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

In this simple project you will learn how to make the game Hungry bat in Scratch.

What is Hungry bat?

In this game, your character is a bat. You can control this with the arrow keys. The aim of the game is to guide the bat to the taco without touching the yellow walls.

What is Scratch?

Scratch is a programming program for you Raspberry Pi. In this program you work with function blocks. This means that you do not write the programming text yourself. The blocks with the functions are already made for you. You put these blocks in a schedule yourself. If you have the Raspbian software on your Raspberry Pi Once downloaded, Scratch should be automatically installed on your Pi. If not, you need to download it.

I program in Scratch on the Raspberry Pi 4 with 4GB. You can also program on the Raspberry Pi 3B+. You can also use the 4 GB or 1 GB with the Pi 2.

Do you program in Scratch 2.0? Then you can still follow this tutorial. Keep in mind that certain functions may be in a different place. The pictures may also look different than on your screen. You can follow this tutorial because the code is the same.

Change language.

Your program is probably in English. To follow the steps properly, it is wise if you change the language in your program. You can change the language by clicking on the “globe” at the top left of your screen. This icon can be found next to the Scratch logo. Select Dutch.

 

  • Level - beginner 33% 33%
  • Duration - 15 Min 20.% 20.%
  • Cost - € 82.3. euros complete 50% 50%

Step 2: Connect & Start Up

On the side of the Raspberry Pi 4B are two Micro HDMI ports. Plug the Micro HDMI cable into the left port, and the HDMI end into a monitor or display.
Connect a keyboard & mouse to the USB ports and finally insert the power cable into the USB-C port.

Your setup is now ready to start.
If the de Raspberry Pi Now you start up you will see a desktop on your monitor. When you start up for the first time, you have to set it up, but that is explained step by step.

Step 3: Programming

First we will choose a character. Next to the text: New Sprite: if you see a head, click on it. Now you all see standard Sprites (characters). For this example I choose Bat2 (Figure 1). But of course you can also choose a different character. If you have chosen a nice character, press OK. You can remove the default character. Right click on the icon. Then click on delete

Figure 1

Now we will write the code so that your character can be controlled.

  1. Go to Events and grab the “when (flag) is clicked”.
  2. Go to Movement and take the “go to X: - Y: -” place it under the start function.
  3. Decide for yourself where you want the ball to start, for convenience I do in the top left of the screen. You can always adjust it. If you know what you want to start, read the X and Y values ​​and enter them.
  4. Go to Events and grab the “when - is pressed” place it next to the code on your screen. Check the arrow up.
  5. Copy this block 3 more times. Also place these side by side on your screen. With these blocks also name the down, right and left arrows.
  6. Go to Motion and take the “aim to degrees” and the “take steps”. Place this function under “when up arrow is pressed”. The order of these functions does not matter.
  7. Copy these 2 functions and also put them under the rest of the "when - is pressed"
  8. For the up arrow, enter: aim 0 degrees. Take 10 steps.
  9. For down arrow enter: aim 180 degrees. Take 10 steps.
  10. For the arrow on the right you enter: aim 90 degrees. Take 10 steps.
  11. For the left arrow enter: aim -90 degrees. Take 10 steps.
  12. The grades determine the direction in which the character moves. The number of steps determines how fast it moves.
  13. Test for yourself if your code works.
  14. Your code looks something like this (Figure 2).

Figure 2

Now we are going to make our maze. We do this in the background. Figuring out a background is a bit like your character. Because we are going to make our own background, we click on the brush. With the square you can draw walls around which you have to move the character around. Experiment a bit on what you like to look like. By drawing these walls you can make the game more difficult by putting them close together, or easier by making the distances bigger (Figure 3).

      Figure 3

      Now we create the finish.

      1. Create a new Sprite (character). When your character reaches the finish you have won the game. For the finish I choose the taco, place it in the background. Remember that where you place it is your finish.
      2. Go back to the code of your bat.
      3. Go to Events and grab the “when (flag) is clicked”.
      4. Then go to Control and take the repeat block. Put this under the start function.
      5. Then take the “if then” block and put it in the repeat block.
      6. Go to Observe and get the "do I touch -?" put this function between the "if then". Press the arrow and select the taco.
      7. Then go to Looks and get the "say -". For example, write after the saying "Yum!"
      8. Go to control and take the “stop” function. Press the arrow and search for “all scips in Sprite”. Place this above the say function.
      9. Test for yourself if the code works.
      10. Your code now looks something like this (Figure 4).
      11. On your screen, it will look something like this (Figure 5).

      Figure 4

      Figure 5

      As you can see the character is too big for me (Figure 5), it comes directly against the yellow walls. We will adjust this.

      1. To adjust the size of the character we go to Looks and choose the “make size with -%” function. Place this under the start function.
      2. Change the percentage according to your own wishes, the lower the percentage the smaller your character becomes.
      3. After you have adjusted the size, your character is probably no longer in the correct position. Adjust the X and Y values ​​again for this starting position.

      The bat has an extra appearance as standard. To check if your character has this too, go to Looks. There you should see 2 or more versions of your character. You can also create an appearance yourself.

      1. To give our character an animation, we go to Control. Grab a repeat block and put it at the bottom of your start function.
      2. Go to Looks and get a “next look” and put it in the repeat block.
      3. Go to Control and grab a “wait - seconds” function. Place this at the bottom of the repeat block. You can adjust the number of seconds to make the animation faster or slower.
      4. Your code now looks something like this (Figure 6). (The X and Y values ​​are different after I adjust it to the size of the character).
      5. On your screen it still looks something like this (Figure 7).

        Figure 6

        Figure 7

        Now we are going to create a loss function.

        1. Go to Events and grab the “when (flag) is clicked”.
        2. Then go to Control and take the repeat block. Put this under the start function.
        3. Grab the “If — then” block and put it in the repeat block.
        4. Grab the “stop -” and check the option “all scrips in sprite”. Place this function in the “If — then”
        5. Go to observe and take "I touch color -" and put it between "If — then". Click on the colored box in the code and then click on one of your walls (yellow).
        6. Then pick up the “say” function at Looks. Place this in the “If — then” block. Write behind say: Game over.
        7. Test for yourself if the code works, your character says "Game over" when it touches a yellow wall. At that time you can no longer control it.
        8. Your code now looks something like this (Figure 8).

            Figure 8

            Now you are done with your hungry bat game! Did you like this? Experiment a little with this program. You can try adding sound effects yourself. Or you can add other objects in the game to collect the character. Lots of fun!

            The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 4782 reviews.