-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
220 lines (196 loc) · 5.15 KB
/
script.js
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
// DECLARE VARIABLES
const canvas = document.querySelector('#canvas')
const player = document.querySelector('#player')
const enemy = document.querySelector('#enemy')
const playButton = document.querySelector('#playButton')
const gameOverText = document.querySelector('.game-over-text')
const gameOverScoreText = document.querySelector('.game-over-score-text')
const howToText = document.querySelector('.how-to-text')
const scoreText = document.querySelector('.score-text')
// Player
let moveLeftRight = 0
// Enemy
let moveDown = 0
let randomizePosition = Math.floor(Math.random() * 300)
let moveDownSpeed = 40
let score = 0
// PLAY GAME
let gamePlaying = false
playButton.addEventListener('click', () => {
playGame()
gamePlaying = true
})
// Play Game if user press space bar or enter key
window.addEventListener('keydown', (event) => {
if (!gamePlaying) {
if (event.key === ' ' || event.key === 'Enter') {
playGame()
gamePlaying = true
}
}
})
function playGame() {
playButton.style.display = 'none'
howToText.style.display = 'none'
// MOVE ENEMY
// randomize enemy position
enemy.style.marginLeft = randomizePosition + 'px'
function moveEnemyDown() {
moveDown += 10
enemy.style.marginTop = moveDown + 'px'
// CHECK IF ENEMY IS OUT OF CANVAS (BOTTOM)
if (moveDown >= 400) {
moveDown = 0
randomizePosition = Math.floor(Math.random() * 300)
enemy.style.marginLeft = randomizePosition + 'px'
}
}
let moveEnemy = setInterval(moveEnemyDown, moveDownSpeed)
// INCREASE ENEMY SPEED
setInterval(() => {
moveDownSpeed -= 2
// max speed
if (moveDownSpeed <= 14) {
moveDownSpeed = 14
}
clearInterval(moveEnemy)
moveEnemy = setInterval(moveEnemyDown, moveDownSpeed)
}, 5000)
// ARROW KEYS EVENT LISTENER
window.addEventListener('keydown', (event) => {
if (event.key === 'ArrowRight') {
// MOVE PLAYER RIGHT
moveLeftRight += 30
player.style.marginLeft = moveLeftRight + 'px'
// STOP PLAYER FROM GOING OUT OF CANVAS (RIGHT)
if (moveLeftRight >= 150) {
moveLeftRight = 160 - 20
}
}
if (event.key === 'ArrowLeft') {
// MOVE PLAYER LEFT
moveLeftRight -= 30
player.style.marginLeft = moveLeftRight + 'px'
// STOP PLAYER FROM GOING OUT OF CANVAS (LEFT)
if (moveLeftRight <= -150) {
moveLeftRight = -150 + 20
}
}
})
// MOBILE SWIPE EVENTS
// TOUCH HANDLER EVENTS CODE
;(function (d) {
var ce = function (e, n) {
var a = new CustomEvent(n, {
bubbles: true,
cancelable: true,
detail: e.target,
})
e.target.dispatchEvent(a)
a = null
return false
},
nm = true,
sp = {
x: 0,
y: 0,
},
ep = {
x: 0,
y: 0,
},
touch = {
touchstart: function (e) {
sp = {
x: e.touches[0].pageX,
y: e.touches[0].pageY,
}
},
touchmove: function (e) {
nm = false
ep = {
x: e.touches[0].pageX,
y: e.touches[0].pageY,
}
},
touchend: function (e) {
if (nm) {
ce(e, 'fc')
} else {
var x = ep.x - sp.x,
xr = Math.abs(x)
if (xr > 20) {
ce(e, x < 0 ? 'swl' : 'swr')
}
}
nm = true
},
touchcancel: function (e) {
nm = false
},
}
for (var a in touch) {
d.addEventListener(a, touch[a], false)
}
})(document)
// SWIPE RIGHT
document.body.addEventListener(
'swr',
() => {
// MOVE PLAYER RIGHT
moveLeftRight += 80
player.style.marginLeft = moveLeftRight + 'px'
// STOP PLAYER FROM GOING OUT OF CANVAS (RIGHT)
if (moveLeftRight >= 160) {
// alert(moveLeftRight);
moveLeftRight = 160 - 80
}
},
false
)
// SWIPE LEFT
document.body.addEventListener(
'swl',
() => {
// MOVE PLAYER LEFT
moveLeftRight -= 80
player.style.marginLeft = moveLeftRight + 'px'
// STOP PLAYER FROM GOING OUT OF CANVAS (LEFT)
if (moveLeftRight <= -150) {
moveLeftRight = -150 + 80
}
},
false
)
// SCORE
let scoreInterval = setInterval(() => {
score += 1
scoreText.textContent = score
}, 200)
// GAME OVER
setInterval(() => {
const playerLeft = player.offsetLeft
const enemyLeft = enemy.offsetLeft
const playerTop = player.offsetTop
const enemyTop = enemy.offsetTop
distanceLeftRight = playerLeft - enemyLeft
distanceTopBottom = playerTop - enemyTop
if (
distanceLeftRight <= 60 &&
distanceLeftRight >= -10 &&
distanceTopBottom <= 10 &&
distanceTopBottom >= -10
) {
clearInterval(scoreInterval)
player.style.width = 0 + 'px'
player.style.height = 0 + 'px'
enemy.style.display = 'none'
gameOverText.style.display = 'block'
gameOverScoreText.textContent = 'Score: ' + score
gameOverScoreText.style.display = 'block'
setTimeout(() => {
location.reload()
}, 3000)
}
}, 8)
}