This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappy.js
75 lines (63 loc) · 1.75 KB
/
flappy.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
function Flappy() {
var location = createVector(width / 4, height / 2);
var speed = createVector(0, 0);
var acceleration = createVector(0, 0);
var bird = assets.bird;
var sprites = bird.sprites;
var gravity = createVector(0, 0.15);
this.flap = function() {
if (!canFlap) {
return false;
}
if (location.y <= 0) {
return false;
}
if (speed.mag() > 0) {
speed.mult(0);
}
acceleration.add(createVector(0, -3));
}
this.update = function() {
speed.add(acceleration);
speed.add(gravity);
speed.limit(3);
acceleration.mult(0);
if (location.y > height - 111 - 11) {
location.y = height - 111 - 11;
speed.mult(0);
gameOver();
return;
}
location.add(speed);
}
this.draw = function() {
var sprite = sprites[frameCount % 3];
if (speed.y > 1) {
sprite = sprites[0];
}
push();
translate(location.x, location.y);
rotate(map(speed.y, -3, 3, -0.9, 0.9));
image(img, -bird.w / 2, -bird.h / 2, bird.w, bird.h, sprite.x, sprite.y, bird.w, bird.h);
pop();
if (debug) {
var b = this.bounds();
push();
noFill();
stroke(0, 0, 255);
rect(b.left, b.top, b.right - b.left, b.bottom - b.top);
pop();
}
}
this.bounds = function() {
return {
left: location.x - bird.w / 8,
right: location.x + bird.w / 8,
top: location.y - bird.h / 8,
bottom: location.y + bird.h / 8
};
}
this.pos = function() {
return location;
}
}