Assignment 4.1: Frogger
Making a Frogger-like gameGoals
The goals of this assignment are to learn to do the following:
- Move a character using different actions
- Load a Model (in this case a box) on a scene and move it
- Use conditional statements in actions to manage collision between a box and a model
Computer Science Principles Curriculum
- Big Idea: Data and Information:EU 3.3, LO 3.3.1, EK 3.3.1G, EK 3.3.1H
- Big Idea: Programming:EU 5.1, LO 5.1.3, EK 5.1.3B, EK 5.1.3D
- Big Idea: Global Impact:EU 7.1, LO 7.1.1, EK 7.1.1C, EK 7.1.1H
Common Core Standards
- English Language Arts Standards ยป Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.8, CCSS.ELA-Literacy.RST.9-10.9
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8, CCSS.Math.Content.HSF.IF.A.1
Overview
This project requires you to download this program template for the Frogger game, which will be completed in this assignment.
In this assignment, you will learn the basics of loading models on a scene, and how you can make them move using actions. We will take the example of a Frogger-style game (if you don't know Frogger, check out this Frogger information page. In this game, you can control (move) a character in order to reach a goal. On the way, you will encounter rows of moving obstacles. If you collide with one of the obstacles, you come back to the starting point and lose a life. When you lose all your lives, it is a Game Over. In our case, one mistake will be a Game Over.
Start this assignment by opening the given project template. We will look at the code that is given, and then complete the game by: making a given model move, loading and moving more obstacles, and setting the win conditions by managing collisions.
Goal 1 : Moving a character using 4 actions
Example
To move our character, we will write actions. In the template, the action MoveForward() has already been written. Right beneath the MoveForward() action, you will find headers for the actions MoveBackward(), StrafeRight(), and StrafeLeft(). Each of these actions, including MoveForward() has one parameter: a number called speed. Let's take a look at the MoveForward() more closely:
Navigate to the MoveForward() action toward the end of the program template.
action MoveForward()
positionX = speed //By setting the X position, we move the character forward
positionY = 0
positionZ = 0
end
This action is responsible for updating 3 variables that are used to move the characters. Right now, our character can only move forward. We need to finish the remaining 3 movement actions.
Activity
Finish the 3 others actions: MoveBackward(number speed), StrafeLeft(number speed), and StrafeRight(number speed) based on the MoveForward(number speed) action. Think about which coordinates (x, y, or z) should be changed to move the character backward, to the left and to the right. Remember, we are looking down the x-axis. Imagine the character is moving in a positive direction on the x-axis as you have him move forward.
Goal 2 : Add boxes and move them
After the character, we need to add the obstacles. We will use the primitive boxes from the Model class to do it.
Example
This example comes from the program template. Right now, there is only one obstacle. The first step to load one of these box obstacles is threefold. We have to
- Instantiate a model (Our box)
- Instantiate a color object (to apply to our box)
- Instantiate an Audio3D object (the sound of our box)
// The box we are using
Model box1
// We have to choose a color for the box
Color colorBox1
// We put a sound on a model
Audio3D soundCar
Activity
To make our Frogger game better we need to add more obstacles. Following the example above, let's instantiate 3 more boxes. Don't forget to instantiate an Audio3D object and a Color for each box we add.
Example
The next step in creating an obstacle is to load the model (the box in our case). The action LoadBox() from the model class requires three parameters: the width, height, and depth of our box and the color we want our box to be.
When we load a box, we must do the following:
- Set the color of our color object
- Load a box into our model object
- Add the model to the game
- Set the position of the box
When we set the color of our color objects, we call the color action
SetColor(number redValue, number blueValue, number greenValue, number alphaValue)
SetColor(number redValue, number blueValue, number greenValue, number alphaValue)
Each parameter should be a decimal value between 0 and 1. 0 means that the color (red, blue, or green) is totally absent. 1 means the complete presence of that color. The fourth number, the alpha value, sets the color transparency, but we should leave this as 1 for this project.
Navigate to the CreateGame() action to see the example where we load our first box.
// in CreateGame
/* Position, color of a box */
colorBox1:SetColor(0, 0, 255, 1)
box1:LoadBox(2,2,2,colorBox1) // width, height, depth = 2
Add(box1) // we add it to scene
box1:SetPosition(10, -2, 0)
Activity
Following the example of our first box, load, add, and set the position of our 9 new boxes. Set the positions so that two boxes exist on the same row (horizontal line across the screen) but not the same place. This should create 5 rows of obstacles.
Example
The next step is adding the sound to our boxes. We can add the Audio3D directly to our model so that the sound will have the same position as our obstacle. When an Audio3D object is added to a model, the following steps must be done:
- Load the sound file (to stream) into the Audio3D object
- Set the volume
- Tell the sound to begin playing
- Enable Looping
- Add the sound to our model
In the CreateGame action, find the code below. This is our example which adds our first Audio3D object to our first model.
// in CreateGame
// Enable the audio file and attach it to the box
soundCar:LoadToStream("Sound/carSound.wav")
soundCar:SetVolume(0.3)
soundCar:Play()
soundCar:EnableLooping()
box1:Add(soundCar)
Activity
Following the example above, add one Audio3D object to each of the boxes we have created.
Example
The last thing we need to do for our obstacles is make them move. This is done in the Update(number seconds) action. When a box hits one wall, it should be moved back to the starting position (z-value 10 or -10).
Navigate to the Update action and let's look at the code to move our obstacle across the screen.
// every update, we move the boxes along the z-axis
box1:Move(0, 0, seconds * -5)
// when a box hits the other wall, we have to put it back to the original position
if box1:GetZ() < -10 // -10 is the Z-Axis position of the left wall
Remove(box1) // we remove it from the scene temporarily
box1:SetZ(10) // the starting point
Add(box1) // we put it again on the scene
end
In this example, we make the box move from 10 to -10 on the Z-axis (where our walls are located). Now we need to move the rest of our obstacles. You can try varying the speed of your different obstacles as well to make your game a bit more challenging.
Activity
Following the above example, in the Update action, call the Move action of each box and write a conditional to reset the position when it reaches the wall. Try making the two different rows of boxes go opposite directions.
The final aim of this section is to have 2 rows with 2 boxes on each (with different colors if possible) that are moving from one wall to another. For the first row, the boxes should go from left to right. For the second row, they should go from right to left.
Goal 3 : Win or lose the game
Now we have to set the win/lose conditions of our game. The program template has some of the code written for us.
Example
Our game has two possible endings: we hit a box and we lose the game or we reach the goal and we win the game. The template manages the win condition (collision with the goal), and manages collisions with our first box. The template uses the action
IsColliding(Model character, Model box)
which returns a boolean value indicating whether the two parameters are colliding.
We can use an action that returns a boolean value in our conditional statements. In this case, we want to call the action Lose() if the character is colliding with the box. Let's look at the win/lose conditional statement in our template. This is in the Update action of our program.
The general idea of this conditional statement is: if there is a collision with a box, you lose the game. Otherwise, if there is a collision with the goal, you win the game. If neither of those things has happened, the game should play normally. This is what that looks like in the template:
if IsColliding(character, box1) // if the player is hit by a box, GAME OVER
Lose()
//elseif {check box 2 collision and then check for the remaining boxes}
elseif IsColliding(character, goal)// if he hit the goal, victory
Win()
else
//this is where the code to move the boxes is (from Goal #2)
//this is where the audio management code is (completed in the next activity)
end
Activity
Following the above example, modify the conditional statement (by adding more elseif statements) to check the collision for the new boxes you have added. Each time you call the
IsColliding(Model character, Model box)
action, be sure to pass the character and the box you are checking. If any of them return true, call the Lose() action.
Example
The last thing that must be done in order to play the game, is managing the Audio for our boxes. When our character is near a box, we need to stream the car sound for that box. Our program template has an action
IsNearCar(Model character, Model box, Audio3D soundCar) returns boolean
which, like the previous IsColliding action, returns a boolean value that indicates if the character is near the box. If the character is near box1, we Stream the audio attached to box1 (soundCar1). This is what the code to do this for box1 look like:
// every update we check if the character is near a box
if IsNearCar(character, box1, soundCar1)
// if the character is near a box we play the sound car
soundCar1:Stream()
//elseif {check nearness for box 2 and then for the rest of the boxes}
end
Activity
Following the above example, and using the IsNearCar action, modify the conditional statement to manage the audio for the boxes you have created. Remember to pass the character, the box you are checking, and the audio for that box.
Now, you have a basic Frogger style game. To make this game even better, you can add more rows of boxes.
Collaborate
Collaboration is an important part of programming. When you work with other programmers, you get other points-of-view that make your programs even better. The final part of this assignment requires you to learn how to use Git, a great tool for collaborating. Head over to the Teams and Version Control Tutorial if you need to know how to use Git.
Optional Collaboration Activity
Social media has changed the way that we communicate. It has provided a simple way to disseminate or broadcast information. It continues to develop new ways to communicate. Take advantage of this modern form of communication by creating a social media account (if you are older than 13) and sharing your Quorum Game. Invite your friends to download and play your game.
Next Tutorial
In the next tutorial, we will discuss Assignment 4.2, which describes an introduction to file saving and action overloading..