-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.html
230 lines (222 loc) · 7.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&family=Quicksand:wght@400;700&display=swap" rel="stylesheet">
<style>
html, body {
font-family: "Quicksand", sans-serif;
text-align: center;
}
.title, .battle-menu {
font-family: "Press Start 2P", sans-serif;
}
.boss {
font-size: 360px;
}
.menu-list {
display: block;
list-style: none;
border: 1px solid black;
width: 50%;
margin: auto;
margin-top: 2rem;
padding-left: 0;
}
.menu-list li {
padding: 1rem;
background-color: red;
color: white;
}
#attack {
background-color: red;
}
#skill {
background-color: yellow;
color:rgb(55, 55, 55);
}
#ultimate {
background-color: white;
color: rgb(55, 55, 55);
}
#attack, #skill, #ultimate {
cursor: pointer;
}
#ultimate.ready {
color: white;
animation-name: ultimate;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
#status {
margin-top: 1rem;
display: block;
}
#combat-log {
margin-top: 1rem;
font-family: "Press Start 2P", sans-serif;
font-size: 1rem;
line-height: 1.5;
width: 50%;
padding: 1rem 0.5rem;
}
@keyframes ultimate {
0% {
background-color: red;
}
100% {
background-color: blue;
}
}
</style>
<title>Boss Battle</title>
</head>
<body>
<h1 class="title" id="boss-name">Boss</h1>
<div class="battle-scene">
<span class="boss">😼</span>
</div>
<div class="battle-menu">
<h2 id="player-name">Josuke</h2>
<span id="health">2000</span> / <span id="full-health">2000</span>
Atk: <span id="atk">20</span>
Def: <span id="def">20</span>
<ul class="menu-list">
<li id="attack">Attack</li>
<li id="skill">Skill</li>
<li id="ultimate" >Ultimate</li>
</ul>
<span id="status"></span>
<textarea readonly id="combat-log"></textarea>
<div style="margin: 40px;">
<button id="btn-reset" onclick="onReset()">Reset</button>
</div>
</div>
<script>
// Game variable
var playerHealth = 2000
var playerAtk = 300
var playerDef = 300
var playerUltimatePoint = 0
var bossHealth = 1500
var bossAtk = 400
var bossDef = 100
var possiblePlayerNames = ["Yusuke", "Mark", "Max", "Gun"]
var possibleBossNames = ["Cleaner Queen", "Prince Crimson", "Mark (Impostor)"]
// DOM Element
var btnAttack = document.getElementById("attack")
var btnSkill = document.getElementById("skill")
var btnUltimate = document.getElementById("ultimate")
var playerHealthBar = document.getElementById("health")
var playerAtkStatus = document.getElementById("atk")
var playerDefStatus = document.getElementById("def")
var playerStatus = document.getElementById("status")
var combatLog = document.getElementById("combat-log")
function onReset() {
playerHealthBar.innerText = 2000
}
function random(min, max) {
// Credit: https://www.geeksforgeeks.org/how-to-select-a-random-element-from-array-in-javascript/
return Math.floor(Math.random() * (max - min) + min)
}
function updateState(state, value) {
switch (state) {
case 'playerHealth':
playerHealthBar.innerText = value
break
case 'playerAtk':
playerAtkStatus.innerHTML = value
break
case 'playerDef':
playerDefStatus.innerHTML = value
break
case 'playerUltimatePoint':
if (playerUltimatePoint >= 20) {
btnUltimate.classList.toggle('ready')
}
}
}
function logCombat(message) {
combatLog.value = combatLog.value + message + "\n"
console.log(combatLog.innerText)
}
function checkWin() {
if (bossHealth <= 0) {
logCombat("You win.")
playerStatus.innerText = "You win."
}
}
function checkLose() {
if (playerHealth <= 0) {
logCombat("You lose.")
playerStatus.innerText = "You lose."
}
}
function playerAttack() {
bossHealth -= playerAtk - bossDef
updateState('playerUltimatePoint', playerUltimatePoint += 2)
logCombat("You attacked the boss")
checkWin()
bossAction()
}
function bossAttack() {
updateState('playerHealth',playerHealth -= bossAtk - playerDef)
updateState('playerUltimatePoint', playerUltimatePoint += 2)
logCombat("The boss attacked you.")
checkLose()
}
function bossAction() {
action = random(0, 12)
switch (action) {
case 1:
bossAtk *= 1.2
logCombat("The boss boosted their attack.")
bossAttack()
break
case 2:
bossDef *= 1.3
logCombat("The boss increase their guard.")
bossAttack()
break
case 3:
bossHealth += 300
logCombat("The boss healed themselve.")
break
default:
bossAttack()
break
}
}
function playerSkill() {
updateState('playerHealth',playerHealth += 150)
logCombat("You heal yourself.")
bossAction()
}
function playerUltimate() {
bossHealth -= playerAtk * 1.2
updateState('playerAtk', playerAtk *= 1.2)
playerUltimatePoint = 0
btnUltimate.classList.toggle('ready')
logCombat("You unleashed the ultimate techique.")
checkWin()
}
function initGame() {
var playerNameElement = document.getElementById("player-name")
var bossNameElement = document.getElementById("boss-name")
playerNameElement.innerText = possiblePlayerNames[random(0, possiblePlayerNames.length)]
bossNameElement.innerText = possibleBossNames[random(0, possibleBossNames.length)]
}
btnAttack.addEventListener('click', playerAttack)
btnSkill.addEventListener('click', playerSkill)
btnUltimate.addEventListener('click', function() {
if (playerUltimatePoint >= 20) {
playerUltimate()
}
})
initGame();
</script>
</body>
</html>