Learn how to make a simple Scratch video game.
This is one of my many free game projects. I will show you step by step how to create an awesome basketball throwing game. The ball randomly spawns on the left side of the screen, and your aim is to get it through the hoop. To make it diagonally up to the right side, click on the first quadrant of the stage, just where the ‘click here’ logo appears. The play is going to have a time limit of 30 seconds, and each successful throw in will count as 1 point. Try to score as many points as possible. Lets get into the programming this Scratch video game!
Programming the ball sprite.
As always, start with a completely blank project. I start with drawing a simple background with a ground. On the left side I draw a frame that contains the ‘time’ and ‘score’ words. Right next to them I will place the variables that actually count them. The next thing I do is import the basketball sprite from the library, and I start writing a script for it. First, I make it bounce of the edge when it touches it, to make the game more collected. Then I pick its spawning point on the stage. The y coordinate is a fixed -125, and the x coordinate, left and right, is a random point in the range of -235 and -133. Next, for this video game, I create a condition that says, if the mouse is down (or simply pressed), and if the ball is touching the brown color (which is ground) then:
1. Set the gravity to 15. Make a variable, and name it gravity. This will be used for another script for this sprite.
2. Change the y position by that of the mouse y-coordinate. The ‘mouse y’ block is from the sensing category and it reports the y-coordinate of my mouse.
3. The script waits 1.85 second while the ball is flying in the air, and it goes back to its approximate starting point.
Place all this into the forever loop.
Scratch video game ball projectile.
Now, to make the ball actually project itself forward, I make another script with a condition that states – if the sprite is not touching the brown color (ground), then change its x value by the square root of the mouse x-coordinate value. And right under it, place the ‘change y by’ motion block and inside it place the actual gravity variable block from the data category. To make it obey the video game gravity, insert the ‘change gravity by -1’ block under it. Put all this in a forever block to make it constantly run. What this means is that when the ball is in the air it will change its x-coordinate by that of the mouse one, but square, and the y-coordinate will be changed by -1, bringing it down. Next I create two collision functions that make the ball bounce to different points depending on which color part of the hoop it touches. I will draw it later. I use the ‘change x by’ motion blocks for x-axis, and for y-axis I simply adjust a different value for the ‘gravity variable, which directly acts upon the y-value of the ball. It is not the best one I could make, but it is a decent enough for a Scratch video game. The fourth script you see in the picture is created for counting points. It is a score variable that resets to 0 when the game starts, and when the ball is touching the ‘goal’ sprite, which I will create later in this tutorial, the score will change by 1. Don’t forget to come back here and choose the ‘goal’ sprite in the drop-down menu.
Video game hoop.
It is time to paint a new sprite called ‘hoop’. The collision colors I set for the basketball are the colors for this hoop. The left part of the hoop is black, and the right part is dark green. Adjust it on the right side of the screen. While I am at this sprite, I script a timer for the game. Simply create a new variable, and set it to be equal to 30 when the game starts. Then inside a forever loop add one ‘wait 1 second’ block and keep changing the timer by -1. And finally, if the timer value is equal to 0, then stop everything, as this is the end of the game. Now I paint an eclipse sprite that I name ‘goal’. Place at the bottom of the hoop. Go back to the basketball sprite and choose the ‘goal’ sprite in the score script. Whenever the ball touches this sprite, the score will increase to one. The ‘click here’ sprite is a simple indication where the user should click on the screen to make the ball move diagonally up to right. It is the first quadrant, and that is where the x and y values for the mouse are positive. The final part for the Scratch video game is to adjust the variable boxes on the screen where appropriate. The score variable next to the score name, and the timer next to the time. The gravity variable can be hidden from the stage by un-checking its box in the data category. That is if for this Scratch video game.