-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlappy_Bird.pde
195 lines (186 loc) · 4.08 KB
/
Flappy_Bird.pde
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Bird flappy;
float g;
int speed;
Stage level;
int pipeWidth;
int pipes_to_the_left; // for keeping track of score.
boolean running;
void setup() {
running = false;
size(400, 600);
background(75, 200, 225);
g = 0.1;
speed = 2;
pipeWidth = 40;
flappy = new Bird(100, 290, 240, 245, 55);
level = new Stage();
}
void draw() {
if (running){
background(75, 200, 225);
level.advance();
flappy.update();
flappy.drawMe(); // draw the bird after drawing the level, to make the bird be in front.
if (level.checkCollisions(flappy)) {
gameOver(level, flappy);
}
level.removeOldPipes();
// print score to screen
fill(255,255,255);
textSize(30);
text(flappy.pipesPassed, width / 2, height * 0.1 );
}
else {
background(75, 200, 225);
fill(255,255,255);
textSize(30);
text("Click to Start!", width / 4, height / 10);
}
}
void mousePressed() {
if (running){
flappy.flap();
}
else {
running = true;
}
}
class Bird {
int x;
int y;
color col;
float velY;
int birdWidth;
int birdHeight;
int pipesPassed = 0;
Bird(int posX, int posY, int rcol, int gcol, int bcol) {
x = posX;
y = posY;
birdWidth = 20;
birdHeight = 20;
col = color(rcol, gcol, bcol);
}
void drawMe() {
fill(col);
rect(x, y, birdWidth, birdHeight);
}
void update() {
velY = velY + g;
y += velY;
// track score
int count = 0;
for (Pipe p : level.pipeList){
if (p.x + pipeWidth/2 < this.x) count++;
}
if (count > pipes_to_the_left){
pipesPassed++;
}
pipes_to_the_left = count;
}
void flap() {
velY = -30*g;
y += velY;
}
}
class Pipe {
int x;
int gapSize;
int start;
int min_gapSize;
int max_gapSize;
int smin;
int smax;
Pipe(int pos) {
min_gapSize = 100;
max_gapSize = 100;
smin = 100;
smax = 500;
gapSize = floor(random(min_gapSize, max_gapSize+1));
start = floor(random(smin, smax-gapSize+1));
x = pos;
}
void drawMe() {
fill(0, 255, 0);
rect(x, 0, pipeWidth, start);
rect(x, start+gapSize, pipeWidth, height-start+gapSize);
}
void update() {
x -= speed;
}
//checks collision with a point x,y
boolean checkCollision(int birdX, int birdY) {
if (birdX >= x && birdX <= x+pipeWidth) {
//top part
if (birdY >= 0 && birdY <= start) {
return true;
}
//bottom part
if (birdY >= start+gapSize && birdY <= height) {
return true;
}
}
return false;
}
}
class Stage {
int tick;
int tickInt;
int tickLimit;
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
Stage() {
tick = 0;
tickLimit = speed*50;
}
void advance() {
if (tick >= tickLimit || tick == 0) {
tick = 0;
tickInt = 1;
pipeList.add(new Pipe(width));
}
tick += tickInt;
for (int i=0; i<pipeList.size (); i++) {
Pipe p = pipeList.get(i);
p.update();
p.drawMe();
}
}
void removeOldPipes() {
for (int i=0; i<pipeList.size (); i++) {
Pipe p = pipeList.get(i);
if (p.x <= 0-pipeWidth) {
pipeList.remove(p);
pipes_to_the_left--;
}
}
}
boolean checkCollisions(Bird bird) {
boolean c = false;
if (bird.y > height - bird.birdHeight) {
bird.y = height - bird.birdHeight;
bird.velY = 0;
c = true;
}
if (bird.y < 0) {
bird.y = 0;
bird.velY = 0;
}
//collision check algorithm
//for each pipe check collision
for (int i=0; i<pipeList.size (); i++) {
Pipe p = pipeList.get(i);
c = c || p.checkCollision(bird.x, bird.y);
c = c || p.checkCollision(bird.x + bird.birdWidth, bird.y);
c = c || p.checkCollision(bird.x, bird.y + bird.birdHeight);
c = c || p.checkCollision(bird.x + bird.birdWidth, bird.y + bird.birdHeight);
}
return c;
}
}
void gameOver(Stage stage, Bird bird) {
noLoop(); // note: this will stop the draw loop from running. It will also cause the program to stop recognizing mouse/keyboard events.
running = false;
speed = 0;
g = 0;
bird.velY = 0;
stage.tickInt = 0;
}