Part 2: Creating Player1 in ‘Alien Attack’ on Scratch

Alien Attack - TechWise Academy

In Part 1: Create Your Own ‘Alien Attack’ Game on Scratch, we covered how to set up your game with all the necessary players, sprites, and variables for our new game. This next part of the series covers how to move around Player1 and shoot a laser across the screen.

This part of the tutorial assumes you’ve completed Part 1 and you know how to move blocks into your workspace and connect commands. If you’re new to Scratch or this game, I recommend you start in Part 1 in the link above.

Moving Player1 on Screen

We want to move Player1 around to shoot and dodge the aliens. This is done through Events when certain keys are pressed. These are some of the simpler commands and actions for this game. Once you get one, you’ll get the rest in no time.

Select the Player1 sprite (your spaceship) for these movement controls.

Alien Attack Player 1 MovementWe will show you the first command and challenge you to do the next steps.

When Up Arrow Key is Pressed

  1. Go to Events and drag when space key pressed to your workspace
  2. Go to Motion and drag change y by 10 below your new event block
  3. Change space to up arrow to read when up arrow key pressed

Your final block should read when up arrow key pressed – change y by 10.

Changing Y

Imagine Y as up and down. A positive number means you’re going up, and a negative number means you’re going down. So changing by 10 will move you up, and changing Y by -10 will move you down. Try this in your remaining movements.

Challenge: Set up your blocks so Player1 moves down when you press the down arrow. Extra Credit: What if you do the same for pressing w and s for the traditional wasd keyboard commands for PC games?

Make Your Laser Move

We want the laser to move across the screen. There are no aliens to shoot yet, but we want to make sure the laser moves across the screen when we press a button (more on that next).

Select the Laser sprite for this next block. At this point, your Scripts tab should be empty.

Setup: Hide the Laser at Start of Game

We only want the Laser sprite to appear when we press a button. At the start of the game, we will hide the Laser at Player1. 

  1. Go to Events and drag when flag clicked to our workspace
  2. Go to Motion and drag go to mouse-pointer below your new event block
  3. Change mouse-pointer to Player1
  4. Go to Looks and drag hide below your last block

Cloning: One Sprite, Many Lasers

We don’t know how many times Player1 will shoot a laser. Scratch gives us the ability to create clones which makes copies of the sprite for the game. When Player1 presses a button, we want a clone of the laser to be created and shoot across the screen. The next block is about creating clones.

Alien Attack: Laser Sprite Movement

Click to enlarge

Select the Laser sprite for this next block.

  1. Go to Control and drag the when I start as a clone block to your workspace
  2. Go to Motion and drag go to mouse-pointer below your new event block
  3. Change mouse-pointer to Player1
  4. Go to Motion and drag set x to 0 below your last block
  5. Change the value of x from 0 to -160
  6. Go to Looks and drag show below your last block

We’re not finished! This will only make your laser appear. We need it to move across the screen.

Complete the following steps in the same block:

  1. Go to Control and drag repeat until below your last block (this is a loop – very important!)
  2. Go to Sensing and drag touching ? within your repeat until block (see picture)
  3. Change value of touching to edge
  4. Go to Motion and drag change x by 10 and place inside the repeat until loop
  5. Go to Control and drag delete this clone to the very end of our blocks

Now our laser will clone, move across the screen, and delete the clone when it touches the edge of the screen. This is a good programming practice to delete or remove sprites/objects when we’re done with them when your code gets more complex.

Shooting Lasers with Player1

We’re almost done! We want to shoot lasers when we press the space bar. The laser knows what to do when we call the clone, but the game doesn’t know when to make the clone. That’s what we’ll do here.

Alien Attack Player1 Shoots LaserSelect the Player1 sprite (your spaceship) for these controls.

  1. Go to Events and drag when space key pressed to your workspace (start a new block set)
  2. Go to Control and drag create clone of myself under your last block
  3. Change myself to laser to read create clone of laser

Now you’re able to shoot lasers! Start your game by pressing the green flag and watch what happens.

What happened? You’re able to spam the lasers by holding the space key. What we want to do is make the game wait just a little bit to make it a little harder.

  1. Go to Control and drag wait 1 secs below the when space key pressed block
  2. Change 1 to 0.1 secs

Your new control should be placed between your space key and clone block. This forces the game to wait a little bit. You can make this longer.

Challenge: Play a sound when you create the clone. Where would you add it?

Click the green flag above your game and move around and shoot lasers. Good ol’ fashioned gaming!

In this series:

See the Full Game