-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.js
More file actions
78 lines (77 loc) · 1.91 KB
/
class.js
File metadata and controls
78 lines (77 loc) · 1.91 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
class Star {
constructor() {
this.x = Math.random() * canvas.width
this.y = Math.random() * canvas.height
this.radius = Math.random() * 2
}
update() {
this.draw()
this.x -= 2 * GAMESPEED
if (this.x + this.radius < 0) {
this.x = canvas.width
this.y = Math.random() * canvas.height
}
}
draw() {
c.beginPath()
c.fillStyle = "white"
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
c.fill()
}
}
class Pillar {
constructor() {
this.x = canvas.width//+Math.random()*canvas.width*.5
this.gapOffset = (Math.random() * canvas.height * .4) + (canvas.height * .2)
this.color = "#fff"
this.width = 30
}
update() {
this.draw()
this.x -= 4 * GAMESPEED
//Gameover code || Collision Detection
if (this.x + this.width >= bird.x && this.x < bird.x + bird.length) {
if (bird.y < this.gapOffset || bird.y + bird.length > this.gapOffset + PILLARGAP) {
gameOver()
}
}
}
draw() {
c.fillStyle = this.color
c.fillRect(this.x, 0, this.width, this.gapOffset) // top pillar
c.fillRect(this.x, this.gapOffset + PILLARGAP, this.width, canvas.height - (this.gapOffset + PILLARGAP)) // bottom pillar
}
}
class Player {
constructor() {
this.x = 50
this.y = canvas.height / 2
this.vy = 0
this.acceleration = 0.8
this.friction = 0.07
this.length = 30
this.color = "#00ffd1"
}
update() {
this.draw()
if (this.y > canvas.height - this.length) {
this.y = canvas.height - this.length
this.vy = 0
} else if (this.y < 0) {
this.y = 0
this.vy = 0
} else {
this.vy += this.acceleration
this.vy *= 1 - this.friction
this.y += this.vy
}
}
draw() {
c.fillStyle = this.color
c.fillRect(this.x, this.y, this.length, this.length)
c.strokeRect(this.x, this.y, this.length, this.length)
}
flap() {
this.vy -= FLAPSPEED * GAMESPEED
}
}