-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (118 loc) · 3.4 KB
/
script.js
File metadata and controls
146 lines (118 loc) · 3.4 KB
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
let myGunSound = new Audio('audio/3z.mp3')
let enemyGunSound = new Audio("audio/enemySound.mp3")
let music = new Audio('audio/music.mp3')
let loseSound = new Audio('audio/lose.mp3')
music.volume = 0.3
enemyGunSound.volume = 0.7
let speedShoot = 1500;
let speed = 3000;
let reSpeed = 2000
let score = 0
let currentLevel = 1
let truHp = 5
let scoreArea = document.getElementById('score')
scoreArea.innerHTML = 'Level: ' + currentLevel + '<br>' +
'Score: ' + score + '<br>' + 'High Score: ' +
localStorage.getItem('score')
function level(num) {
speedShoot -= 115 * num
speed -= 230 * num
reSpeed -= 153 * num
truHp += 5 * num
}
function updateScore() {
score += 10 * currentLevel
if (score > localStorage.getItem('score')) {
localStorage.setItem('score', score)
}
scoreArea.innerHTML = 'Level: ' + currentLevel + '<br>' +
'Score: ' + score + '<br>' + 'High Score: '
+ localStorage.getItem('score')
if (score % 100 == 0) {
level(currentLevel++)
}
}
function reEnemy(enemy) {
if (enemy.classList.contains('dead')) {
setTimeout(() => {
enemy.classList.remove('dead')
enemy.classList.toggle('showing')
}, reSpeed)
}
}
function iShoot(enemy) {
enemy.classList.add('dead')
updateScore()
reEnemy(enemy)
if (!livingEnemies().length) {
setTimeout(function () {
alert('WIN');
window.location.reload()
}, 1000)
}
}
function enemyAttacksMe(enemy) {
enemy.classList.add('showing')
setTimeout(() => {
enemyShootsMe(enemy)
}, speedShoot)
setTimeout(() => {
enemy.classList.remove('showing')
}, speed);
}
function enemyShootsMe(enemy) {
if (!enemy.classList.contains('dead')) {
enemyGunSound.play()
enemy.classList.add('shooting');
updateHealthPoints(healthPoints - truHp);
setTimeout(() => {
enemy.classList.remove('shooting')
}, 200);
}
}
function livingEnemies() {
return document.querySelectorAll('.enemy:not(.dead)')
}
function randomEnemyAttacks() {
let randomEnemyNo = Math.random() * livingEnemies().length;
randomEnemyNo = Math.floor(randomEnemyNo)
let enemy = livingEnemies()[randomEnemyNo]
let randomDelay = Math.random() * speed + reSpeed
setTimeout(() => {
enemyAttacksMe(enemy)
randomEnemyAttacks()
}, randomDelay)
}
let healthPoints = 100;
function updateHealthPoints(points) {
healthPoints = points;
let healthBar = document.querySelector('#healthBar')
healthBar.style.width = points + '%';
if (healthPoints < 1) {
loseSound.play()
alert('GAME OVER')
window.location.reload()
}
}
function newGame() {
music.play()
randomEnemyAttacks();
document.querySelector('button').style.display = 'none';
}
let hack = document.getElementById('hack')
let checkHack = true
window.addEventListener('keypress', (evt) => {
if (evt.keyCode == 13) {
if (checkHack) {
hack.innerHTML = '<center><input id="hackCode" type="text" autofocus></center>'
} else {
let hackValue = document.getElementById('hackCode').value
if (hackValue == 'hp') {
updateHealthPoints(99999999999999)
}
hack.innerHTML = ''
}
checkHack = !checkHack
}
}
)