Skip to content

Commit 08dd116

Browse files
committed
completed fightingGame by following steps
1 parent fad0f22 commit 08dd116

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

projects/fightinggame/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ <h3>L:</h3>
7373
<audio id="victory" controls style="display: none">
7474
<source src="sounds/victory.mp3" type="audio/mpeg" />
7575
</audio>
76-
77-
<script src="script.js"></script>
76+
<script src="./script.js"></script>
7877
</body>
7978
</html>

projects/fightinggame/script.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const updateGame = (p1,p2,gameState) => {
3737
{
3838
game.isOver = true
3939
gameState = game.isOver
40+
resultDiv.innerText = game.declareWinner(game.isOver,p1,p2)
41+
return gameState
4042
}
4143
}
4244

@@ -55,24 +57,31 @@ class Player {
5557
strike (player,enemy,attackDmg) {
5658

5759
// Get random number between 1 - 10 and that is damageAmount
58-
60+
let damageAmount = Math.ceil(Math.random() * attackDmg)
5961
// Subtract the enemy health with the damageAmount
60-
61-
// Update the game and DOM with updateGame()
62-
62+
enemy.health -= damageAmount
63+
// Update the game and DOM with updateGame
64+
updateGame(p1,p2,gameState)
6365
// Return a message of 'player name attacks enemy name for damageAmount'
64-
66+
return `${ player.name } attacks ${ enemy.name } for ${ damageAmount }`
6567
}
6668
// ** Heal the player for random number from 1 to 5 **
6769
heal (player) {
6870

6971
// Get random number between 1 - 5 and store that in hpAmount
72+
let hpAmount = Math.ceil(Math.random() * 5)
7073

7174
// Add hpAmount to players health
75+
if (player.health < 100)
76+
{
77+
player.health += hpAmount
78+
}
7279

7380
// Update the game and DOM with updateGame()
81+
updateGame(p1,p2,gameState)
7482

7583
// Return a message of 'player name heals for hpAmount HP'
84+
return `${ player.name } heals for ${ hpAmount } HP!`
7685

7786
}
7887
}
@@ -87,34 +96,55 @@ class Game {
8796

8897
// ** If the game is over and a player has 0 health declare the winner! **
8998
declareWinner (isOver,p1,p2) {
90-
99+
let message
91100
// Create a message variable that will hold a message based on the condition
92-
93101
// If isOver is true AND p1 health is <= 0 then update message variable to 'p1 WINS!'
102+
if (isOver == true && p1.health <= 0)
103+
{
104+
message = `${ p2.name } WINS!`
105+
}
94106

95107
// Else if isOver is true AND p2 health is <= 0 then update message variable to 'p2 WINS!'
108+
else if (isOver == true && p2.health <= 0)
109+
{
110+
message = `${ p1.name } WINS!`
111+
}
112+
console.log(isOver,p1.health,p2.health);
113+
96114
// Play victory sound
115+
document.getElementById('victory').play()
97116

98117
// Return message variable
118+
return message
99119

100120
}
101121

102122
// ** Reset the players health back to it's original state and isOver to FALSE **
103123
reset (p1,p2) {
104124
// set p1 health and p2 health back to 100 and isOver back to false and clear resultDiv.innerText and don't forget to updateGame()
125+
p1.health = 100
126+
p2.health = 100
127+
this.isOver = false
128+
resultDiv.innerText = ""
129+
updateGame(p1,p2)
105130

106131
}
107132

108133
// ** Simulates the whole match untill one player runs out of health **
109134
play (p1,p2) {
110135
// Reset to make sure player health is back to full before starting
111-
136+
this.reset(p1,p2)
112137
// Make sure the players take turns untill isOver is TRUE
113138
while (!this.isOver)
114139
{
115140
//Make sure both players get strike() and heal() once each loop
141+
p1.strike(p1,p2,p1.attackDmg)
142+
p2.heal(p2)
143+
p2.strike(p2,p1,p2.attackDmg)
144+
p1.heal(p1)
116145
}
117146
// Once isOver is TRUE run the declareWinner() method
147+
return this.declareWinner(this.isOver,p1,p2)
118148

119149
}
120150

@@ -133,13 +163,15 @@ let p2 = player2
133163
let game = new Game()
134164

135165
// ** Intialize the game by calling updateGame() **
136-
updateGame(p1,p2,game.isOver)
166+
updateGame(p1,p2)
137167

138168
// ** Save intial isOver from the game object inside this variable **
139-
let gameState;
140-
169+
let gameState = game.isOver
141170

142171
// ** Add a click listener to the simulate button that runs the play() method on click and pass in the players **
172+
play.onclick = () => {
173+
resultDiv.innerText = game.play(p1,p2)
174+
}
143175

144176

145177
// Add functionality where players can press a button to attack OR heal
@@ -153,18 +185,17 @@ document.addEventListener('keydown',function (e) {
153185
// After striking then play attack sound
154186
document.getElementById('p1attack').play()
155187
}
156-
157188
});
158189

159190
document.addEventListener('keydown',function (e) {
160191

161192
// if you press a AND the player health is greater than 0 AND isOver is still false then strike()
162193
if (e.key == "a" && p2.health > 0)
163194
{
195+
p1.heal(p1)
164196
// After healing then play heal sound
165197
document.getElementById('p1heal').play()
166198
}
167-
168199
});
169200

170201
// ** Player 2 Controls **
@@ -177,7 +208,6 @@ document.addEventListener('keydown',function (e) {
177208
// After striking then play attack sound
178209
document.getElementById('p2attack').play()
179210
}
180-
181211
});
182212

183213
document.addEventListener('keydown',function (e) {
@@ -188,6 +218,5 @@ document.addEventListener('keydown',function (e) {
188218
player2.heal(p2)
189219
document.getElementById('p2heal').play()
190220
}
191-
192221
});
193222

0 commit comments

Comments
 (0)