#include #include "basic/basic.h" #include "lcd/render.h" #include "lcd/display.h" #include "lcd/allfonts.h" void ReinvokeISP(void); void EnableWatchdog(uint32_t ms); void delayms(uint32_t ms); /**************************************************************************/ #define POS_PLAYER_Y 60 #define POS_UFO_Y 0 #define ENEMY_ROWS 3 #define ENEMY_COLUMNS 6 #define DISABLED 255 #define UFO_PROB 100 #define TYPE_PLAYER 1 #define TYPE_ENEMY_A 2 #define TYPE_ENEMY_B 3 #define TYPE_ENEMY_C 4 #define TYPE_UFO 5 #define BUNKERS 3 #define BUNKER_WIDTH 10 static const BUNKER_X[] = {15, RESX/2-BUNKER_WIDTH/2,RESX-BUNKER_WIDTH-15}; static const ENEMY_WIDTHS[] = {10,11,8}; struct gamestate { char player; char ufo; char shot_x, shot_y; char shots_x[ENEMY_COLUMNS]; char shots_y[ENEMY_COLUMNS]; char alive; char move, direction, lastcol; bool killed; bool step; uint32_t score; char level; char rokets; char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; char enemy_row_y[ENEMY_ROWS]; uint8_t bunker[BUNKERS][BUNKER_WIDTH]; } game; char key; void init_game(); void check_end(); void move_ufo(); void move_shots(); void move_player(); void move_enemay(); void draw_score(); void draw_bunker(); void draw_player(); void draw_enemy(); void draw_shots(); void draw_ufo(); void ram(void) { gpioSetValue (RB_LED1, CFG_LED_OFF); backlightInit(); init_game(); game.rokets = 3; while (1) { ////checkISP(); lcdFill(0); check_end(); move_ufo(); move_shot(); move_shots(); move_player(); move_enemy(); draw_score(); draw_ufo(); draw_bunker(); draw_player(); draw_enemy(); draw_shots(); // draw_status(); lcdDisplay(); delayms(12); } return; } void init_game(void) { game.player = RESX/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 = 0; game.step = false; game.ufo = DISABLED; init_enemy(); for (char col=0; colBUNKER_X[BUNKERS-1-b] && game.shot_xRESY-16) { int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]); if (game.bunker[b][offset]!=0) { game.bunker[b][offset]&=game.bunker[b][offset]<<1; game.shot_x=DISABLED; } } } //check for collision with enemy, kill enemy if for (int row=0; row= 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]+ENEMY_WIDTHS[row]) { game.enemy_x[row][col]=DISABLED; game.shot_x = DISABLED; game.alive--; game.score++; return; } } } } //check for collision with ufo if (game.ufo != DISABLED && game.shot_x>game.ufo && game.shot_x= RESY) { game.shots_x[col] = DISABLED; return; } //check for collision with bunker for (int b=0; bBUNKER_X[BUNKERS-1-b] && game.shots_x[col]RESY-16) { int offset = BUNKER_WIDTH - (game.shots_x[col]-BUNKER_X[BUNKERS-1-b])-1; if (game.bunker[b][offset]!=0) { game.bunker[b][offset]&=game.bunker[b][offset]>>1; game.shots_x[col]=DISABLED; } } } //check for collision with player if (game.shots_y[col] >= RESY-13 && game.shots_x[col] > game.player+1 && game.shots_x[col] < game.player+6) { game.killed = true; } //move shots down game.shots_y[col] += 1; } } void move_ufo() { if (game.ufo == DISABLED) { if ((getRandom()%UFO_PROB)==0) { game.ufo = 0; } return; } if (game.ufo >= RESX){ game.ufo = DISABLED; return; } game.ufo++; } void move_player() { if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){ game.player-=1; } if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){ game.player+=1; } if(gpioGetValue(RB_BTN4)==0 && game.shot_x == 255){ game.shot_x = game.player+4; game.shot_y = POS_PLAYER_Y; } } void move_enemy() { if(game.move > 0){ game.move--; 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]; if (pos != DISABLED) { //Check collision with player if(game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8){ game.killed = true; } //check for collision with bunker, dirty for (int b=0; b=BUNKER_X[BUNKERS-1-b] && pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH && game.enemy_row_y[row]+8RESY-16) { int offset = BUNKER_WIDTH - (pos-BUNKER_X[BUNKERS-1-b]); if (game.bunker[b][offset]!=0) { game.bunker[b][offset]&=game.bunker[b][offset]>>2; } } } //Are we at the beginning or end? Direction change if((pos <=0 && game.direction != 1) || (pos >=RESX-11-1 && game.direction == 1)){ game.direction = (game.direction==1)?-1:1; for (char r = 0; r