forked from Lakshya-Coder/Flappy-Bird-With-ESP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy_bird.ino
174 lines (137 loc) · 3.48 KB
/
flappy_bird.ino
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
#include <Wire.h>
#include "SH1106Wire.h"
#include "images.h"
#define MAIN_MENU_SCREEN 0
#define GAMEPLAY_SCREEN 1
#define GAMEOVER_SCREEN 2
#define BIRD_WIDTH 14
#define BIRD_HEIGHT 9
SH1106Wire display(0x3c, D1, D2);
float wall[4];
int empty[4];
int space = 32;
int widthOfPassage = 30;
int gameState = MAIN_MENU_SCREEN;
const int buttonPin = D3;
int buttonState = 0;
int score = 0;
int isJumping = 0;
int currentTime = 0;
int direction = 1;
int play = 0;
float birdX = 5.00;
float birdY = 22.00;
unsigned long ton = 0;
void setup() {
display.init();
display.flipScreenVertically();
generateWalls();
display.drawRect(0, 0, 128, 63);
delay(634);
pinMode(buttonPin, INPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
display.clear();
buttonState = digitalRead(buttonPin);
if (gameState == MAIN_MENU_SCREEN) {
showMainMenuScreen();
} else if (gameState == GAMEPLAY_SCREEN) {
showGamePlayScreen();
} else if (gameState == GAMEOVER_SCREEN) {
showGameOverScreen();
}
display.display();
}
void showMainMenuScreen() {
display.setFont(ArialMT_Plain_16);
display.drawString(0, 4, "Flappy ");
display.drawXbm(0, 0, 128, 64, background);
drawBird(20, 32);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 44, "Press to start");
if (buttonState == HIGH) {
gameState = GAMEPLAY_SCREEN;
}
}
void showGamePlayScreen() {
int val = analogRead(A0) * 12.5 / 100;
display.setFont(ArialMT_Plain_10);
display.drawString(3, 0, String(score));
if (buttonState == HIGH) {
if (isJumping == 0) {
currentTime = millis();
ton = millis();
direction = 1;
play = 1;
isJumping = 1;
}
} else {
isJumping = 0;
}
for (int i = 0; i < 4; i++) {
// Draw the wall
display.setColor(WHITE);
display.fillRect(wall[i], 0, 6, 64);
display.setColor(BLACK);
display.fillRect(wall[i], empty[i], 6, widthOfPassage);
// Move the wall and check if need to move the position of the wall
wall[i] -= 0.01;
if (wall[i] < 0) {
score = score + 1;
empty[i] = random(8, 32);
wall[i] = 128 + val;
}
// Check for collision
if (wall[i] <= birdX + 7 && birdX + 7 <= wall[i] + 6) {
if (birdY < empty[i] || birdY + BIRD_HEIGHT > empty[i] + widthOfPassage) {
gmaeOver();
}
}
}
drawBird(birdX, birdY);
if ((currentTime + 185) < millis()) {
direction = 0;
}
if ((ton + 40) < millis()) {
play = 0;
}
if (direction == 0) {
birdY += 0.01;
} else {
birdY -= 0.03;
}
if (birdY >= 64 || birdY < -3) { // Check if the bird collided with the upper bound or the lower bound
gmaeOver();
}
display.drawRect(0, 0, 128, 63);
}
void showGameOverScreen() {
display.setFont(ArialMT_Plain_16);
display.drawString(0, 4, "Score: ");
display.drawXbm(0, 0, 128, 64, background);
display.drawString(20, 25, String(score));
display.setFont(ArialMT_Plain_10);
display.drawString(0, 44, "Press to start");
if (buttonState == HIGH) {
score = 0;
gameState = GAMEPLAY_SCREEN;
}
}
void drawBird(int x, int y) {
display.setColor(WHITE);
display.drawXbm(x, y, BIRD_WIDTH, BIRD_HEIGHT, bird);
}
void gmaeOver() {
gameState = GAMEOVER_SCREEN;
birdY = 22;
delay(500);
generateWalls();
}
void generateWalls() {
for (int i = 0; i < 4; i++) {
wall[i] = 128 + ((i + 1) * space);
empty[i] = random(8, 32);
}
}