The objective of this lab is to create a simple space battle game where the player controls a spaceship and battles against alien ships to save Earth.
- Object-Oriented Programming (OOP) principles
- Classes and objects
- Methods and attributes
- Game logic and loops
- This is a space battle game where you, as the player, control a spaceship and fight against alien ships to save Earth.
- The game involves two classes:
Player
(for the player's spaceship) andAlienShip
(for the alien spaceships). - Both classes have attributes such as hit points (HP), damage/power, armor, and a method for attacking.
- Start by creating a constructor for the
Player
class that takes aname
parameter. - Inside the constructor, initialize
this.hp
to 1000 (hit points),this.power
to a random number between 50 and 100 (damage), andthis.armor
to a random number between 20 and 40 (armor).
- Next, create a constructor for the
AlienShip
class that initializesthis.hp
to 100 (hit points),this.power
to a random number between 30 and 60 (damage), andthis.armor
to a random number between 0 and 20 (armor).
- In the
Player
class, define anattack
method that takes atarget
parameter (this parameter represents an instance ofAlienShip
). - Inside the
attack
method, subtract the damage (this.power
) minus the target's armor from the target's HP and log the attack result. (e.g.HIT! You've struck an alien ship! Their current HP is now 25
except the number 25 will be dynamic and not hardcoded)
- Define a function named
startGame
that takes the player's name as a parameter and create an instance of the player class using the name that the user entered. - Inside
startGame
, create an arrayalienShips
to store instances ofAlienShip
(you can start with, say, 6 alien ships). - Initialize a boolean variable
playersTurn
totrue
to track whose turn it is.
- Create a game loop that runs as long as the player's spaceship has HP and there are still alien ships.
- Inside the loop:
- Determine the current target (
currentTarget variable
) by accessing the first alien ship in thealienShips
array. - If it's the player's turn:
- Ask the Player what they want to do next: attack, defend, power up.
- If it's the alien's turn:
- Randomize what the alien will do next. Perhaps 20% chance of a power up, 50% attack, 30% defend.
- Update
playersTurn
after each turn. - Check if the current target (
currentTarget
) is defeated (HP <= 0) and remove it from thealienShips
array. - Check if the player's spaceship HP is <= 0 to end the game loop.
- Determine the current target (
- Inside the loop:
- After each attack, check if the current target's HP is less than or equal to 0. If so, remove it from the
alienShips
array.
- After the loop ends, check if
alienShips.length === 0
to determine if the player won (all aliens defeated). - If
alienShips.length > 0
, check if the player's HP is less than or equal to 0 to determine if the player lost. - Log appropriate messages for winning or losing.
- Call the
startGame
function with a player name to start the game. For example,startGame("YourName")
. - In your terminal type
node index.js
. You will see the output of your game.