Add POWER-UP Code-Along
Once you have customized the game, it's time to add a new feature: a POWER-UP. For this example, we will make the player run faster when they eat the POWER-UP.
Adding a "Consumable"
The game is pre-built to allow for "Consumables" to be added to the map. "Consumables" (as defined in the zobject.consumable.js file) are designed to disappear and cause some effect when the player touches them.
- Open the script.js file
- Put your cursor on line
8(way down at the bottom)- We will be adding more lines of code above this line
- Add this line of code to create a new
Consumableobject:game.addConsumable(); - Save the project
- Look at the game preview
- You should notice a small white ball!
- Eat the ball
- You should notice the ball disappears!
So we have a consumable! But we can change quite a few things to make sure it makes sense in the game.
Changing the North Position
We can set the north position of our new consumable. We will do this using variables that are passed into addConsumable!
First, make the new variable north:
- Put your cursor at the top of the script.js file (above the
game.addConsumable()line) - Start a new variable definition by typing
let - Then, type a variable name:
north - After that, add an equals sign
= - Next, add a number for the
northposition:2 - At the end of the line, add a semi-colon
;
Next, pass the variable to addConsumable:
- Find the
game.addConsumableline - Put your cursor right between the parentheses (
(and)) - Type the variable name,
north - Save the project
- Look at the preview
- Notice the little ball should have moved!
The code in the script.js file should look something like this:
let north = 2;
game.addConsumable(north);
Changing the East and Vertical Position
To change the east and vertical positions of the POWER-UP, follow very similar steps:
- Create the new variables (
eastandvertical) - Set their values to things that make sense (like
10and4) - In the
game.addConsumableline, addeastandvertical, all separated by commas - Save the project
- Look at the preview
- Notice the little ball should have moved!
At this point, the code in the script.js file should look something like this:
let north = 2;
let east = 10;
let vertical = 4;
game.addConsumable(north, east, vertical);
Making the POWER-UP Actually Power Up
Now for the fun and difficult part - making the POWER-UP actually functional! To do this, we will actually be defining a function in JavaScript. A function is a piece of code that does something on command. Just like with the previous variables, we will be making a new variable to store this command, and then we will pass it to the game.addConsumable line.
VARIABLE SETUP
First, get the variable setup:
- Make a new line in the script.js file, still above the
game.addConsumableline - Create a new variable named
eatPowerUpletkeyword- Variable name (
eatPowerUp) - Equals sign (
=) - Value (?????????)
let eatPowerUp =
FUNCTION SKELETON
Now, it's time to define the function skeleton:
- After the equals sign, instead of a number, type the word
function - After the word
function, add left and right parentheses:() - In between the parentheses, add left and right curly brackets:
{} - Put your cursor between the curly brackets and press Enter to make a new line
let eatPowerUp = function() {
}
Whatever code is between those curly brackets will run whenever the player consumes the POWER-UP!
FUNCTION FUNCTIONALITY
We can set one of our variables in there to change the gameplay. Setting a variable is a lot like defining one, but we don't need the let keyword.
Let's set our PLAYER_SPEED to 20:
let eatPowerUp = function() {
PLAYER_SPEED = 20;
}
FUNCTION PASSING
Finally, we need to pass the new eatPowerUp function to our game.addConsumable.
- Find the
game.addConsumableline - Put your cursor in between the parentheses, right at the end (after
vertical, before)) - Add a comma
,and the function nameeatPowerUp
game.addConsumable(north, east, vertical, eatPowerUp);
Save the project, eat the POWER-UP, and see what happens!
CODE NOW
The code in the script.js file should now look something like this:
let north = 2;
let east = 10;
let vertical = 4;
let eatPowerUp = function() {
PLAYER_SPEED = 20;
}
game.addConsumable(north, east, vertical, eatPowerUp);
Changing Size, Color, and Shape
The last thing to do is customize our POWER-UP with its own size, color, and shape. Setting these POWER-UP properties will be very similar to setting north, east, and vertical. It is very important that they are added in the proper order, AND have the proper formatting (watch commas , and quotation marks "").
- Above the
game.addConsumableline, define thesizevariable, and set it to10 - Under that, define the
colorvariable and set it to"cyan"- Note the
""quotation marks!
- Note the
- Under that, define the
shapevariable and set it to"box"- The only options now are
"sphere","box", and"torus"
- The only options now are
- Add
size,color, andshapebetween the parentheses on thegame.addConsumableline - Save the project
- Look at the preview
- See the new size, shape, and color!
Conclusion
At the end of this code-along, the code in the script.js file should look something like this:
let north = 2;
let east = 10;
let vertical = 4;
let eatPowerUp = function() {
PLAYER_SPEED = 20;
}
let size = 10;
let color = "cyan";
let shape = "box";
game.addConsumable(north, east, vertical, eatPowerUp, size, color, shape);
Next Steps
There are so many additional things you can try to make your game even better!
- Add a new power-up, or change the current one to do something interesting
- Add more platforms, coins, or enemies
- Continue changing the variables
The possibilities are endless!