-
Notifications
You must be signed in to change notification settings - Fork 0
/
progetto.ino
265 lines (238 loc) · 8.61 KB
/
progetto.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
U8G2_SSD1327_WS_128X128_2_4W_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 7, 8);
// Obstacle data struct
typedef struct Obstacle {
int x, y;
} Obstacle;
// respectively current and last time in milliseconds, score and transition data
int current_time, last_time, score, x_transition;
// deltatime, pig's y position, and y acceleration
float delta, py, accy;
// auxiliary buffer
char buffer[15];
// transition flag
bool is_transition;
// obstacle
Obstacle obstacle;
// Game scene object
typedef struct Scene {
void (*init)();
void (*draw)();
void (*update)();
} Scene;
// main scenes used in this game
Scene menu, game, game_over ;
// current scene pointer
Scene * current_scene;
// flag that is true if this is the first draw call
bool first_render;
// flags respectively used to notify a point and a button press event by the user
bool point, last_press;
// pig bitmap
static unsigned char pig_bits[] = {
0x21, 0x72, 0xb4, 0xfe, 0x7f, 0x3f, 0x22, 0x22 };
// logo bitmap
static unsigned char Flappy_Pig_Logo_bits[] = {
0x00, 0xf0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x1f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xfe, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf8, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7e, 0x00, 0x00,
0x00, 0xc0, 0x07, 0xfe, 0x73, 0x1c, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
0x00, 0xe0, 0x01, 0xf0, 0x1f, 0xff, 0x73, 0x1c, 0x00, 0x00, 0x00, 0x00,
0xf0, 0xff, 0x00, 0xf0, 0x07, 0xf8, 0x5f, 0xff, 0x73, 0x1c, 0x00, 0x00,
0x00, 0x00, 0xf0, 0x07, 0x00, 0xf0, 0x0f, 0xf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 0x80, 0x03,
0xf8, 0x43, 0x04, 0x24, 0xf4, 0x04, 0xc0, 0x8f, 0x3f, 0xf0, 0x79, 0x1c,
0x00, 0x00, 0xf8, 0x60, 0x04, 0x24, 0x96, 0x04, 0xc0, 0x8f, 0x07, 0x70,
0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04, 0x26, 0xf2, 0x0c, 0x00, 0x80,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04, 0x22, 0x02, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04, 0x22,
0x7e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
0x07, 0x3e, 0xc0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0e, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xf2, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1e, 0x00 };
// change scene invoking init method
void change_scene(Scene * s){
current_scene = s;
current_scene->init();
}
// init menu
void init_menu(){
x_transition = 50, py = 80;
is_transition = false, first_render = true;
}
// init game
void init_game(){
py = 60, accy = 0, score = 0;
point = false, last_press = false;
u8g2.setFont(u8g2_font_6x12_tr);
obstacle = {128, 35 + rand()%50};
}
// init game over
void init_game_over(){
first_render = true;
}
float v = 0;
void update_menu(){
static float v = 0;
static int height = 86;
// if game button is pressed then start transitioning
if(digitalRead(2) == HIGH) is_transition = true;
// once the transition is finished change scene
if(is_transition && x_transition <= 10) {
change_scene(&game);
return;
}
// otherwise continue transitioning
else if(is_transition){
x_transition -= 5 * delta;
}
// pig vertical movement
if(py <= 77) height = 86;
else if(py >= 83) height = 74;
v = (height - py) / 4.f;
py += v * 10.f * delta;
}
void draw_menu(){
if(first_render) {
first_render = false;
u8g2.firstPage();
do {
u8g2.drawXBM(10, 0, 108, 39, Flappy_Pig_Logo_bits);
u8g2.setFont(u8g2_font_6x12_tr);
u8g2.drawStr(18,50,"Press to start");
} while(u8g2.nextPage());
}
else {
u8g2.customPage(7);
do {
u8g2.drawXBM(x_transition, py, 8, 8, pig_bits);
} while(u8g2.nextPage());
}
}
void update_game(){
// if there is a collision then change scene to GameOver
if(py > 127 || py < 0 || (18 > obstacle.x && 10 < obstacle.x + 10 && (py < obstacle.y - 24 || py+8 > obstacle.y + 24))){
change_scene(&game_over);
return;
}
// increase score and play the sound
if(10 > obstacle.x + 5 && !point){
point = true;
score++;
tone(9,1500,200);
}
// increase vertical acceleration
accy += 100.f * delta;
// if the game button is pressed then jump
int dr = digitalRead(2);
if (!last_press && dr == HIGH) {
last_press = true;
accy = -45.f;
}
else if(last_press && dr == LOW) {
last_press = false;
}
// change pig's y position
py += accy * delta;
if(obstacle.x + 5 <= 0){
obstacle = {128, 35 + rand()%50 };
point = false;
}
// horizontally move obstacle based on potentiometer value
obstacle.x -= map(analogRead(A0), 0, 1023, 1, 3) * 15.f * delta;
}
void draw_game(){
u8g2.firstPage();
do {
sprintf(buffer, "%d", score);
u8g2.drawStr(2,10,buffer);
u8g2.drawXBM(10, py, 8, 8, pig_bits);
u8g2.drawFrame(obstacle.x, -1, 10, obstacle.y - 25);
u8g2.drawFrame(obstacle.x, obstacle.y + 25, 10, 128);
} while ( u8g2.nextPage() );
}
void update_game_over(){
// if the game button is pressed then start a new game
if(digitalRead(2) == HIGH){
change_scene(&game);
}
}
void draw_game_over(){
if(first_render){
first_render = false;
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(10,24,"Game Over!");
sprintf(buffer, "Score: %d", score);
u8g2.drawStr(10,44, buffer);
u8g2.setFont(u8g2_font_6x12_tr);
u8g2.drawStr(10,64,"Premi il pulsante");
u8g2.drawStr(10,74,"per ricominciare");
} while ( u8g2.nextPage() );
// run "game over" song
tone(9,500,200);
delay(200);
tone(9,400,200);
delay(200);
tone(9,200,600);
delay(600);
}
}
void setup(void) {
u8g2.begin();
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(9, OUTPUT);
pinMode(A0, INPUT);
current_time = 0, last_time = 0;
delta = 0;
menu = { &init_menu, &update_menu, &draw_menu };
game_over = { &init_game_over, &update_game_over, &draw_game_over };
game = { &init_game, &update_game, &draw_game };
change_scene(&menu);
}
void loop(void) {
static float timer = 0;
last_time = current_time;
current_time = millis();
delta = (current_time - last_time) / 1000.f;
current_scene->update();
if(timer > 0.0625f){
timer = 0;
current_scene->draw();
}
timer += delta;
}