Update Flappy Bird game.

This commit is contained in:
Serge Vakulenko
2015-11-22 14:34:44 -08:00
parent 7503ce7856
commit e3c86f0ffa
5 changed files with 8322 additions and 40 deletions

View File

@@ -1131,6 +1131,7 @@ file /share/examples/gpanel/fonts/digits20.c
file /share/examples/gpanel/fonts/digits32.c file /share/examples/gpanel/fonts/digits32.c
file /share/examples/gpanel/fonts/lucidasans11.c file /share/examples/gpanel/fonts/lucidasans11.c
file /share/examples/gpanel/fonts/lucidasans15.c file /share/examples/gpanel/fonts/lucidasans15.c
file /share/examples/gpanel/fonts/lucidasans28.c
file /share/examples/gpanel/fonts/lucidasans7.c file /share/examples/gpanel/fonts/lucidasans7.c
file /share/examples/gpanel/fonts/lucidasans9.c file /share/examples/gpanel/fonts/lucidasans9.c
file /share/examples/gpanel/fonts/verdana7.c file /share/examples/gpanel/fonts/verdana7.c

View File

@@ -3,7 +3,7 @@ LIBS = -lgpanel
PROG = tft tftetris pixel line rect fill circle font color speed flappy PROG = tft tftetris pixel line rect fill circle font color speed flappy
FONTS = 5x7.o 6x9.o digits20.o digits32.o lucidasans11.o lucidasans15.o \ FONTS = 5x7.o 6x9.o digits20.o digits32.o lucidasans11.o lucidasans15.o \
lucidasans7.o lucidasans9.o verdana7.o lucidasans28.o lucidasans7.o lucidasans9.o verdana7.o
all: $(PROG) all: $(PROG)
@@ -37,8 +37,8 @@ color: color.c
speed: speed.c lucidasans15.o speed: speed.c lucidasans15.o
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) speed.c lucidasans15.o $(LIBS) $(CC) $(CFLAGS) -o $@ $(LDFLAGS) speed.c lucidasans15.o $(LIBS)
flappy: flappy.c lucidasans15.o flappy: flappy.c lucidasans15.o lucidasans28.o
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) flappy.c lucidasans15.o $(LIBS) $(CC) $(CFLAGS) -o $@ $(LDFLAGS) flappy.c lucidasans15.o lucidasans28.o $(LIBS)
font: font.c $(FONTS) font: font.c $(FONTS)
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) font.c $(FONTS) $(LIBS) $(CC) $(CFLAGS) -o $@ $(LDFLAGS) font.c $(FONTS) $(LIBS)
@@ -61,6 +61,9 @@ lucidasans11.o: fonts/lucidasans11.c
lucidasans15.o: fonts/lucidasans15.c lucidasans15.o: fonts/lucidasans15.c
$(CC) $(CFLAGS) -o $@ -c $? $(CC) $(CFLAGS) -o $@ -c $?
lucidasans28.o: fonts/lucidasans28.c
$(CC) $(CFLAGS) -o $@ -c $?
lucidasans7.o: fonts/lucidasans7.c lucidasans7.o: fonts/lucidasans7.c
$(CC) $(CFLAGS) -o $@ -c $? $(CC) $(CFLAGS) -o $@ -c $?

View File

@@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <signal.h>
#include <sys/gpanel.h> #include <sys/gpanel.h>
#include <sys/time.h> #include <sys/time.h>
#ifdef CROSS #ifdef CROSS
@@ -30,6 +31,7 @@
* Data from external font files. * Data from external font files.
*/ */
extern const struct gpanel_font_t font_lucidasans15; extern const struct gpanel_font_t font_lucidasans15;
extern const struct gpanel_font_t font_lucidasans28;
int wing; int wing;
int fx, fy, fallRate; int fx, fy, fallRate;
@@ -106,10 +108,10 @@ void drawFlappy(int x, int y)
fill(BLACK, x+18, y+2, 2, 2); fill(BLACK, x+18, y+2, 2, 2);
fill(BLACK, x+16, y+4, 2, 6); fill(BLACK, x+16, y+4, 2, 6);
fill(BLACK, x+18, y+10, 2, 2); fill(BLACK, x+18, y+10, 2, 2);
fill(BLACK, x+18, y+4, 2, 6); fill(WHITE, x+18, y+4, 2, 6);
fill(BLACK, x+20, y+2, 4, 10); fill(WHITE, x+20, y+2, 4, 10);
fill(BLACK, x+24, y+4, 2, 8); fill(WHITE, x+24, y+4, 2, 8);
fill(BLACK, x+26, y+6, 2, 6); fill(WHITE, x+26, y+6, 2, 6);
fill(BLACK, x+24, y+6, 2, 4); fill(BLACK, x+24, y+6, 2, 4);
// Beak // Beak
@@ -176,22 +178,24 @@ void startGame()
{ {
fx = 50; fx = 50;
fy = 125; fy = 125;
fallRate = 1; fallRate = -1;
pillarPos = 320; pillarPos = 320;
gapPos = 60; gapPos = 60;
crashed = 0; crashed = 0;
score = 0; score = 0;
gpanel_clear(BLUE, 0, 0); gpanel_clear(BLUE, 0, 0);
gpanel_text(&font_lucidasans15, WHITE, BLUE, 5, 5, gpanel_text(&font_lucidasans28, WHITE, BLUE, 10, 10,
"Flappy Bird: tap to begin"); "Flappy Bird");
gpanel_text(&font_lucidasans15, WHITE, BLUE, 50, 180,
"(Press Space to start)");
//TODO: read high score value from file. //TODO: read high score value from file.
highScore = 0; highScore = 0;
char scoreLine[80]; char scoreLine[80];
sprintf(scoreLine, "High Score : %u", highScore); sprintf(scoreLine, "High Score: %u", highScore);
gpanel_text(&font_lucidasans15, GREEN, BLUE, 60, 60, scoreLine); gpanel_text(&font_lucidasans28, GREEN, BLUE, 10, 60, scoreLine);
// Draw Ground // Draw Ground
int tx, ty = 230; int tx, ty = 230;
@@ -201,6 +205,8 @@ void startGame()
gpanel_fill_triangle(YELLOW, tx+10, ty, tx+20, ty, tx+10, ty+10); gpanel_fill_triangle(YELLOW, tx+10, ty, tx+20, ty, tx+10, ty+10);
gpanel_fill_triangle(GREEN, tx+20, ty+10, tx+20, ty, tx+10, ty+10); gpanel_fill_triangle(GREEN, tx+20, ty+10, tx+20, ty, tx+10, ty+10);
} }
nextDrawLoopRunTime = millis() + DRAW_LOOP_INTERVAL;
} }
void drawLoop() void drawLoop()
@@ -214,7 +220,7 @@ void drawLoop()
fy += fallRate; fy += fallRate;
fallRate++; fallRate++;
pillarPos -=5; pillarPos -= 5;
if (pillarPos == 0) { if (pillarPos == 0) {
score++; score++;
} }
@@ -258,17 +264,15 @@ void checkCollision()
crashed = 1; crashed = 1;
if (crashed) { if (crashed) {
//TODO: use larger font. gpanel_text(&font_lucidasans28, RED, BLUE, 50, 50, "Game Over!");
gpanel_text(&font_lucidasans15, RED, BLUE, 75, 75, "Game Over!");
char scoreLine[80]; char scoreLine[80];
sprintf(scoreLine, "%u", score); sprintf(scoreLine, "Score: %u", score);
gpanel_text(&font_lucidasans15, RED, BLUE, 75, 125, "Score:"); gpanel_text(&font_lucidasans28, RED, BLUE, 50, 100, scoreLine);
gpanel_text(&font_lucidasans15, RED, BLUE, 220, 125, scoreLine);
if (score > highScore) { if (score > highScore) {
highScore = score; highScore = score;
gpanel_text(&font_lucidasans15, RED, BLUE, 75, 175, "NEW HIGH!"); gpanel_text(&font_lucidasans28, RED, BLUE, 50, 150, "NEW HIGH!");
//TODO: Write high score value to file. //TODO: Write high score value to file.
} }
@@ -284,15 +288,15 @@ void checkCollision()
#if 1 #if 1
struct sgttyb origtty, newtty; struct sgttyb origtty, newtty;
int restore_input() /*
* Terminate the game when ^C pressed.
*/
void quit(int sig)
{ {
#ifdef CROSS signal(SIGINT, SIG_IGN);
if (newtty.c_cc[VMIN] != 0)
ioctl(0, TCSETAW, &origtty);
#else
if (newtty.sg_flags != 0) if (newtty.sg_flags != 0)
ioctl(0, TIOCSETP, &origtty); ioctl(0, TIOCSETP, &origtty);
#endif exit(0);
} }
/* /*
@@ -300,25 +304,14 @@ int restore_input()
*/ */
int get_input() int get_input()
{ {
#ifdef CROSS
if (newtty.c_cc[VMIN] == 0) {
ioctl(0, TCGETA, &origtty);
newtty = origtty;
newtty.c_lflag &= ~(ICANON | ECHO);
newtty.c_oflag &= ~ONLCR;
newtty.c_cc[VMIN] = 1;
newtty.c_cc[VTIME] = 0;
ioctl(0, TCSETAW, &newtty);
}
#else
if (newtty.sg_flags == 0) { if (newtty.sg_flags == 0) {
ioctl(0, TIOCGETP, &origtty); ioctl(0, TIOCGETP, &origtty);
newtty = origtty; newtty = origtty;
newtty.sg_flags &= ~(ECHO|CRMOD|XTABS); newtty.sg_flags &= ~(ECHO|CRMOD|XTABS);
newtty.sg_flags |= CBREAK; newtty.sg_flags |= CBREAK;
ioctl(0, TIOCSETP, &newtty); ioctl(0, TIOCSETP, &newtty);
} }
#endif
int nchars = 0; int nchars = 0;
ioctl(0, FIONREAD, &nchars); ioctl(0, FIONREAD, &nchars);
if (nchars <= 0) if (nchars <= 0)
@@ -342,13 +335,15 @@ int main()
gpanel_clear(BLUE, &xsize, &ysize); gpanel_clear(BLUE, &xsize, &ysize);
startGame(); startGame();
nextDrawLoopRunTime = millis() + DRAW_LOOP_INTERVAL; signal(SIGINT, quit);
for (;;) { for (;;) {
if (millis() > nextDrawLoopRunTime && !crashed) { if (millis() > nextDrawLoopRunTime && !crashed) {
drawLoop(); drawLoop();
checkCollision(); checkCollision();
nextDrawLoopRunTime += DRAW_LOOP_INTERVAL; nextDrawLoopRunTime += DRAW_LOOP_INTERVAL;
} }
usleep(10000);
// Get user input. // Get user input.
int user_input = get_input(); int user_input = get_input();
@@ -361,7 +356,8 @@ int main()
} }
else if (!running) { else if (!running) {
// clear text & start scrolling // clear text & start scrolling
gpanel_fill(BLUE, 0, 0, 320-1, 80); gpanel_fill(BLUE, 0, 0, 320-1, 100);
gpanel_fill(BLUE, 0, 180, 320-1, 205);
running = 1; running = 1;
} else { } else {
// fly up // fly up

View File

@@ -1,5 +1,5 @@
CFLAGS = -Wall -O -g CFLAGS = -Wall -O -g
LDFLAGS = -lpng LDFLAGS = -L/opt/local/lib -lpng
FONTS = digits32.c \ FONTS = digits32.c \
digits20.c \ digits20.c \
@@ -47,6 +47,11 @@ lucidasans15.c: convbdf ttf/Lucida_Sans_Unicode.ttf
./convbdf -u164 -a5 -d5 lucidasans15.bdf ./convbdf -u164 -a5 -d5 lucidasans15.bdf
rm -f lucidasans15.bdf rm -f lucidasans15.bdf
lucidasans28.c: convbdf ttf/Lucida_Sans_Unicode.ttf
-otf2bdf -p 28 -l "$(CHARSET)" ttf/Lucida_Sans_Unicode.ttf > lucidasans28.bdf
./convbdf -u164 -a11 -d9 lucidasans28.bdf
rm -f lucidasans28.bdf
lucidasans9.c: convbdf ttf/Lucida_Sans_Unicode.ttf lucidasans9.c: convbdf ttf/Lucida_Sans_Unicode.ttf
-otf2bdf -p 9 -l "$(CHARSET)" ttf/Lucida_Sans_Unicode.ttf > lucidasans9.bdf -otf2bdf -p 9 -l "$(CHARSET)" ttf/Lucida_Sans_Unicode.ttf > lucidasans9.bdf
./convbdf -u164 -a2 -d3 lucidasans9.bdf ./convbdf -u164 -a2 -d3 lucidasans9.bdf

File diff suppressed because it is too large Load Diff