|
1 |
| -let playButton = document.getElementById('play') |
2 |
| -let resultDiv = document.getElementById('result') |
| 1 | +/* |
| 2 | +🌟 APP: Fighting Game |
| 3 | +
|
| 4 | +Create an updateGame() function that will update the DOM with the state of the game 👇 |
| 5 | +======================================== |
3 | 6 |
|
| 7 | +- updateGame() |
4 | 8 |
|
| 9 | +These are the 2 classes you must create and their methods 👇 |
| 10 | +======================================== |
5 | 11 |
|
6 | 12 | class Player {
|
7 |
| - constructor(name, health, atkDamage) { |
| 13 | + - strike() |
| 14 | + - heal() |
| 15 | +} |
| 16 | +
|
| 17 | +class Game { |
| 18 | + - play() |
| 19 | + - checkIsOver() |
| 20 | + - declareWinner() |
| 21 | + - reset() |
| 22 | +} |
| 23 | +
|
| 24 | +These functions are hard coded in the HTML. So, you can't change their names. |
| 25 | +
|
| 26 | +These are all the DIV ID's you're gonna need access to 👇 |
| 27 | +======================================================== |
| 28 | +#1 ID 👉 'play' = Button to run simulation |
| 29 | +#2 ID 👉 'result' = Div that holds the winner of the match |
| 30 | +#3 ID 👉 'p1Health' = Div that holds player 1's health |
| 31 | +#4 ID 👉 'p2Health' = Div that holds player 2's health |
| 32 | +*/ |
| 33 | + |
| 34 | +// ** Grabs elements from the DOM and stores them into variables ** |
| 35 | +let playButton = document.getElementById('play') |
| 36 | +let resultDiv = document.getElementById('result') |
| 37 | +let p1HealthDiv = document.getElementById('p1Health') |
| 38 | +let p2HealthDiv = document.getElementById('p2Health') |
| 39 | + |
| 40 | +// ** Check if either players health is 0 and if it is, then update isOver to true ** |
| 41 | +const updateGame = (p1,p2,p1HealthDiv,p2HealthDiv, gameState) => { |
| 42 | + // Update the DOM with the latest health of players |
| 43 | + console.log(p1.health, p1, "👈👈👈👈") |
| 44 | + p1HealthDiv.innerText = p1.health |
| 45 | + p2HealthDiv.innerText = p2.health |
| 46 | + if (p1.health <= 0 || p2.health <= 0) { |
| 47 | + game.isOver = true; |
| 48 | + gameState = game.isOver |
| 49 | + result.innerText = game.declareWinner(game.isOver,p1,p2) |
| 50 | + return gameState |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// ** Create the Player class which can create a player with all it's attributes and methods ** |
| 55 | +class Player { |
| 56 | + constructor(name, health, attackDamage) { |
8 | 57 | this.name = name;
|
9 | 58 | this.health = health;
|
10 |
| - this.attack = atkDamage; |
| 59 | + this.attackDmg = attackDamage; |
11 | 60 | }
|
12 |
| - |
13 |
| - |
14 |
| - attack(player, enemy) { |
15 |
| - let damageAmount = Math.ceil(Math.random() * 10) |
| 61 | + // ** Attack an enemy with a random number from 0 to YOUR attackDmg bonus ** |
| 62 | + strike (player, enemy, attackDmg) { |
| 63 | + let damageAmount = Math.ceil(Math.random() * attackDmg) |
16 | 64 | enemy.health -= damageAmount
|
| 65 | + |
| 66 | + updateGame(p1,p2,p1HealthDiv,p2HealthDiv,gameState) |
17 | 67 |
|
18 |
| - return `${player} attacks ${enemy} for ${damageAmount} damage!` |
| 68 | + return `${player.name} attacks ${enemy.name} for ${damageAmount}` |
19 | 69 | }
|
20 |
| - //Generate a random number between 1 and 5 using Math.random() |
21 |
| - //Use that number to increase health of player 1 |
22 |
| - heal(player) { |
| 70 | + // ** Heal the player for random number from 1 to 5 ** |
| 71 | + heal (player) { |
23 | 72 | let hpAmount = Math.ceil(Math.random() * 5)
|
24 | 73 | player.health += hpAmount
|
25 |
| - |
26 |
| - return `${player} heals for ${hpAmount} + HP!` |
| 74 | + |
| 75 | + updateGame(p1,p2,p1HealthDiv,p2HealthDiv,gameState) |
| 76 | + return `${player.name} heals for ${hpAmount} + HP!` |
27 | 77 | }
|
28 | 78 | }
|
29 | 79 |
|
| 80 | +// ** Create the Game class with all it's attributes and methods to run a match ** |
30 | 81 | class Game {
|
31 |
| - // default arugments |
32 |
| - constructor(player1, player2) { |
33 |
| - // Flag that indicates if the game is over or not |
| 82 | + constructor(p1HealthDiv,p2HealthDiv) { |
34 | 83 | this.isOver = false;
|
| 84 | + this.p1HealthDiv = p1HealthDiv |
| 85 | + this.p2HealthDiv = p2HealthDiv |
35 | 86 | }
|
36 | 87 |
|
37 |
| - // Starts the game and logs out the status of players |
38 |
| - play(player1, player2) { |
39 |
| - // this.reset(); |
40 |
| - |
41 |
| - // while loops |
42 |
| - console.log(player1.attack()) |
43 |
| - // while (!this.isOver) { |
44 |
| - // player1.attack(player1,player2) |
45 |
| - // player1.heal(player1) |
46 |
| - // player2.attack(player2,player1); |
47 |
| - // player2.heal(player2) |
48 |
| - // this.checkTheEnd(player1,player2); |
49 |
| - // } |
50 |
| - // return this.declareWinner(this.isOver,this.player1,this.player2); |
51 |
| - } |
52 |
| - |
53 |
| - // Console log the winner of the battle |
| 88 | + // ** If the game is over and a player has 0 health declare the winner! ** |
54 | 89 | declareWinner(isOver,p1, p2) {
|
55 | 90 | let message
|
56 | 91 | if (isOver == true && p1.health <= 0) {
|
57 | 92 | message = `${p2.name} WINS!`;
|
58 |
| - console.log(message) |
59 | 93 | }
|
60 | 94 | else if(isOver == true && p2.health <= 0) {
|
61 | 95 | message = `${p1.name} WINS!`
|
62 | 96 | }
|
| 97 | + console.log(isOver,message, "🧑🚀🧑🚀🧑🚀🧑🚀", p2.health, p1.health) |
63 | 98 | return message
|
64 | 99 | }
|
65 | 100 |
|
66 |
| - // If player 1 or player 2 health is below 0 |
67 |
| - // Mark theEnd true, to stop the game |
68 |
| - checkTheEnd(p1,p2) { |
69 |
| - if (p1.health <= 0 || p2.health <= 0) { |
70 |
| - this.isOver = true; |
71 |
| - return this.isOver |
72 |
| - } |
73 |
| - |
| 101 | + // ** Reset the players health back to it's original state and isOver to FALSE ** |
| 102 | + reset(p1,p2) { |
| 103 | + p1.health = 100 |
| 104 | + p2.health = 100 |
| 105 | + this.isOver = false |
| 106 | + resultDiv.innerText = '' |
| 107 | + updateGame(p1,p2,p1HealthDiv,p2HealthDiv) |
| 108 | + } |
| 109 | + |
| 110 | + // ** Simulates the whole match untill one player runs out of health ** |
| 111 | + play(p1, p2) { |
| 112 | + this.reset(p1,p2); |
| 113 | + // Make sure the players take turns untill isOver is TRUE |
| 114 | + while (!this.isOver) { |
| 115 | + p1.strike(p1,p2, p1.attackDmg) |
| 116 | + p2.heal(p2) |
| 117 | + p2.strike(p2,p1, p2.attackDmg); |
| 118 | + p1.heal(p1) |
| 119 | + updateGame(p1,p2,p1HealthDiv,p2HealthDiv); |
| 120 | + } |
| 121 | + // Once isOver is TRUE run the declareWinner() method |
| 122 | + return this.declareWinner(this.isOver,player1,player2); |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +// ** Create 2 players using the player class ** |
| 128 | +let player1 = new Player('Lance', 100, 15) |
| 129 | +let player2 = new Player('Qazi', 100, 15) |
| 130 | + |
| 131 | +// ** Save original Player Data ** |
| 132 | +let p1 = player1 |
| 133 | +let p2 = player2 |
| 134 | + |
| 135 | +// ** Create the game object from the Game class ** |
| 136 | +let game = new Game(p1HealthDiv,p2HealthDiv); |
| 137 | + |
| 138 | +// ** Save original Game Data ** |
| 139 | +let gameState = game.isOver |
| 140 | + |
| 141 | + |
| 142 | +// ** Add a click listener to the simulate button that runs the play() method on click and pass in the players ** |
| 143 | +play.onclick = () => result.innerText = game.play(player1,player2); |
| 144 | + |
| 145 | + |
| 146 | +// ** BONUS ** |
| 147 | +// Add functionality where players can press a button to attack OR heal |
| 148 | + |
| 149 | +// ** Player 1 Controls ** |
| 150 | +document.addEventListener('keydown', function(e) { |
| 151 | + if (e.key == "q" && player2.health > 0 ){ |
| 152 | + player1.strike(player1, player2, player1.attackDmg) |
| 153 | + document.getElementById('p1attack').play(); |
74 | 154 | }
|
| 155 | +}); |
75 | 156 |
|
76 |
| - // Console log the name and health of both players |
77 |
| - // Ex: Player 1: 100 | Player 2: 50 |
78 |
| - playerStatus() { |
79 |
| - // console.log( |
80 |
| - // 'Player 1: ' + this.player1.name + ',' + ' Health: ' + this.player1.health |
81 |
| - // ); |
82 |
| - // console.log( |
83 |
| - // 'Player 2: ' + this.player2.name + ',' + ' Health: ' + this.player2.health |
84 |
| - // ); |
| 157 | +document.addEventListener('keydown', function(e) { |
| 158 | + if (e.key == "a" && player2.health > 0 ){ |
| 159 | + player1.heal(player1) |
| 160 | + document.getElementById('p1heal').play(); |
85 | 161 | }
|
| 162 | +}); |
86 | 163 |
|
87 |
| - //Reset health of player 1 and player 2 to 100 |
88 |
| - //Reset theEnd to false |
89 |
| - // reset() { |
90 |
| - // this.player1.health = 100; |
91 |
| - // this.player2.health = 100; |
92 |
| - // this.isOver = false; |
93 |
| - // } |
94 |
| - // ** pl1AttackPl2 allows you to attack another player & deplete their health ** |
95 |
| - //Generate a random number between 1 and 10 using Math.random() |
96 |
| - //Use that number to decrease health from player 2 |
97 |
| - // Problem: trying to do 2 things |
| 164 | +// ** Player 2 Controls ** |
| 165 | +document.addEventListener('keydown', function(e) { |
| 166 | + if (e.key == "p" && player1.health > 0){ |
| 167 | + player2.strike(player2, player1, player2.attackDmg) |
| 168 | + document.getElementById('p2attack').play(); |
| 169 | + } |
| 170 | +}); |
98 | 171 |
|
99 |
| -} |
| 172 | +document.addEventListener('keydown', function(e) { |
| 173 | + if (e.key == "l" && player2.health > 0 ){ |
| 174 | + player2.heal(player2) |
| 175 | + document.getElementById('p2heal').play(); |
| 176 | + } |
| 177 | +}); |
100 | 178 |
|
101 |
| -// Initialize the class here |
102 |
| -// Call the start function of the game |
103 |
| -let player1 = new Player('Lance', 75, 25) |
104 |
| -let player2 = new Player('Qazi', 200, 0) |
105 |
| -let game = new Game(); |
106 | 179 |
|
107 |
| -play.onclick = () => result.innerText = game.play(player1,player2) |
108 |
| -; |
109 | 180 |
|
110 | 181 |
|
0 commit comments