From bad31c466aa01a15e7d9ce641f49f0a8aebc3be0 Mon Sep 17 00:00:00 2001 From: iggy Date: Tue, 2 Aug 2011 01:12:52 +0200 Subject: [PATCH] spacinvaders stuff --- firmware/applications/spaceinvaders.c | 338 -------------------------- firmware/loadable/spaceinvaders.c | 324 +++++++++++++++++++----- 2 files changed, 258 insertions(+), 404 deletions(-) delete mode 100644 firmware/applications/spaceinvaders.c diff --git a/firmware/applications/spaceinvaders.c b/firmware/applications/spaceinvaders.c deleted file mode 100644 index 80e2edb..0000000 --- a/firmware/applications/spaceinvaders.c +++ /dev/null @@ -1,338 +0,0 @@ -#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 ENEMY_ROWS 3 -#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 - -#define BUNKERS 2 -#define BUNKER_WIDTH 10 -static const BUNKER_X[] = {20,RESX-BUNKER_WIDTH-20}; -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 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) { - 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 = 0; - game.step = false; - 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; - } - } - } - } - - game.shot_y -= 2; -} - -void move_shots() { - for (char col = 0; col= 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]); - 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_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; - } - - if((pos <=0 && game.direction != 1) || - (pos >=RESX-11-1 && game.direction == 1)){ - game.direction = (game.direction==1)?-1:1; - for (char r = 0; rBUNKER_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]+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; + //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() { @@ -117,6 +267,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]; @@ -125,12 +276,27 @@ void move_enemy() { 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-8-1 && game.direction == 1)){ + (pos >=RESX-11-1 && game.direction == 1)){ game.direction = (game.direction==1)?-1:1; for (char r = 0; r=RESX-8 && game.direction == 1)){ - game.direction *= -1; - //TODOmove down - return; - } - game.enemy_x[row][col] += game.direction; - next = true; - } - } - if (next){ - game.lastcol += game.direction; - return; - } - } - game.move = game.alive; - return; - } - game.move--; -} void draw_player() { //draw_sprite(50, 20); - draw_sprite(TYPE_PLAYER,game.player, POS_PLAYER_Y); + draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y); +} + +void draw_ufo() { + if (game.ufo!=DISABLED) + draw_sprite(TYPE_UFO, game.ufo, POS_UFO_Y); } void draw_enemy() { for (int row = 0; row