Creating a game - Cannon and Balls



Let us create our first projectile launching game in Scratch. Don't be scared,, We will be up and shooting our balls from our cannon within a few minutes.


Try your hand at shooting by clicking on the above image. The cannon changes it's angle by 5 degrees when you press either Left or the Right arrow keys. Spacebar launches a ball in the direction the cannon is facing. There is a ball receiver that appears at random locations which will collect the balls that you fire. There are two black holes that will make your balls disappear before they can reach the ball receiver. You need to shoot and collect 20 balls into the receiver in 30 seconds to win this game. This game requires concentration and a quick hand eye co-ordination.

There are 7 sprites in this game. The cannon, two black holes, ball receiver, and two sprites to display messages at the end of the game. I have drawn the cannon in the sprites editor using the ellipse and line tools. the initial position of the cannon should be facing right and the center of the sprite is in the center of the cannon's bulge. The ball receiver is drawn using squares tool. Two black holes are just two circles filled in black color. The ball sprite is taken from the sprites library.

If you have acquainted yourselves with the "see inside" button on the project's homepage, it should not be difficult for you to read and understand the code used with each sprite.

You can see the project's homepage on this link

https://scratch.mit.edu/projects/108735443/

Variables used in this game

A single variable used in this game is "Balls caught". It counts the number of balls touching the ball receiver sprite. When the number of balls touching the ball receiver becomes 20, it sends a message "End Win". If the time exceeds 30 seconds then it sends a message "Time over". There are two separate blank sprites used to display messages. One of them receives "End Win" message and displays a congratulation message. The other sprite receives the "Time over" and displays Time exceeded message. The game stops after each of these messages.

When space bar is pressed, a ball is launched. I have used "next costume" in the code, therefore the color of the ball changes each time for 5 iterations. These are the 5 different colored costumes of the ball sprite. You can keep the ball of the same color each time if you want.

There is an extra animation effect that is not essential in the running of the game. If the ball touches the black holes then I have used "change size by -10" repeated 10 times for the ball sprite to create a shrinking animation. It looks good, but not essential. You can simply replace the code with "hide" if you wish so.

The random x and y co-ordinates are chosen so that the ball receiver and the black holes appear only in the upper half of the stage area.

In this game you can launch only one ball at a time. We need to make some changes to the coding to make it launch a barrage of balls. See how it works in the second version of this game below. You can see it's coding on this link

Cannon and Balls V2