Asteroids Session 1 of 3
In this lesson, we’ll recreate the classic game of Asteroids using JavaScript. We’ll learn about functions and events, and demonstrate a real world use for trigonometry—video games!
This lesson uses the Kid.js JavaScript framework. To get started with Kid.js, create an account on kidjs.app or import the framework into your own project.
Part 1: The Spaceship 40 Minutes
Our first task will be to design the spaceship.
Functions
We program a computer by giving it a series of commands, also known as functions. To run a command or function in JavaScript, type the name of the function followed by parentheses.
Many functions need parameters. For example, if you were to command someone to walk, you may also need to tell them how many steps or which direction. The parameters go inside of the parentheses.
Let's use the triangle()
function to create the body of our
spaceship. The parameters for the triangle function are (in order) the
x coordinate, y coordinate, width and height.
Play around with different numbers and see the results.
Variables
Variables are used to keep track of things. Think of a variable as a box with a label you can put things in. There are many things we may need to keep track of in an app, such as the high score or health.
Variables are also useful for assigning names to things. Let's give our
triangle a name. This will allow us to reference it later. We use the
let
keyword to create a variable and the equals operator to
assign it a value.
Properties
Properties describe something. You have properties, such as your age and
height. Objects in JavaScript have properties too. The color
property for example.
We can set the color of the spaceship by assigning a new value to the color property.
We can also change the angle of the shape. While designing our spaceship, we're going to point the nose of the spaceship to the right. We'll do this because it corresponds to the angle zero and will simplify our code later on. Don't worry, we'll reposition the spaceship later.
Now add some wings, setting their color and angle properties to align with the spaceship body.
This is perhaps the most boring looking spaceship in history, and I'm sure you can do better. Spend some time making the spaceship your own. Explore the different shape commands. Add a cockpit, or afterburner. Remember that each piece of your spaceship must have an unique variable name.
Grouping Shapes
When finished designing your spaceship, group all the shapes
that make up your craft into a single object using the group()
command. Pass the shapes as parameters.
let spaceship = group(body, leftwing, right)
With the shapes grouped together, we can reposition the entire spaceship in the center of the screen, and point it upwards.
let spaceship = group(body, leftWing, rightWing)
spaceship.x = width / 2
spaceship.y = height / 2
spaceship.angle = -90
Part 2: Movement 20 minutes
With our spaceship complete, the next task is to make it move.
Events
Events are "things" that happen while our app is running, like a mouse click, or key press. We can write code that runs when that event occurs.
We’re going to create our own command or function that will run when a key is pressed. A function that runs when an event occurs is called an Event Handler.
The event handler for a keyboard event is passed a parameter that
contains which key was pressed. In the event handler we use an
if
statement to check if the key pressed is the left arrow.
If it is, we'll change the ship’s angle, rotating it slightly
counterclockwise.
function moveShip(key) {
if (key == 'ArrowLeft') {
spaceship.rotate(-1)
}
}
on('keydown', moveShip)
We'll rotate in the other direction for the right arrow key.
function moveShip(key) {
if (key == 'ArrowLeft') {
spaceship.rotate(-1)
}
if (key == 'ArrowRight') {
spaceship.rotate(1)
}
}
on('keydown', moveShip)
And move the ship forward if the up arrow is pressed.
function moveShip(key) {
if (key == 'ArrowLeft') {
spaceship.rotate(-1)
}
if (key == 'ArrowRight') {
spaceship.rotate(1)
}
if (key == 'ArrowUp') {
spaceship.forward(1)
}
}
on('keydown', moveShip)
Let's take a look at the complete project so far.