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

Scratch Tips: Randomly Animating a Sprite

Scratch is a powerful tool that allows your student to fully explore programming logic.  Where other tools guide you along the way, Scratch gives creative freedom.  We typically use this tool during the second half of our sessions to reinforce concepts and to see what the kids come up with.

This short tutorial will demonstrate how to randomly animate a sprite.  It can be used to create enemies, obstacles, or just background graphics for your Scratch experience.

What is a Sprite?

First off, a ‘sprite’ is Scratch’s terminology for any object in which you can apply programming logic. They are located in the bottom middle portion of your Scratch interface.  Typically students really enjoy adding new sprites as it allows them to choose their own characters and objects.

ScratchTip1

For this example, we’ve created a dog sprite that our cat will need to avoid.   The dog will randomly move around the screen making it difficult for our cat to reach the ball.

Step 1: Move the Dog

Since the dog will move as soon as we start we want to use the ‘when clicked’ event from our control blocks.  This tells us that when we click the flag above our animation, execute the code immediately under this block.  Drag the ‘when clicked’ block to your workspace on the right.

ScratchTip2

Step 2: Move the Dog “Forever”

Now we want to tell the code that the dog should move forever.  We don’t want him to stop until we stop the game.  Drag the ‘forever’ block from the ‘Control’ section immediately under ‘when clicked’ in your workspace.  It should look like:

ScratchTip3

What do we want the dog to do forever?  Move around, right?  There’s a really cool block in the ‘Motion’ section called glide.  It allows you to specify how many seconds and to where your sprite should glide.  Drag the glide block inside the forever block in your workspace.

ScratchTip4

Step 3: Animate

That’s great but we’re not quite finished yet.  Why doesn’t this achieve our goal?  Well, we’re telling our sprite to glide for 1 second to a specific x,y location.  Once they get there they’ll just stay (at -94x, 40y in this example).  We want to make the movements completely random in this example.

Under ‘Operators’ you’ll find a ‘pick random 1 to 10’ block.  You can use this to create the desired effect.  We’ll need three random generators: 1) number of seconds; 2) x coordinate; and 3) y coordinate.

Number of seconds is easy.  We’ll use the default 1 to 10 numbers in the random generator.  Drag this block into the white area around the number ‘1’ in the glide block in our workspace.  It should look like:

ScratchTip5

For the x- and y-coordinates, we’ll have to modify the numbers in the random generator.  Our x-axis in Scratch runs from -240 to 240.  That’s what we’ll put in and then drag into the x: area of the glide statement.  Our y-axis runs from -180 to 180.  Use the same process for it.  Your finished workspace should look like:

ScratchTip6

You can click the start flag at the top and your sprite should be randomly moving around the screen at different speeds.

Try it Out

The full example can be viewed online here:  https://scratch.mit.edu/projects/84587392/

Click ‘See Inside’ to see all of the code.

Have fun!

minecraft

Minecraft Modding – Setting Up Your Environment

We’re excited to offer a course teaching young people how to create Minecraft mods in November.  This class introduces high schoolers to Java and how to use it to modify the popular game.

This post serves as a guide to help get started and is a little longer than our normal posts.  I couldn’t find a single source of content so this is also a compilation of several sites that I used for research.

What is a “mod”?

Simply put, it’s a modification of the underlying code that runs the Minecraft game on your PC.  It allows developers to create new items, blocks, characters, etc. inside the Minecraft environment.  It is also perfectly legal and encouraged by Mojang (the game developers) in their licensing agreement:  “If you’ve bought the Game, you may play around with it and modify it.”

My Environment

Windows 8.1 64 bit; Minecraft 1.8.8

There are four steps which I’ll describe in sections below:

  1. Install and set up Java
  2. Install Eclipse
  3. Install and configure Minecraft Forge
  4. Configure Eclipse environment

Install and set up Java

Java is the programming language that we’ll use to make modifications to Minecraft.  This section is probably the trickiest if you’ve not used Java before.

Download the Java Development Kit (JDK).  Note that you need the development kit and not the Runtime Environment (JRE).  This will give you what you need to not only run Java programs but to also be able to compile Java code on your machine.  You also need to make sure you select the correct version for your computer.

Click on the downloaded .exe file and your new code should be installed somewhere similar to: C:\Program Files\Java\jdk1.7.0_79

You’ll now need to setup your system’s environment variables.  On Windows (in your Metro interface, not desktop mode), click on the Search bar and type ‘Edit the System’ and the following will appear:

edit the system environment variables

Click on the ‘Edit the system environment variables’ link.  This window will pop up and you should click the ‘Environment Variables…’ button at the bottom.

system properties

There are two settings that you need to update/edit.

First, JAVA_HOME.  It’s Value should match the install folder that was created above when you clicked the java .exe.
edit the JAVA_HOME system variable

Second, Path.  It’s Value should have a lot of values already setup.  At the very end (after the semicolon) enter the above path followed by \bin;
edit the path system variable

Now your machine is setup to compile Java code.  (Fun, right?)

Install Eclipse

Eclipse serves as the Development Environment for your Java code.  It provides built-in features that make programming much easier.  Download Eclipse (select your appropriate environment on the upper right-hand area of the screen).

The downloaded file will be a .zip file.  Once downloaded, you should right-click on that file and select ‘Extract All…’.  This will open the .zip file and dump its contents into the folder that you choose.  Mine looks like this:

extract compressed files

Eclipse is now installed on your machine.

Install and configure Minecraft Forge

“Minecraft Forge is a modding API (Application Programming Interface), which makes it easier to create mods, and also make sure mods are compatible with each other.”  It helps us create mods better and faster.  I spent a lot of time finding the correct files to download so my time spent is time saved for you.  There are two methods for setting up Forge:  1)  For running mods that other people have created; and 2) being able to create and compile your own mods.  Here’s the download link.  Find the ‘Download Recommended’ area on the right and click on the ‘Src’ button.

minecraft forge download

Be careful, the next screen will display a “Download” button in the middle of the screen.  It’s actually an ad and will download adware to your computer.
 Wait 5 seconds and a ‘Skip’ button will be displayed at the top right.  Again, this will be downloaded as a .zip file.

Right-click on the .zip file and ‘Extract All…’ again.  My folder is C:\MinecraftModding\Forge.  Once this is finished extracting, right-click in the MinecraftModding folder and click New->Folder and call it ‘workspace’ (this is for our Eclipse setup later).

Click on the “Forge” folder and you should see the following:

Hold down shift, and right click, then select “Open command window here”.

Once the command window opens, type the following command exactly:

gradlew setupDecompWorkspace --refresh-dependencies

This will run through several commands and will take several minutes.  I found that if I was on an unstable internet connection the process would fail (and the error wasn’t much help).  Once I got to a stable connection it ran fine.

Next, run the following command to associate Forge with Eclipse:

gradlew eclipse

Configure Eclipse Environment

Last step and you should be ready to go.  Click on the eclipse.exe file in the folder that you extracted to earlier (mine was C:\eclipse-java-mars-R-win32-x86_64\eclipse).

Eclipse will ask you to select a workspace.  I chose the folder that we created earlier in the MinecraftModding folder entitled ‘workspace’.  You can select to make this Default so that you’re not asked again but I work on other Java projects so I didn’t want to always open to Minecraft mods 🙂

eclipse setup

In Eclipse we first have to create your mod project. To do so, we click on File > New > Project and select “Java Project” in the pop-up window which will appear. Give your project a name and click Finish. Now you have a blank Java project.

At this point we have a blank Java project for our code but our project has no awareness of the Minecraft Forge code.  Click File > Import and choose “Existing Projects into workspace”. In the dialog for the import click the “Browse” button and select your C:\MinecraftModding\Forge directory as the source to import from. Then click finish. Ensure that a project named “Forge” is in the list of projects and is checked. Press ‘Finish’.   The import linked the Forge workspace to your project’s workspace so you now have access to the Forge code.

Although Forge has been linked to the workspace it can’t be used yet. Forge must be added to your project’s buildpath so that the Java compiler compiles your mod and Forge together. Right-click on your Project in the Package Explorer on the left. Then click on Build Path > Configure. Go to the tab “Projects” and click “Add”. Select Forge as the project and click OK.

Finally you have to create Run Configurations. Select your project in the Package Explorer and click on the drop-down arrow next to the green “Run” button in the top tool bar. In the drop-down menu click on “Run Configurations”. Right-click on “Java Application” and click “New”. This creates a new Run Configuration of the type “Java Application”, so it will be a program that runs on your PC.

Name the Configuration ‘Launch Client’ with the following settings:

Main Class:
GradleStart
Program Arguments:
–username=<Username> –password=<Password>

The <Username> and <Password> are for your regular Minecraft game.  Click Apply.

Now you can click on the green run button in the top of Eclipse and Minecraft should eventually start.  Now Minecraft is running as a modified version and will load any code that we create.

Hopefully this was helpful.  If you have any issues please check out some of the references that I linked below. Setting the environment up properly was much more difficult than actually programming the mods.  Stay tuned for additional posts that demonstrate the coding portion of the mods (or sign up for our class we’ll be hosting in November for 14-18 year olds).


References

http://jabelarminecraft.blogspot.com/
https://minecraft.net/
http://www.wuppy29.com/minecraft/modding-tutorials/forge-modding-1-8/
http://bedrockminer.jimdo.com/modding-tutorials/set-up-minecraft-forge/set-up-advanced-setup/
http://www.minecraftforge.net/wiki/Installation/Source