diff --git a/firmware/applications/spaceinvaders.c b/firmware/applications/spaceinvaders.c index c774fd4..b6fc4c7 100644 --- a/firmware/applications/spaceinvaders.c +++ b/firmware/applications/spaceinvaders.c @@ -16,28 +16,43 @@ void delayms(uint32_t ms); #define ENEMY_COLUMNS 6 #define DISABLED 255 +#define TYPE_PLAYER 1 +#define TYPE_ENEMY_A 2 +#define TYPE_ENEMY_B 3 +#define TYPE_ENEMY_C 4 +static const ENEMY_WIDTHS[] = {10,11,8}; + struct gamestate { char player; char shot_x, shot_y; + char shots_x[ENEMY_COLUMNS]; + char shots_y[ENEMY_COLUMNS]; char alive; char move, direction, lastcol; - bool killed; + bool killed, step; + uint32_t score; + char level; + char rokets; char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; char enemy_row_y[ENEMY_ROWS]; -} game = {RESX/2-4, DISABLED, 0,ENEMY_ROWS*ENEMY_COLUMNS, 0, -1, ENEMY_COLUMNS-1, false}; +} game; char key; - -void checkISP(void) { - if(gpioGetValue(RB_BTN2)==0){ - gpioSetValue (RB_LED1, CFG_LED_ON); - delayms(200); - gpioSetValue (RB_LED1, CFG_LED_OFF); - while(gpioGetValue(RB_BTN0)==0); - EnableWatchdog(1000*5); - ReinvokeISP(); - } +void init_game(void) { + game.player = RESY/2-4; + game.shot_x = DISABLED; + game.shot_y = 0; + game.alive = ENEMY_ROWS*ENEMY_COLUMNS; + game.move = 0; + game.direction = -1; + game.lastcol = ENEMY_COLUMNS-1; + game.killed = false; + game.step = false; + init_enemy(); + for (char col=0; col= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) { for(int col = 0; col= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+8) { + if(game.shot_x >= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+ENEMY_WIDTHS[row]) { game.enemy_x[row][col]=DISABLED; game.shot_x = DISABLED; game.alive--; + game.score++; return; } } } } - game.shot_y -= 3; + game.shot_y -= 2; } +void move_shots() { + for (char col = 0; col= RESY) { + game.shots_x[col] = DISABLED; + return; + } + + //check for collision with player + game.shots_y[col] += 1; + } +} + + void move_player() { if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){ game.player-=1; @@ -99,6 +142,7 @@ void move_enemy() { return; } + game.step = !game.step; for (int col = 0; col < ENEMY_COLUMNS; col++) { for (int row = 0; row < ENEMY_ROWS; row++) { char pos = game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col]; @@ -109,10 +153,10 @@ void move_enemy() { } if((pos <=0 && game.direction != 1) || - (pos >=RESX-8-1 && game.direction == 1)){ + (pos >=RESX-11-1 && game.direction == 1)){ game.direction = (game.direction==1)?-1:1; for (char r = 0; r