DIY Automatic Railway Crossing
This project will show you how to make a DIY automatic railway crossing.
In this chapter, we will make a game which will involve shooting bats using a joystick. Well not exactly shooting, no one wants PETA running after them for no reason; instead we will adopt a more non-violent approach and only aim at bats. They’ll disappear once we correctly aim at them, but hey, at least they won’t die.
|Image||Component||Quantity||Available in Kit|
|XY-axis Joystick Module||1|
|Male to Female Jumper Cable 20cm||5|
|USB A-B Cable||1|
Notice the basic elements of the game. Observe that there is a planet in the distance, which is probably Saturn (rings) so we are most probably on some moon of Saturn. Bats on a moon of Saturn? Does life exist in Saturn’s proximity? Strange isn’t it? Could our simple Scratch game discredit years of research by NASA? Boy, aren’t we on the cusp of creating history. Look at the resplendent yellow stars shining against the gloomy Grey sky and before you turn into a nature-loving romantic instead of an innovator, let’s clear things up: WE WERE JUST KIDDING. You can skip this paragraph for what it’s worth.
However scientifically inaccurate the Stage may seem, we’ll have to work with it. The Stage is only for effects and aesthetic purposes; we should treat it as such. The actually important aspects of the game which you should note are:
The Bat Sprite: Just like the Panda sprite (which is default), we can have many other sprites, one of which is the bat sprite. Bats will fly around the stage one at the time and we will aim at them using the Aim sprite, which is another important aspect of the game.
The Aim Sprite: The bats shall fly around randomly but we will have to aim at them using the Aim sprite. We will move the aim sprite using a joystick. When the aim sprite will touch a bat, the bat will disappear.
Score: Your score, i.e. – number of bats you aim correctly at will be displayed at the top left corner.
Timer: Time remaining before the bats stop coming and the game gets over.
Scratch is a graphical programming language which let you interface and control your evive with laptop. If you have not installed Scratch visit here to get full instruction on installing Scratch.
To learn about the Scratch interface, visit here. There are four important things you have to know in Scratch:
We have developed these Scratch extensions containing special blocks through which you can control your evive in Scratch:
In Scratch, there are two modes in which you can work:
In this project we are using stage and sprite, hence we have to work in Scratch mode to use the block which senses the keyboard input.
First things first, let us select the backdrop for our stage and the bat sprite. Without them, there would be no game. Below the stage, to the left, you will see a “New backdrop” option. mBlock’s library has our required backdrops. Click on the icon which says – “Choose Backdrop from library”. A list of backdrops will appear. The backdrop we want is titled “space”. Double-click on that. You will see the change that happens.
Double-click on “Bat1” sprite. Now, there are two sprites – bat and panda.
As much as we love our dear friend panda, we will have to let go of him. Select the icon (small panda) for the Panda sprite (below the stage) and right click on it. One of the options in the drop down menu is “delete”. Click on it and the Panda will disappear.
Now the “Stage is set” for us to start with our game.
The bat we created right now has stationary wings. Surely, an animal that flies must know how to flap its’ wings. Let us then create an animation so that the bat flaps its’ wings while flying. Switch to the “Costumes” tab. Observe that, there are two different costumes available and the orientation of the wings is different. Try to quickly switch between the two costumes by clicking the first costume and then the second one in succession. You will get a glimpse of how the bat should look while flying. We will now write a script for this animation.
To make the bat flap its’ wings while flying, we should be able to switch between costumes so quickly. Scratch has a block called “switch costume to” which can be used for switching costumes. The code is straight forward now:
Once we manage to touch the bat with our Aim sprite it will disappear and a new bat will spawn and we will have to aim for that bat and so on. Here’s what we want to happen next (or what should happen with every subsequent bat):
Scratch has blocks for changing sizes and random motion etc. The script for the bat is shown below:
Read through the script and you’ll see it’s quite self-explanatory.
We are now done with the bat’s activities in the game. Now let us move to another important aspect of the game, the Aim sprite.
Again, the first step will be to bring the sprite into the game. The Aim sprite is not in Scratch’s library, so we will have to import it. Download the image file for the aim sprite. When you click on the option – “Upload sprite from file” in Scratch, you will have to choose which file you want to upload, select the “aim” file and you will see that the sprite appears on the Stage. You can adjust the size (under “Looks” tab) of the sprite if it seems too large or too small to you.
To control the movement of the Aim sprite we will create two variables which keep track of its’ X-coordinate and Y-coordinate. Let’s call these variables “XPosition” and “YPosition”. Create these variables by visiting the “Data & Blocks” tab. The value stored in these variable will depend on the input we give to the joystick.
A Joystick has two potentiometers (a potentiometer is a resistor which varies its’ value upon rotating a knob, recall Mr. Ohm trying to stop flowing water in Mr. Volta and Mr. Ampere’s irrigation project), which allows it to give motion directions along X and Y axes movement made by the user and a switch which gets active when user center press on the joystick. Given below is the joystick circuit.
In Scratch, we read analog reading using “read analog value“ block available in Arduino extension.
Create two variables named “XPosition“ and “YPosition“. To create them, go to “Data & Block“ palette and select create a variable.
Write variable name and click OK. Now, you can observe new blocks in “Data & Block“ palette. Reporter blocks contain the value of the variable and other blocks are available to manipulate the variable value. Like, if you use set block, it will change the value of the variable and if you use change block it will add specified value to the variable.
Now, variables “XPosition” and “YPosition” will be changed directly according to the values read by the analog pins A0 and A1 respectively. The mean position of the potentiometer is at center hence a reading around 512 is expected at the analog pins. (the analog pins divide complete voltage range into 2^10-1 i.e. 1023 parts, starting from 0 to 1023. A value of, say 300 will mean voltage equal to 5*300/1023 Volts, mean value will be about 1023/2 i.e. ~512 ) You can refer to the script given below. Also, sign is not an issue as the resulting outputs will be either positive or negative and will hence cover whole range.
Again, all the scripts we have written till now and will write in this project will run simultaneously as long as they have the same hat block.
What’s the use of playing a game without knowing your score? It’s like having pizza without cheese, only that it wouldn’t be a pizza anymore. Let us then write a script which keeps track of score. We will write this script for the bat sprite so switch to the bat sprite by clicking on it. The logic is simple: As soon as the Aim sprite touches the bat, the player would have scored a point.
There is a “touching” block under the “Sensing” in Scratch that checks if the sprite you are writing the script for touches a particular element of the Stage. Select “aim” from the “touching” block’s drop down. Now this block will check if the bat has touched the Aim sprite or not. Create a variable “Score” to store the score.
This code will serve two purposes:
Also, in the “Data&Blocks” tab, you will see the variable list, check (tick) the small box beside “Score” and uncheck the boxes beside “XPosition” and “YPosition”. The variables you have ticked will have their values displayed on the top left corner of the Stage. We would like to display the Score but it certainly would not be convenient to display “XPosition” and “YPosition”. This is a great feature if you want to debug your code.
We will give the player a limited amount of time to score as high as possible. We need a timer for that. It’ll count how many seconds are remaining on the game. Create a variable “Timer” and check the box in front of it (to display time remaining to the user). Every second the time remaining should decrease by 1.
You see that initially, the Timer has been set to 60, i.e. we are giving the player 60 seconds. We will now decrement the time remaining by 1 every second. Therefore, we will wait for one second before decreasing it by 1. Also, we want all activity to stop once the Timer reaches zero, i.e. no time is remaining and the game is over. Hence, we will repeat this loop till the Timer reaches zero (use the “repeat until” block). Once that loop exits we will stop all activity using the “stop” block.
Your game is ready now 🙂
Try adding sounds and effects to the game every time a bat is correctly aimed at. Try Displaying “Game Over” on a black screen when the Timer reaches zero, the possibilities and modifications are infinite and restricted only by your imagination and creativity.
The diagram shows how to interface a joystick with evive.
This project will show you how to make a DIY automatic railway crossing.
This project will show you how to make a DIY Smart Trash Bin.
This project will show you how to make a unique DIY Christmas Tree for this year’s Christmas.
0 items in the cart ($0.00)