Connecting MicroBlocks to Snap!
Introduction to Snap!
- In this session we will see how we can create a video game with Snap!, a programming environment similar to Scratch that allows us to develop more complex projects.
- The idea will be to control it through the ED1 board, to which we will load a program called Signada, in order to be able to interact with the video game created with Snap!
- Then to Snap! we will load a library with the same name that will allow us to connect to the board and interact with it.
Movement of the ship
- We have a template available with the objects already created at this link.
- We will start by programming the movement of the ship. Since we only want to move it from left to right, we just need to modify the x component of its position using the *set x to _ block in the Movement category.
- To test it for now we can use the mouse to move it, we need the mouse x block that is in the Sensors category.
Challenge 1: Make the ship always follow the mouse x when starting the game
Solution to challenge 1
Shoot Laser (I)
To fire the laser we will create copies or "clones" of the Laser object. To do this we need to look for the create a clone of _ block that we have in Control.
Instead of "myself" we must select the Laser object.
We can do the shooting action when we click the mouse, so we will have to look for the block corresponding to Sensors and use a conditional to execute the action.
Shoot Laser (II)
- To create copies of the Laser we will first select it and look for the block when I start as a clone. You must select the laser object to apply this code.
-Once the copy is created, we will make it go to the ship and also point in the same direction as it.
- Finally, we will make it move until it touches the edge, where we will also delete it.
Challenge 2: Make the ship always follow the mouse x when starting the game
Solution to challenge 2
Asteroid movement (I)
- With the asteroids we will do the same, we will create copies that fall from top to bottom.
- To do this, we will run the create clone block every 2 seconds (for example) from the start.
- Once the copy is created, we'll make it go to a point on the top edge with a random x. For this we will need the pick random _ to _ block that we have in Operators.
Asteroid movement (II)
- Once the copy of the asteroid is located, we will make it move down by subtracting 1 (or rather, adding -1) to its y using the change y by _.
- In addition to falling, we can also make it rotate using the rotate _ degrees block.
- To make the copy deleted when the Laser is touched or or when its y-position is lower than -200, you need to use a conditional with the _ or_ block.
Challenge 3: Make the Asteroid object hide at startup and only show when the copy is created
Solution to challenge 3
End of game (I)
- The idea is that the game ends once the ship collides with an asteroid.
- Although this check can be done on either one object or the other, in this example we will do it on the ship.
- The only thing that needs to be done is that, in case of touching the Asteroid, send a "game over" message and also hide the ship (as if it had been destroyed).
- Don't forget to make it show at the start, otherwise it will stay hidden!
End of game (II)
- When the message is received we will make the Game Over object appear and stop the program using the stop all block.
- In addition, it will be necessary to hide the Game Over object at the beginning, since otherwise it will remain visible the whole time while we play!
Signada MicroBlocks
- To be able to control the ship remotely with the ED1 board, it will be necessary to load a program on it using MicroBlocks.
- This program is located in the Network folder and is called Signada (network remote control).
- Once opened, it is necessary to enter the credentials of the WiFi network in the indicated block.
Signada (Snap!)
- On Snap! you will need to load the Signada (network remote control) library.
- Once loaded, we will see that a few blocks are added in some categories, for example in Movement, Sound, Pen, Sensors and Variables.
- In our case we are mainly interested in the blocks of the last two categories.
Connection with board
To connect the Snap! with the ED1 it will be necessary to use the block connect to _ using the IP shown on the screen.
If the connection is successful, the screen will change to show the text "Connected.".
We can also check the connection status in Snap! through the connected block.
- Once connected we can make the appropriate changes to use the board to control the ship.
- For example, instead of the x-position of the mouse, we will use the tilt in the x-axis of the plate to move the ship from one side to the other.
- And to shoot the laser we can use either the OK button or the X.
Final Challenge: Complete the game!
⬅️ Previous activity |
---|
© von Citilab Edutec 2019-2022.
This work is licensed under a CC-BY-SA 4.0 license.