-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappyBird.html
180 lines (138 loc) · 3.54 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Canvas Sandbox</title>
</head>
<body>
<canvas id="Canvas" width="1000" height="500"></canvas>
<script>
var lastTime = Date.now();
var TIME_INCREMENT = 1;
var x = 0;
var y = 0;
var vy = 0;
var ay = 0;
var rotation = 0;
var gravidade = 0.0018;
var impulso = 0.4;
var distanciaPilares = 200;
var pilares = [];
var elem = document.getElementById('Canvas');
var context = elem.getContext('2d');
(function() {
var requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
function redraw () {
var currentTime = Date.now();
var lag = currentTime - lastTime;
while (lag >= TIME_INCREMENT) {
update();
lag -= TIME_INCREMENT;
}
lastTime = currentTime;
draw();
window.requestAnimationFrame(redraw);
}
var sentidoAtual = -1;
function update() {
ay = getAcceleration();
vy = getVelocity();
y = getPosition();
rotation = getRotation();
if (pilares.length == 0 ||
pilares[pilares.length-1].posicao < 800 - distanciaPilares)
{
var novo = {
posicao: 800,
sentido: sentidoAtual,
altura: Math.random() * 100
};
pilares.push(novo);
sentidoAtual *= -1;
}
for (var i = 0; i < pilares.length; i++) {
var pilar = pilares[i];
if (pilar.posicao < -80)
pilares.splice(i, 1);
else {
pilar.posicao -= 0.12;
}
}
}
function getAcceleration() {
return gravidade;
}
function getVelocity() {
var v = vy;
if (y > 0)
v -= ay;
return v;
}
function getPosition() {
var pos = y + vy;
if (pos < 0) {
pos = 0;
vy = 0;
}
return pos;
}
function getRotation() {
return Math.atan2(vy, 0.5);
}
function draw() {
context.save();
context.fillStyle = '#fdc';
context.fillRect(0,0,elem.width, elem.height);
context.translate(100, elem.height * 0.8);
context.scale(1, -1);
// desenha quadradinho
context.save();
context.translate(x,y);
context.rotate(rotation);
context.fillStyle = '#00f';
var rh = 15;
var rv = 10;
context.fillRect(-rh, -rv, 2*rh, 2*rv);
context.restore();
// desenha pilares
for (var i = 0; i < pilares.length; i++) {
var pilar = pilares[i];
var inicio = pilar.altura;
var fim = pilar.sentido < 0 ? 0 : 500;
context.beginPath();
context.moveTo(pilar.posicao, inicio);
context.lineTo(pilar.posicao, fim);
context.stroke();
}
context.restore();
}
window.requestAnimationFrame(redraw);
window.addEventListener("keydown", onKeyDown, false);
window.addEventListener("touchstart", onTouchStart, false);
function onTouchStart(event) {
vy = impulso;
var jumpSound = new Audio('sfx_wing.ogg');
jumpSound.volume = 0.7;
jumpSound.play();
}
function onKeyDown(event) {
var keyCode = event.keyCode;
switch (keyCode) {
case 87: //w
case 38: //arrowUp
var jumpSound = new Audio('sfx_wing.ogg');
jumpSound.volume = 0.7;
jumpSound.play();
vy = impulso;
break;
}
}
</script>
</body>
</html>