-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
161 lines (132 loc) · 3.25 KB
/
sketch.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
var GRAVITY = 0.5;
var FLAP_FORCE = -7;
var BIRD_RADIUS = 15;
var OBSTACLE_GAP_SIZE = 50;
var OBSTACLE_WIDTH = 70;
var OBSTACLE_SPEED = 2;
var BACKGROUND_COLOR = [0,0,0];
var BIRD_COLOR = [255,255,255];
var OBSTACLE_COLOR = [255,255,255];
class Bird {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.radius = r;
this.velocity = 0;
this.active = true;
}
Update() {
this.radius = BIRD_RADIUS;
// Update bird velocity
this.velocity += GRAVITY;
// Update bird position
this.y += this.velocity;
if (this.y >= height) {
this.y = height;
this.Die();
}
}
Draw() {
fill(BIRD_COLOR);
circle(this.x, this.y, this.radius);
}
Flap() {
if (!this.active)
return;
this.velocity = FLAP_FORCE;
}
Die() {
this.active = false;
}
}
class Obstacle {
constructor() {
this.mid = 0;
this.top = [0, 0, 0, 0]; // x,y,width,height
this.bottom = [0, 0, 0, 0];
this.x = width;
this.active = true;
this.passed = true;
}
RandomizePosition() {
this.passed = false;
this.x = width;
this.mid = Math.random() * ((height - OBSTACLE_GAP_SIZE) - OBSTACLE_GAP_SIZE + 1) + OBSTACLE_GAP_SIZE;
// Find top and bottom positions
this.topHeight = this.mid - OBSTACLE_GAP_SIZE;
this.bottomY = this.mid + OBSTACLE_GAP_SIZE;
this.bottomHeight = height - this.bottomY;
this.top = [this.x, 0, OBSTACLE_WIDTH, this.topHeight];
this.bottom = [this.x, this.bottomY, OBSTACLE_WIDTH, this.bottomHeight];
}
Update() {
if (!this.active)
return;
this.x -= OBSTACLE_SPEED;
if (this.x <= -OBSTACLE_WIDTH)
this.RandomizePosition();
}
Draw() {
fill(OBSTACLE_COLOR);
rect(this.x, this.top[1], this.top[2], this.top[3]);
rect(this.x, this.bottom[1], this.bottom[2], this.bottom[3]);
}
DoesCollide(x, y, r) {
return collideRectCircle(this.x, this.top[1], this.top[2], this.top[3], x, y, r) ||
collideRectCircle(this.x, this.bottom[1], this.bottom[2], this.bottom[3], x, y, r);
}
}
// Create BIRD
var bird;
// Create Obstacle
var obstacle;
var start = true;
let score = 0;
function setup() {
start = true;
score = 0;
document.getElementById("score").innerHTML = score.toString();
var canvas = createCanvas(300, 500);
canvas.parent("canvas");
bird = new Bird(width / 4, height / 2, BIRD_RADIUS);
obstacle = new Obstacle();
}
function draw() {
background(BACKGROUND_COLOR);
textSize(width / 20);
if (start) {
fill(BIRD_COLOR);
circle(bird.x, bird.y, BIRD_RADIUS);
bird.y += Math.sin(millis() / 200);
} else {
bird.Update();
bird.Draw();
obstacle.Update();
obstacle.Draw();
if (obstacle.DoesCollide(bird.x, bird.y, bird.radius)) {
bird.Die();
}
obstacle.active = bird.active;
if (obstacle.x < bird.x && bird.active && !obstacle.passed) {
obstacle.passed = true;
score++;
document.getElementById("score").innerHTML = score.toString();
}
fill(255, 255, 255);
if (!bird.active)
text("Press Enter to respawn", width/4, height / 2);
}
}
function keyPressed() {
switch (key) {
case " ":
if (start) {
start = false;
}
bird.Flap();
break;
case "Enter":
setup();
break;
}
}