Suggested Solution

Task 1 Task 2
 

Task 1 Part A

Mario exists in a 2D grid. The top-left corner of the grid has an (x,y) co-ordinate of (0,0). Moving right, the x value increases. Moving down, the y value increases.

The grid is 400x400:

Every Mario object has 3 private properties:

At the start of a game, Mario's (x,y) position is always (200,200) and his health is 10.

In the game loop, Mario can jump around. Each jump is counted. Note, the jump count is not a property of Mario, it is just a game variable.

Every time he jumps off the grid, his health decreases by 1. The game ends when his health reaches 0 and the jump count, health and final location are displayed.

Mario is off-grid when:

  • his x co-ordinate is less than 0 or
  • his x co-ordinate is greater than 400 or
  • his y co-ordinate is less than 0 or
  • his y co-ordinate is greater than 400

Design the Mario class. Use private properties ie Data Hiding. Create:

Remember! Accessor methods allow programs to view the contents of an object's private variables.

Mutator methods allow programs to change the values stored in an object's private variables.

Task 1 Part B - Jumping Mario Game!

We will now create a game. It will have 1 object: a Mario object. In your Main program, construct a Mario object and run your program - hopefully no errors!

Task 1 Part C - The Game Loop

Create a variable to store the jump count and set it to 0.

Create a game loop where the game will continue while Mario's health is greater than 0.

Task 1 Part D - Make Mario Jump!

Inside the game loop, create 2 random numbers in the range -5 to 405. One will be assigned to Mario's x co-ordinate and the other will be assigned to Mario's y co-ordinate.

Task 1 Part E - Decreasing Health Logic

In the game loop, after Mario jumps, we need to check his new location. If he is off the grid, we will decrease his health by 1.

Task 1 Part F - Counting the jump

In the game loop, after updating Mario's health, let's increase the jump count by 1 and display Mario's current location and health.

Also, when the game loop ends, let's write a line of code to confirm Mario's final location and health value.

Task 2 - Jumping Competition

Create a game where 2 Mario's are jumping around the grid - they have the same behaviour! The last Mario alive is the winner and his name will be displayed.

Hint: Every Mario now has a new property - a name! Don't forget to create an accessor method for this property. You will also need to redesign the constructor method.