Created
June 29, 2014 05:01
-
-
Save kamiyaowl/81ad8cf177d16d271a0a to your computer and use it in GitHub Desktop.
flappy bird for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <time.h> | |
typedef int boolean; | |
#define true 1 | |
#define false 0 | |
#define WIDTH 64 | |
#define HEIGHT 48 | |
#define CENTER_X (WIDTH / 2) | |
#define CENTER_Y (HEIGHT / 2) | |
#define USER_X 4//ユーザーの位置 | |
#define WALL_COUNT 8//壁の数 | |
#define SPAN_Y 5//壁の空きスペース | |
#define SPAN_WIDTH (WIDTH / WALL_COUNT) | |
#define SPAN_HEIGHT (HEIGHT / SPAN_Y) | |
#define ACCELA_END 0xff | |
const int8_t flappy_accela[] = { | |
5, 3, 2, 1, | |
-1, -2, -3, -5, -8, -9, -10, -11, -12, -15, | |
ACCELA_END//end signal | |
}; | |
uint8_t current_accela = 0; | |
uint8_t user_x, user_y, wall_offset; | |
uint16_t flappy_count; | |
uint8_t walls[WALL_COUNT]; | |
/* ====================================== */ | |
//private functions | |
void wall_create(uint8_t index){ | |
walls[index] = rand() % (HEIGHT - SPAN_HEIGHT); | |
} | |
//速度を取得し変更します | |
int8_t get_accela(){ | |
int8_t data = flappy_accela[current_accela]; | |
//data shift | |
if(flappy_accela[current_accela + 1] != ACCELA_END) ++current_accela; | |
return data; | |
} | |
//上昇します | |
void up_accela(){ | |
current_accela = 0; | |
} | |
//平衡状態まで移動します | |
void init_accela(){ | |
up_accela(); | |
while(flappy_accela[current_accela] > 0) { | |
++current_accela; | |
} | |
} | |
/* ====================================== */ | |
//public functions | |
void flappy_init(){ | |
for(uint8_t i = 0; i < WALL_COUNT ; ++i) { | |
wall_create(i); | |
} | |
user_x = USER_X; | |
user_y = CENTER_Y; | |
wall_offset = 0; | |
flappy_count = 0; | |
init_accela(); | |
} | |
boolean flappy_update(boolean is_up) { | |
if(is_up) up_accela(); | |
//position update | |
uint8_t ny = user_y + get_accela(); | |
//valid | |
if(ny >= HEIGHT) return false; | |
if(wall_offset % SPAN_WIDTH == user_x) { | |
uint8_t p = (wall_offset + user_x + 1) / SPAN_WIDTH % WALL_COUNT; | |
if(user_y < walls[p] || walls[p] + SPAN_HEIGHT < user_y) return false; | |
} | |
//すすむ | |
user_y = ny; | |
wall_offset = (wall_offset + 1) % WIDTH; | |
//wall update | |
if(wall_offset % SPAN_WIDTH == 1) { | |
wall_create(wall_offset / SPAN_WIDTH); | |
} | |
//score | |
++flappy_count; | |
return true; | |
} | |
void flappy_print(){ | |
printf("**************** %d *****************\n",flappy_count); | |
for(uint8_t i = 0 ; i < WIDTH ; ++i) { | |
//ゲームグラフィックならもっと簡単に | |
if((i + wall_offset) % SPAN_WIDTH == 0) { | |
uint8_t index = (i + wall_offset) / SPAN_WIDTH % WALL_COUNT; | |
for(uint8_t j = 0 ; j < HEIGHT ; ++j) { | |
if(user_x == i && user_y == j) printf("@"); | |
else if(j < walls[index]) printf("#"); | |
else if(walls[index] + SPAN_HEIGHT < j) printf("#"); | |
else printf(" "); | |
} | |
printf("\twalls[%d] = %d",index,walls[index]); | |
} else { | |
if(i == user_x) { | |
for(uint8_t j = 0 ; j < user_y ; ++j) | |
printf(" "); | |
printf("%c",'@'); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
/* ====================================== */ | |
int main(void){ | |
char c; | |
boolean is_up = false; | |
srand(time(NULL)); | |
flappy_init(); | |
while(true) { | |
printf("D>"); | |
scanf("%c%*c",&c); | |
switch(c) { | |
case 'd': | |
is_up = true; | |
break; | |
case 'q': | |
return 0; | |
case 'n': | |
flappy_init(); | |
break; | |
default: | |
is_up = false; | |
break; | |
} | |
if(!flappy_update(is_up)) { | |
printf("===== game over =====\n"); | |
flappy_init(); | |
} | |
flappy_print(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment