Table of Contents

Dodge

Dodge Scratch Game Cover Image
Project Description

Introduction

The aim of the game is to control the kid and dodge the objects coming his way for as long as possible. You can’t dodge them forever; because basically we are playing a game of survival and the challenge essentially is to stay in the game for as long as possible. Let’s begin by explaining the rules of the game through some figures.

  • When there are no obstacles
    When there are no obstacles
  • The ball will come bouncing towards our “protagonist”
    The ball will come bouncing towards our “protagonist”
  • This isn’t a real plane. And yes, we are assuming that planes can fly at such low altitudes.
  • The ball will bounce and come to you but it’ll be grounded in your vicinity to give you a fair chance at dodging it. Dodge the ball by jumping.
  • The plane will fly straight at you. Dodge it by sliding under it.
    The aeroplane will fly straight at you. Dodge it by sliding under it
  • If you fail to dodge an obstacle, our sprite returns a message confirming the same.
  • It’s GAME OVER once an object hits you.

Lets make the game 

A quick recap about Scratch

  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:

  • Arduino Mode: Under Arduino mode, the instruction blocks will be tranfer to Arduino C++ into Arduino IDe, and user could modify code in Arduino IDe, and then upload the code into Robot. In that case, the Robot is running offline and it can not interact with Scratch’s stage.
  • Scratch Mode: Under Scratch mode which is defualt mode when mBlock start, mBlock could program robot through USB serial port, the robot is running a program could communicate with mBlock, and robot could intercate with Stage and create more insteresting projects and animations.

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.

The Running Animation

You should be familiar with designing and importing backdrops and sprites while making Shoot the Bat game. If not, go through the game or few tutorials like:

The sprite for the kid is not available in the library so import it. Download them from here. The basketball, the school backdrop and the plane sprite are present in the library. Change the school backdrop name to “School”.

Select the sprite and scripts tab. He is running when he isn’t jumping or sliding. Also, we have created a bird flapping animation before (in the bat game). We know that all we have to do is to just switch between costumes as fast as possible. The various costumes for running are shown in sequence:

running

The Running script can easily be created.

Running

The X and Y coordinates can be determined by dragging the sprite and dropping it where you require it to be. The glide block automatically reads those coordinates.
Be careful not to place your sprite too much on the lower side or the upper half of the Stage. An ideal position will be the greyest area on the road.

 

running

Jumping Animation

Let us now write scripts which shall help the boy jump and slide. The costumes for jumping are given below. The 3 costumes in the middle represent time when the boy is in the air while the first and last represent the start and the end of the jump respectively. The 2nd and 4th costumes represent the times it is about to take off and touchdown respectively. While the 3rd costume represents the highest points of the jump.

Jumping

Observe that, and this is important, the sprite should remain in air for more time than on ground because we are jumping.

Jumping

Another thing worth noting is that when you switch costumes, your Y-coordinate should increase when you switch from 1->2 and 2->3 with the Y-coordinate for the 3rd costume having the highest Y-coordinate (height). To get an idea (of the Y-coordinate), bring your ball sprite near the boy and drag around to see which height is suitable. Make sure that the height is enough so as the boy may be able to go over the ball.

Jumping

Sliding Animation

The subsequent images show the stages of sliding. Again, one has to carefully manipulate the time and height on each costume so it may be possible to slide under the plane.

sliding

Notice the increased time (in seconds) the sprite spends in costumes 3, 4 and 5, where the boy looks as if he is sliding.

Sliding

 

It’s OK if you are not able to calibrate the time the sprite spends in each costume as of now. The required time can be adjusted once you run all the scripts and you can actually get an estimate as to how long it will take you to jump over the ball or slide under the plane.

Running, Jumping and Sliding simultaneously

We will jump and slide to avoid obstacles and run at all other times. What we need for this purpose is for the three scripts: “Running, Jumping, And Sliding” to interact with each other. We will make changes in the Running script in such a way that the Jumping and Sliding scripts “know” when to activate. The Jumping and Sliding scripts start off with the “When I receive” block. We will check if the tactile switch 1 or the tactile switch 2 of evive is pressed and then jump or slide accordingly.

evive Tactile Switch Pushbuttons

We know what to do. The question is: How to do it? Worry not, for Scratch has the required blocks.

  1. When our sprite is in any of the Running costumes, we can press the tactile switches to make it jump or slide respectively.
  2. In the Robot palette, there is the tactile switch pressed block under evive inbuilt functions extension.
  3. We know we have to jump; we should activate the Jumping script now. For this use the if block and then broadcast Jump using the broadcast and wait block.
    tactile pressed
  4. Drag and drop the hat block When I receive in Events palette. Change the message name to “Check User Input” and snap the above code below it.
  5. Where to send the message to check user input? Yeah you guessed it right, as soon as you switch costumes in the Running script. The complete running script is shown below. You can observe that you are broadcasting the message after changing costumes. This is done to reduce the lag while playing. You can try the other one, and experience it yourself. Also, to keep track of your performance, create a variable “Score”. At the start of the game Score should be 0.
  6. Try running this and see if the sprite is jumping every time. Similarly, create blocks for sliding. The complete Check user input script is shown:
    tactile pressedschool backdropschool backdrop

 

Test whatever you have written until now. Run the scripts and see if the sprite (boy) performs the required actions. This way, debugging is easy. If you write the whole code at once and your program misbehaves, it is very difficult to identify where the mistake may be.

Spawning the ball and the plane randomly

It’s time for the obstacles now. We are not yet programming our plane and ball. Instead, we are just deciding at what times they will appear on stage. We will pick a random time interval at which the plane and ball appear. See the script given below:

We first wait for a random time period from 1 to 5 seconds. Then, we want any of the ball or the plane to appear so we choose randomly between the two.

Depending on what is broadcasted (chosen), the respective scripts under the ball and the plane sprite get activated. We will now write the scripts for the ball and the plane.

Plane Script

Starting from the right edge, keep the Y-coordinate constant and glide the plane to the same position at the other end. Remember the plane has to disappear at the end. Hence we hide it at the end of the script. Also after going to the initial position, we show the plane. Every time the plane successfully passes the boy, Score should increase by 1. We are executing this using Change block in the variable palette.

Script for Plane

Plane Gliding

Ball Script

Next, we write a script for the ball. Start the ball from (240,0). Don’t get confused; ground level (where the boy’s feet are) is at -120 (Y-coordinate).

Because the ball has to bounce on its’ way before settling down, the code is a bit complicated. We create variables X, Y and XSpeed, YSpeed to keep track of position and speeds (in horizontal and vertical directions) respectively.

  1. We need the ball to reach the kid, so we keep a constant speed in the x-direction, say 30. Because the speed is towards left, we use a negative sign.
  2. The following steps will be repeated till the X-coordinate is less than -240, i.e. leftmost edge:
  3. By a speed of -30 we mean the ball will move 30 units to the left every second. So we change the X-coordinate by -30 every time.
  4. We want the ball to fall down from height initially but bounce back once it touches ground. However, we do not want it to rise to the same height, i.e. the ball’s vertical (Y) speed should decrease once it bounces.
  5. For the ball to fall down we should give it speed in the negative Y direction (downward) and change the Y coordinate accordingly.
  6. However, we do not want the same to happen once the ball touches the ground, i.e. Y= -120. We give it an upward speed lesser than the speed it fell with (hence the division by 1.4).
  7. Just to make sure that the ball doesn’t fall below -120, we again set its’ Y coordinate to -120 as soon as that happens.
  8. Every time the ball successfully passes the boy, Score should increase by 1.
  9. We also want to hide the ball at the end of the script and show it at the starting of the script.

Ball Script

Ball

Game Over

In this section, we will control what happens when the game is over, i.e. either the plane or the ball has struck the boy. When the game ends, we want these things to happen:

  1. The boy displays a message saying he is hurt
  2. All motion on the school backdrop stops.
  3. The backdrop switches to GAME OVER

Script for the Boy

We will check if the boy touches the plane or the ball. After that, we will broadcast GAME OVER throughout the scripts. This will notify the other sprites in the game to execute their respective “GAME OVER” scripts. We will also make the kid say – “Ouch! I’m hurt”, hide the sprite and switch backdrops.

We use “stop other scripts in sprite” to avoid glitches. This option is available in the drop down of the stop block.

Touched by Plane Touched by Ball

The ball and the plane

As soon as the ball receives GAME OVER, it stops other scripts, hides the ball and sends it to its’ starting point: (240, 0).

The same script is written for the plane too, except that its’ starting point is different from that of the ball.

When Game Over

When Game Over2

Also, when you start the game, the sprites, the ball and the plane, should go to their initial position and should be invisible. Given below is the script for the same.

end script2 end script1
And the game is complete and ready for playing. You can adjust the speed of the moving objects to make the game easy or hard.

Project Downloads - Code and Files

Circuit Diagram

Circuit Diagram DescriptionCircuit Diagram Fritzing Diagram

Only evive is required in this project

Explore Related Projects by STEMpedia

Level up your robot control skills by learning how to control a 2-Wheel Drive Robot with hand gestures using PictoBlox AI's Machine Learning extension. Get ready to channel your inner Jean Grey!
Learn how to make a two-wheel-drive robot and code on the go with PictoBlox's mobile app. Control your robot in real-time with your smartphone!
Learn how to make an automatic chocolate dispenser to delight Santa this Christmas. All components can be found in the evive Starter Kit. Let's begin!

Explore Community Projects