Skip to content

Commit 2a78acd

Browse files
committed
had some fun
1 parent 89124c5 commit 2a78acd

3 files changed

Lines changed: 73 additions & 29 deletions

File tree

‎assignments/index.html‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22

33
<html lang="en">
4-
<head>
5-
<meta charset="utf-8">
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="stylesheet" href="style.css" />
78

8-
<title>JS III</title>
9+
<title>JS III</title>
910

10-
<script src="this.js"></script>
11-
<script src="prototypes.js"></script>
12-
</head>
11+
<script src="this.js"></script>
12+
<script src="prototypes.js"></script>
13+
</head>
1314

14-
<body>
15-
<h1>JS III - Check your work in the console!</h1>
16-
</body>
17-
</html>
15+
<body>
16+
<h1>JS III - Check your work in the console!</h1>
17+
<div class="healthBar">
18+
<h3>Press S to make Sniper hit Pudge</h3>
19+
<h3>Press P to make Pudge hit Sniper</h3>
20+
<div class="sniper"><div id="SniperHealth"></div></div>
21+
<div class="pudge"><div id="PudgeHealth"></div></div>
22+
<div id="over"></div>
23+
</div>
24+
</body>
25+
</html>

‎assignments/prototypes.js‎

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,33 @@ function Hero(heroObj) {
142142
Hero.prototype = Object.create(Humanoid.prototype);
143143

144144
Hero.prototype.hit = function(enemy) {
145-
if (enemy.healthPoints <= 0) {
146-
return "Game Over! Stop hitting your enemy!!!";
147-
}
148-
enemy.healthPoints = enemy.healthPoints - this.dmg;
149-
let leftHealthMessage = "";
150-
if (enemy.healthPoints <= 0) {
151-
enemy.healthPoints = 0;
152-
leftHealthMessage = `Now ${enemy.name} is dead. ${this.name} wins the game.`;
145+
document.getElementById(
146+
`${this.name}Health`
147+
).innerHTML = `${this.name}: ${this.healthPoints} HP`;
148+
document.getElementById(
149+
`${enemy.name}Health`
150+
).innerHTML = `${enemy.name}: ${enemy.healthPoints} HP`;
151+
152+
if (this.healthPoints > 0) {
153+
if (enemy.healthPoints <= 0) {
154+
return "Game Over! Stop hitting your enemy!!!";
155+
}
156+
enemy.healthPoints = enemy.healthPoints - this.dmg;
157+
let message = "";
158+
if (enemy.healthPoints <= 0) {
159+
enemy.healthPoints = 0;
160+
message = `Now ${enemy.name} is dead. ${this.name} wins the game.`;
161+
document.getElementById(`over`).innerHTML = `GAME OVER!`;
162+
} else {
163+
message = `Now ${enemy.name} has ${enemy.healthPoints} health left.`;
164+
}
165+
document.getElementById(
166+
`${enemy.name}Health`
167+
).innerHTML = `${enemy.name}: ${enemy.healthPoints} HP`;
168+
return `${this.name} hit ${this.dmg} damage to ${enemy.name}. ${message}`;
153169
} else {
154-
leftHealthMessage = `Now ${enemy.name} has ${enemy.healthPoints} health left.`;
170+
return `You are dead ${this.name}, you can't hit ${enemy.name} anymore!`;
155171
}
156-
return `${this.name} hit ${this.dmg} damage to ${enemy.name}. ${leftHealthMessage}`;
157172
};
158173

159174
function Villain(villainObj) {
@@ -192,10 +207,12 @@ const sniper = new Villain({
192207
dmg: 30
193208
});
194209

195-
console.log(sniper.hit(pudge));
196-
console.log(sniper.hit(pudge));
197-
console.log(pudge.hit(sniper));
198-
console.log(pudge.hit(sniper));
199-
console.log(sniper.hit(pudge));
200-
console.log(sniper.hit(pudge));
201-
console.log(sniper.hit(pudge));
210+
document.addEventListener("keypress", hitEnemy);
211+
212+
function hitEnemy(e) {
213+
if (e.key == "s" || e.key == "S") {
214+
console.log(sniper.hit(pudge));
215+
} else if (e.key == "p" || e.key == "P") {
216+
console.log(pudge.hit(sniper));
217+
}
218+
}

‎assignments/style.css‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.healthBar {
2+
display: flex;
3+
width: 500px;
4+
margin: 0 auto;
5+
flex-direction: column;
6+
justify-content: space-around;
7+
text-align: center;
8+
}
9+
10+
.sniper,
11+
.pudge {
12+
font-size: 20px;
13+
font-weight: bold;
14+
line-height: 30px;
15+
}
16+
17+
#over {
18+
font-size: 60px;
19+
}

0 commit comments

Comments
 (0)