-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappyBird.html
152 lines (115 loc) · 3.32 KB
/
flappyBird.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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Flappy bird</title>
<style media="screen">
canvas {
display: block;
margin: 0 auto;
background-color: skyblue;
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="500"></canvas>
<script type="text/javascript">
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
var poeng=0;
class Player {
constructor(x,y, yFart) {
this.x=x;
this.y=y
this.yFart=yFart;
}
move() {
this.yFart+=0.2;
this.y+=this.yFart;
}
draw() {
ctx.fillRect(this.x, this.y, 50, 50);
}
}
class Hurdle {
constructor(x,y, height) {
this.x=x
this.y=y
this.height=height
}
move() {
this.x-=4
}
draw() {
ctx.fillRect(this.x, this.y, bredde, this.height);
}
}
var bredde=50;
var minHeight=10;
var opening=150;
var hurdleArr=[];
var bird=new Player(200,100,0);
ctx.fillRect(200, 100, 50, 50);
var animationId;
var stopAni=0;
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
bird.draw()
bird.move()
for (var i = 0; i < hurdleArr.length; i++) {
hurdleArr[i].draw()
hurdleArr[i].move()
}
for (var i = 0; i < hurdleArr.length; i++) {
if ((i==0 || i%2==0) && bird.x+40>hurdleArr[i].x && bird.x<hurdleArr[i].x+bredde && bird.y+5<hurdleArr[i].height) {
stopAni++;
console.log("hei");
} else if ((i!=0 || i%2!=0) && bird.x+40>hurdleArr[i].x && bird.x<hurdleArr[i].x+bredde && bird.y+40>canvas.height-hurdleArr[i].height) {
stopAni++;
console.log(bird.y+40);
console.log(canvas.height-hurdleArr[i].y);
}
}
if (hurdleArr[0].x+5<0) {
poeng++;
hurdleArr.shift()
hurdleArr.shift()
}
if (bird.y<-2 || bird.y>canvas.height-38) {
stopAni++;
}
if (stopAni==0) {
animationId=requestAnimationFrame(animate)
} else {
animationId=cancelAnimationFrame(animate)
console.log(poeng);
}
}
var bodyEl=document.querySelector("body")
bodyEl.addEventListener("keydown", flappyFun)
function flappyFun() {
console.log("hei");
bird.yFart-=5;
}
var start=0;
function newHurdle() {
var randNum=canvas.height-opening-2*minHeight;
var random=Math.ceil(Math.random()*randNum);
var heightt=minHeight+random;
var heightb=minHeight+randNum-random;
var startB=heightt+opening;
var hurdleT=new Hurdle(canvas.width, 0, heightt)
var hurdleB=new Hurdle(canvas.width, canvas.height-heightb, heightb)
hurdleArr.push(hurdleT)
hurdleArr.push(hurdleB)
start++;
if (start==1) {
animationId = requestAnimationFrame(animate);
}
}
newHurdle()
setInterval(newHurdle, 1500)
</script>
</body>
</html>