## Scratch GameBeginner

In this lesson you will program a game called Paddle Ball! You will learn about moving sprites, backdrops and using sensing blocks.

1

#### Create a new Scratch project

Create a new Scratch project and delete the cat sprite that's added by default.

2

Drag the paddle to near the bottom of the stage area (by holding down the left mouse button and moving your mouse). Make sure to leave some space between the bottom of the stage area and the paddle (this is important later on as we'll be putting something there).

3

Add the sprite called 'Soccer Ball' from the sprite library.

4

#### Position the ball

At the start of the game we want the ball to start at the top center of the screen. We can do this by setting the X and Y coordinates of the ball.

The X and Y coordinates are like an address, they give the location of a point on the screen. The X number is the point on the X axis which runs vertically across the screen (left and right). The Y number is the point on the Y axis which runs horizontally across the screen (up and down). The very center of the screen is X:0 Y:0. As you move to the right the X number increases, as you move to the left the X number decreases. Similarly as you move up the Y number increases, as you move down the Y number decreases.

We want the ball to start at the top center of the screen which as at the location X:0 Y:170. To do this we add the following code to the ball sprite:

``` when green flag clicked go to x [0] y [170] ```

Once you've added this code and entered the correct X and Y coordinates, click on the green flag at the top right of your screen to run the code. Your ball should go to the top center of your screen.

5

#### Make the ball bounce around

Now that we have the ball in the correct position for the start of the game, we want to make the ball start moving around the screen and bouncing of the edge of the screen when it touches it.

First we point the ball in a downwards diagonal direction and then we get it to continually move, if it touches the edge of the screen we want it to bounce. Add the following code underneath the go to x: 0 y: 170 block:

``` when green flag clicked go to x [0] y [170] // insert new code under here point in direction [120] forever move [10] steps if on edge bounce end ```

Run your code by clicking the green flag. The ball should start bouncing around the screen!

6

Next we want to be able move the paddle left and right by controlling it with our mouse.  We'll do this by getting the paddle to sense where our mouse is on the screen and setting the X coordinate (the coordinate that controls the left and right position) to be in the same position as your mouse.

First click on the paddle in the sprite list so that you can give it some code and then add the following code to the paddle sprite:

``` when green flag clicked forever set x to (mouse x) end ```

We use the forever block as we want this to always happen. The mouse x block senses the X position of your mouse. Click the green flag and move your mouse around the screen, the paddle should copy your mouse by moving left and right!

7

#### Make the ball bounce off the paddle

Now that we can control our paddle, we want to program the ball to bounce off it. We'll do this by sensing if the ball touches the paddle and if it does then we'll point it in the opposite direction (making it bounce).

First click on the ball in the sprite list so that we see the code for the ball. Then add this code to the ball sprite:

``` when green flag clicked forever if < touching [paddle v] > then turn ccw [180] degrees move [10] steps end end ```

Now when you click the green flag again the ball should bounce when you move the paddle under it.

8

Open the backdrop library and choose the 'Stars' backdrop for your game!

9

#### Add the game over line

At the moment there's no consequences for failing to keep the ball bouncing so we want to add a line below the paddle that if the ball touches, it'll be game over. This is why we left a small gap between the paddle and the bottom of the screen!

Select the backdrop on the bottom right of the screen and then click on the 'Backdrops' tab. This will open the Backdrop Editor which we're going to use to draw a red line at the bottom of the screen under the paddle. Then we're going to add some code that makes the game over if the ball touches the red line.

1. Choose the colour red from the 'Fill' box
2. Select the line tool
3. Position your mouse at the bottom left of the backdrop
4. Hold down your left mouse button and move your mouse to the bottom right hand side of the screen
5. Release your left mouse button

10

#### Program game over

Now we need to detect if the ball touches our red line and if so, game over!

Click on the ball in our sprite list (you may also need to click on the 'Scripts' tab above the block palette if the editor is still showing). Add the following code underneath the if then statement for touching the paddle:

``` when green flag clicked forever if < touching [paddle v] > then // insert new code under here turn ccw [180] degrees move [10] steps end if < touching color [#ff6666]? > then stop [all v] end end ```

Now you've finished all the code for the Paddle Ball game, have fun playing it!

11

#### Improve the game?

How could we improve the game and make it better by adding to or changing the code? Have you any ideas?

End of lesson