-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.c
225 lines (196 loc) · 5.3 KB
/
flappy.c
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
/* flappy bird */
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "matelight.h"
#define MODE_GAME 0
#define MODE_DEAD 1
#define NUM_PIPES (grid_width / 3)
#define MAX_NUM_PIPES (MAX_GRID_WIDTH / 3)
#define PIPE_WIDTH 2
#define PIPE_SEPARATOR 7
#define FLOOR_HEIGHT 2
#define BIRD_X 2
#define START_OFFSET grid_width
static int game_mode = MODE_DEAD;
static int game_pause = false;
static int button = 0;
static int bird_y = 0;
static int pipes_high[MAX_NUM_PIPES] = { 0 };
static int pipes_low[MAX_NUM_PIPES] = { 0 };
static int pipes_off = 0;
static void setup_game(bool start)
{
int i, r;
game_mode = start ? MODE_GAME : MODE_DEAD;
game_pause = false;
button = 0;
bird_y = (grid_height / 2);
for (i = 0; i < NUM_PIPES; i++) {
r = rand() % ((grid_height - FLOOR_HEIGHT) - (grid_height / 2));
pipes_high[i] = 2 + r;
pipes_low[i] = 2 + r + 8;
}
pipes_off = START_OFFSET;
}
static void activate(bool start)
{
setup_game(start);
}
static void deactivate(void)
{
setup_game(false);
}
static bool doit(void)
{
int r;
int x;
int ix;
int pipe_idx;
if (game_pause)
return true;
// move bird
if (button) {
bird_y--;
button--;
} else {
bird_y++;
}
while (bird_y < 0) {
bird_y++;
}
if ((bird_y + 1) >= (grid_height - FLOOR_HEIGHT)) {
while ((bird_y + 1) >= grid_height) {
bird_y--;
}
return false;
}
// move pipes
pipes_off--;
if ((pipes_off + PIPE_WIDTH) < 0) {
memmove(pipes_high, &pipes_high[1], sizeof(pipes_high[0]) * (NUM_PIPES - 1));
memmove(pipes_low, &pipes_low[1], sizeof(pipes_low[0]) * (NUM_PIPES - 1));
pipes_off += (PIPE_WIDTH + PIPE_SEPARATOR);
r = rand() % ((grid_height - FLOOR_HEIGHT) - (grid_height / 2));
pipes_high[NUM_PIPES - 1] = 2 + r;
pipes_low[NUM_PIPES - 1] = 2 + r + 8;
}
// collision detection
for (pipe_idx = 0; pipe_idx < NUM_PIPES; pipe_idx++) {
for (ix = 0; ix < PIPE_WIDTH; ix++) {
x = pipes_off + (pipe_idx * (PIPE_WIDTH + PIPE_SEPARATOR)) + ix;
if (x == BIRD_X || x == (BIRD_X + 1)) {
if (bird_y <= pipes_high[pipe_idx] || (bird_y + 1) >= pipes_low[pipe_idx]) {
return false;
}
}
}
}
return true;
}
static void key_button(void)
{
button = 4;
}
static void tick(void)
{
if (game_mode != MODE_GAME) return;
if (! doit())
game_mode = MODE_DEAD;
}
static void input(int player, int key_idx, bool key_val, int key_state)
{
(void)player;
(void)key_state;
switch (game_mode) {
case MODE_GAME:
if (key_idx == KEYPAD_START && key_val) {
if (! game_pause) {
game_pause = true;
} else {
game_pause = false;
}
}
if ((key_idx == KEYPAD_B || key_idx == KEYPAD_A) && key_val) {
key_button();
}
break;
case MODE_DEAD:
switch (key_idx) {
case KEYPAD_SELECT:
case KEYPAD_START:
case KEYPAD_B:
case KEYPAD_A:
if (key_val) {
setup_game(true);
}
break;
default:
break;
}
}
}
static void draw(char *screen)
{
int x, y;
int ix;
int pipe_idx;
// background and floor
for (y = 0; y < grid_height; y++) {
for (x = 0; x < grid_width; x++) {
if (y < (grid_height - FLOOR_HEIGHT)) {
set_pixel(screen, y, x, COLOR_BLUE);
} else {
set_pixel(screen, y, x, 0x964b00);
}
}
}
// pipes
for (pipe_idx = 0; pipe_idx < NUM_PIPES; pipe_idx++) {
for (ix = 0; ix < PIPE_WIDTH; ix++) {
x = pipes_off + (pipe_idx * (PIPE_WIDTH + PIPE_SEPARATOR)) + ix;
if (x >= 0 && x < grid_width) {
for (y = 0; y < pipes_high[pipe_idx]; y++) {
set_pixel(screen, y, x, 0x1fd655);
}
set_pixel(screen, pipes_high[pipe_idx], x, 0x83f28f);
set_pixel(screen, pipes_low[pipe_idx], x, 0x83f28f);
for (y = pipes_low[pipe_idx] + 1; y < (grid_height - FLOOR_HEIGHT); y++) {
set_pixel(screen, y, x, 0x1fd655);
}
}
}
}
// bird
set_pixel(screen, bird_y + 0, BIRD_X + 0, COLOR_YELLOW);
set_pixel(screen, bird_y + 1, BIRD_X + 0, COLOR_YELLOW);
set_pixel(screen, bird_y + 0, BIRD_X + 1, COLOR_WHITE);
set_pixel(screen, bird_y + 1, BIRD_X + 1, COLOR_RED);
}
static void render(bool *display, char *screen)
{
if (game_mode == MODE_GAME) {
*display = true;
draw(screen);
} else {
*display = false;
}
}
static bool idle(void)
{
return game_mode != MODE_GAME;
}
const struct game flappy_game = {
"flappy bird",
true,
false,
0.1,
NULL,
activate,
deactivate,
input,
tick,
render,
idle,
};