-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
277 lines (265 loc) · 7.27 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
window.addEventListener('load', () => {
const canvas = document.getElementById('canvas1')
const ctx = canvas.getContext('2d')
canvas.width = 800
canvas.height = 720
let enemies = []
let score = 0
let gameOver = false
class InputHandler {
constructor() {
this.keys = []
window.addEventListener('keydown', (e) => {
if (
(e.key === 'ArrowDown' ||
e.key === 'ArrowUp' ||
e.key === 'ArrowRight' ||
e.key === 'ArrowLeft') &&
this.keys.indexOf(e.key) === -1
) {
this.keys.push(e.key)
}
console.log(e.key, this.keys)
})
window.addEventListener('keyup', (e) => {
if (
e.key === 'ArrowDown' ||
e.key === 'ArrowUp' ||
e.key === 'ArrowLeft' ||
e.key === 'ArrowRight'
) {
this.keys.splice(this.keys.indexOf(e.key), 1)
}
})
}
}
class Player {
constructor(gameWidth, gameHeight) {
this.gameWidth = gameWidth
this.gameHeight = gameHeight
this.width = 200
this.height = 200
this.x = 10
this.y = this.gameHeight - this.height
this.image = document.getElementById('playerImage')
this.frameX = 0
this.maxFrame = 8
this.fps = 20
this.frameTimer = 0
this.frameIterval = 1000 / this.fps
this.frameY = 0
this.speed = 0
this.vy = 0
this.weight = 1
}
draw(context) {
// context.fillStyle = 'white'
// context.fillRect(this.x, this.y, this.width, this.height)
context.strokeStyle = 'transparent'
context.strokeRect(this.x, this.y, this.width, this.height)
context.beginPath()
context.arc(
this.x + this.width / 2,
this.y + this.height / 2,
this.width / 2,
0,
Math.PI * 2
)
context.stroke()
context.drawImage(
this.image,
this.frameX * this.width,
this.frameY * this.height,
this.width,
this.height,
this.x,
this.y,
this.width,
this.height
)
}
update(input, deltaTime, enemies) {
//collision detection
enemies.forEach((enemy) => {
const dx = enemy.x - this.x
const dy = enemy.y - this.y
const distance = Math.sqrt(dx * dx + dy * dy)
if (distance < enemy.width / 2 + this.width / 2) {
gameOver = true
}
})
//sprite animation
if (this.frameTimer > this.frameIterval) {
if (this.frameX >= this.maxFrame) this.frameX = 0
else this.frameX++
this.frameTimer = 0
} else {
this.frameTimer += deltaTime
}
if (input.keys.indexOf('ArrowRight') > -1) {
this.speed = 5
} else if (input.keys.indexOf('ArrowLeft') > -1) {
this.speed = -5
} else if (input.keys.indexOf('ArrowUp') > -1 && this.onGround()) {
this.vy -= 30
} else {
this.speed = 0
}
//horizontal movement
this.x += this.speed
if (this.x < 0) {
this.x = 0
} else if (this.x > this.gameWidth - this.width) {
this.x = this.gameWidth - this.width
}
//vertical movement
this.y += this.vy
if (!this.onGround()) {
this.vy += this.weight
this.maxFrame = 5
this.frameY = 1
} else {
this.vy = 0
this.maxFrame = 8
this.frameY = 0
}
if (this.y > this.gameHeight - this.height) {
this.y = this.gameHeight - this.height
}
}
onGround() {
return this.y >= this.gameHeight - this.height
}
}
class Background {
constructor(gameWidth, gameHeight) {
;(this.gameWidth = gameWidth),
(this.gameHeight = gameHeight),
(this.image = document.getElementById('backgroundImage'))
this.x = 0
this.y = 0
this.width = 2400
this.height = 720
this.speed = 2
}
draw(context) {
context.drawImage(this.image, this.x, this.y, this.width, this.height)
context.drawImage(
this.image,
this.x + this.width,
this.y,
this.width,
this.height
)
}
update() {
this.x -= this.speed
if (this.x < 0 - this.width) this.x = 0
}
}
class Enemy {
constructor(gameWidth, gameHeight) {
this.gameWidth = gameWidth
this.gameHeight = gameHeight
this.width = 160
this.height = 119
this.image = document.getElementById('enemyImage')
this.x = this.gameWidth
this.y = this.gameHeight - this.height
this.frameX = 0
this.maxFrame = 5
this.fps = 20
this.frameTimer = 0
this.frameIterval = 1000 / this.fps
this.speed = 8
this.markedForDeletion = false
}
draw(context) {
context.strokeStyle = 'transparent'
context.strokeRect(this.x, this.y, this.width, this.height)
context.beginPath()
context.arc(
this.x + this.width / 2,
this.y + this.height / 2,
this.width / 2,
0,
Math.PI * 2
)
context.stroke()
context.drawImage(
this.image,
this.frameX * this.width,
0,
this.width,
this.height,
this.x,
this.y,
this.width,
this.height
)
}
update(deltaTime) {
if (this.frameTimer > this.frameIterval) {
if (this.frameX >= this.maxFrame) this.frameX = 0
else this.frameX++
this.frameTimer = 0
} else {
this.frameTimer += deltaTime
}
this.x -= this.speed
if (this.x < 0 - this.width) {
this.markedForDeletion = true
score++
}
}
}
// enemies.push(new Enemy(canvas.width, canvas.height))
function handleEnemies(deltaTime) {
if (enemyTimer > enemyInterval + randomEnemyInterval) {
enemies.push(new Enemy(canvas.width, canvas.height))
enemyTimer = 0
} else {
enemyTimer += deltaTime
}
enemies.forEach((enemy) => {
enemy.draw(ctx)
enemy.update(deltaTime)
})
enemies = enemies.filter((enemy) => !enemy.markedForDeletion)
}
function displayStatusText(context) {
context.font = '40px Helvetica'
context.fillStyle = 'black'
context.fillText('Score: ' + score, 20, 50)
context.fillStyle = 'white'
context.fillText('Score: ' + score, 22, 50)
if (gameOver) {
context.textAlign = 'center'
context.fillStyle = 'black'
context.fillText('GAME OVER try again', canvas.width / 2, 200)
context.fillStyle = 'red'
context.fillText('GAME OVER try again', canvas.width / 2 + 2, 200)
}
}
const input = new InputHandler()
const player = new Player(canvas.width, canvas.height)
const background = new Background(canvas.width, canvas.height)
const enemy1 = new Enemy(canvas.width, canvas.height)
let lasttime = 0
let enemyTimer = 0
let enemyInterval = 2000
let randomEnemyInterval = Math.random() * 1000 + 500
function animate(timeStamp) {
const deltaTime = timeStamp - lasttime
lasttime = timeStamp
ctx.clearRect(0, 0, canvas.width, canvas.height)
background.draw(ctx)
background.update()
player.draw(ctx)
player.update(input, deltaTime, enemies)
handleEnemies(deltaTime)
displayStatusText(ctx)
if (!gameOver) requestAnimationFrame(animate)
}
animate(0)
})