Posts

Alien Attack - TechWise Academy

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

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

Alien Attack - TechWise Academy

Part 1: Create Your Own ‘Alien Attack’ Game on Scratch

In May, we introduced Alien Attack, a game we created to highlight many of the lessons we learned through the first three levels of our instructional courses. In this series, we’re going to walk through how to create the game from start to finish. Our hope is you’ll be able to take the lessons learned from creating this game and add to this game on your own or use them to create your own game.

To follow this tutorial start a brand new Scratch project. This first part only starts with the setup of the game, and you’re encouraged to check the full game at the end of the article for reference.

Setup: Backdrop and Sprites

Alien Attack: Sprites and Backdrop

Click to enlarge.

We wanted the player to drive a rocket in space and fight giant alien octopuses for this game. We needed to set a backdrop and create the necessary sprites and objects.

Stage/Backdrop

For the Stage, go to Choose backdrop from library. Select “space” from the “Space” category.

Sprites

Delete the original sprite. Then select Choose new sprite from library for each of our new sprites.

We have 3 distinct sprites or objects in this game:

  • Player1 – that’s you!
  • Laser
  • Octopus

Spaceship

  1. Select “Spaceship” from the “Space” theme.
  2. Go to Costumes at the top of your screen, and rotate the spaceship to face right and center it.
  3. Adjust the size to your liking. We made it a little smaller to fit our game.
  4. Rename the sprite to Player1.

Laser

  1. Select “Paddle” from the “Things” category.
  2. Go to Costumes at the top of your screen, and adjust the size to be a little smaller than your spaceship.
  3. The “Paddle” is green by default. We changed ours to red by using the color tools.
  4. Rename the sprite and costume to Laser.

Octopus

  1. For your enemy, select “Octopus” from the “Animals” category.
  2. Go to Costumes at the top of your screen, and adjust the size to your liking.
  3. Flip it horizontally. For simplicity, you only need to do this on the first costume called “octopus-a.” We do this so the octopus aliens will be facing your spaceship.
  4. Make sure the sprite is named Octopus for this game.

Setup: Variables

We’re going to need a lot of variables in this game. There are a lot of moving parts, and we want the game to get harder as it goes along. See the list of variables below along with why we need them.

New to variables? You can learn more about variables in our other tutorial.

Create all variables in the Data portion of your scripts. Make sure each variable has the For all sprites button checked.

  • BaseSpeed – this is the basic speed of our octopus aliens
  • HP – this is Player1’s health
  • Level – this is your current level to track your progress
  • MaxSpeed – this is the maximum speed we want our octopus aliens to travel
  • MaxTimer – this is the maximum amount of time in between each octopus alien’s spawn
  • MinSpeed – this is the minimum speed we want our octopus alients to travel
  • MinTimer – this is the minimum amount of time in between each octopus alien’s spawn
  • Player_Y_Position – this keeps track of Player1’s current y position (more on this later)
  • Score – this is Player1’s score
  • Seeker_X_Activate – later in the game we make our octopus aliens movement more difficult so this is important later

Once all variables are created, you’re ready to move on!

Setup: Starting the Game

Alien Attack: When starting the gameIn this final setup, we want certain things to happen when the game starts every time.

Before you begin, make sure the Player1 sprite is selected.

Setting Looks, Position, and Data

  1. Go to Events and drag when flag clicked to our workspace on the right
  2. Go to Looks and drag switch costume to spaceship-a to the workspace directly below the flag
  3. Go to Looks and drag switch backdrop to space to the workspace directly below your last block
  4. Go to Motion and drag set y to 0 to our workspace directly below your last block
  5. Go to Motion and drag set x to 0 to our workspace directly below your last block
  6. Change set x to -200 (this sets your spaceship in the same spot at the start of every game)
  7. Go to Data and drag set Score to 0 to the workspace directly below your last block
  8. Go to Data and drag set HP to 0 to the workspace directly below your last block
  9. Go to Data and drag set Level to 0 to the workspace directly below your last block
  10. Go to Data and drag set MinTimer to 0 to the workspace directly below your last block
  11. Go to Data and drag set MaxTimer to 0 to the workspace directly below your last block
  12. Change Score to 0
  13. Change HP to 3
  14. Change Level to 1
  15. Change MinTimer to 1
  16. Change MaxTimer to 3

Go to Data and check the boxes next to HP, Level, and Score so they’re visible at all times.

Adding Space Ripple Sound Effect

We added a space ripple sound effect to play each time the game starts to add a little flair to the game.

Make sure Player1 is still selected.

  1. Go to the Sounds tab at the top of your workspace
  2. Click Choose sound from library button (looks like a speaker)
  3. Click the Effects category
  4. Choose space ripple and click OK
  5. Go to Scripts tab at the top of your workspace
  6. Go to Sound and drag play sound space ripple to the workspace directly below your last block

Click the green flag above your game and see all the variables get set and your sound play!

Now we’re ready for part 2!

See the Full Game