spaceinvaders improved

This commit is contained in:
iggy 2011-08-01 18:29:50 +02:00
parent bd047cbe5a
commit f82cb8382a
2 changed files with 122 additions and 44 deletions

View File

@ -16,28 +16,43 @@ void delayms(uint32_t ms);
#define ENEMY_COLUMNS 6 #define ENEMY_COLUMNS 6
#define DISABLED 255 #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 { struct gamestate {
char player; char player;
char shot_x, shot_y; char shot_x, shot_y;
char shots_x[ENEMY_COLUMNS];
char shots_y[ENEMY_COLUMNS];
char alive; char alive;
char move, direction, lastcol; 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_x[ENEMY_ROWS][ENEMY_COLUMNS];
char enemy_row_y[ENEMY_ROWS]; 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; char key;
void init_game(void) {
void checkISP(void) { game.player = RESY/2-4;
if(gpioGetValue(RB_BTN2)==0){ game.shot_x = DISABLED;
gpioSetValue (RB_LED1, CFG_LED_ON); game.shot_y = 0;
delayms(200); game.alive = ENEMY_ROWS*ENEMY_COLUMNS;
gpioSetValue (RB_LED1, CFG_LED_OFF); game.move = 0;
while(gpioGetValue(RB_BTN0)==0); game.direction = -1;
EnableWatchdog(1000*5); game.lastcol = ENEMY_COLUMNS-1;
ReinvokeISP(); game.killed = false;
} game.step = false;
init_enemy();
for (char col=0; col<ENEMY_COLUMNS; col++){
game.shots_x[col] = DISABLED;
}
} }
void init_enemy() { void init_enemy() {
@ -65,19 +80,47 @@ void move_shot() {
for (int row=0; row<ENEMY_ROWS; row++) { for (int row=0; row<ENEMY_ROWS; row++) {
if (game.enemy_row_y[row]+6 >= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) { if (game.enemy_row_y[row]+6 >= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) {
for(int col = 0; col<ENEMY_COLUMNS; col++) { for(int col = 0; col<ENEMY_COLUMNS; col++) {
if(game.shot_x >= 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.enemy_x[row][col]=DISABLED;
game.shot_x = DISABLED; game.shot_x = DISABLED;
game.alive--; game.alive--;
game.score++;
return; return;
} }
} }
} }
} }
game.shot_y -= 3; game.shot_y -= 2;
} }
void move_shots() {
for (char col = 0; col<ENEMY_COLUMNS; col++){
//No shot, maybe generate
if (game.shots_x[col] == DISABLED) {
for (char row = 0; row<ENEMY_ROWS; row++) {
if (game.enemy_x[row][col] != DISABLED) {
if(getRandom()%(game.alive*5)==0) {
game.shots_x[col] = game.enemy_x[row][col]+5;
game.shots_y[col] = game.enemy_row_y[row]+0;
}
}
}
continue;
}
//moving out of bottm, end shot
if (game.shots_y[col] >= RESY) {
game.shots_x[col] = DISABLED;
return;
}
//check for collision with player
game.shots_y[col] += 1;
}
}
void move_player() { void move_player() {
if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){ if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){
game.player-=1; game.player-=1;
@ -99,6 +142,7 @@ void move_enemy() {
return; return;
} }
game.step = !game.step;
for (int col = 0; col < ENEMY_COLUMNS; col++) { for (int col = 0; col < ENEMY_COLUMNS; col++) {
for (int row = 0; row < ENEMY_ROWS; row++) { for (int row = 0; row < ENEMY_ROWS; row++) {
char pos = game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col]; 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) || 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; game.direction = (game.direction==1)?-1:1;
for (char r = 0; r<ENEMY_ROWS; r++) { for (char r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=4; game.enemy_row_y[r]+=2;
} }
return; return;
} }
@ -154,25 +198,34 @@ void move_enemy2() {
} }
void draw_player() { void draw_player() {
//draw_sprite(50, 20); //draw_sprite(50, 20);
draw_sprite(game.player, POS_PLAYER_Y); draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y);
} }
void draw_enemy() { void draw_enemy() {
for (int row = 0; row<ENEMY_ROWS; row++) { for (int row = 0; row<ENEMY_ROWS; row++) {
for (int col = 0; col<ENEMY_COLUMNS; col++) { for (int col = 0; col<ENEMY_COLUMNS; col++) {
if (game.enemy_x[row][col] != DISABLED) { if (game.enemy_x[row][col] != DISABLED) {
draw_sprite(game.enemy_x[row][col],game.enemy_row_y[row]); draw_sprite(TYPE_ENEMY_A+row,game.enemy_x[row][col],game.enemy_row_y[row]);
} }
} }
} }
} }
void draw_shot() { void draw_shots() {
if (game.shot_x != 255) { if (game.shot_x != 255) {
for (int length=0; length<=5; length++) { for (int length=0; length<=5; length++) {
lcdSetPixel(game.shot_x, game.shot_y+length, true); lcdSetPixel(game.shot_x, game.shot_y+length, true);
} }
} }
for (char col = 0; col < ENEMY_COLUMNS; col++) {
if (game.shots_x[col] != DISABLED) {
for (int length=0; length<=5; length++) {
lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true);
}
}
}
} }
void draw_status() { void draw_status() {
@ -181,18 +234,34 @@ void draw_status() {
} }
} }
void draw_sprite(char type, char x, char y) {
font = &Font_Invaders;
void draw_sprite(char x, char y) { switch(type) {
font = &Font_Invaders; case TYPE_PLAYER:
DoString(x,y-1,"C"); DoChar(x,y-1,'P');
/* for (int dx=0; dx<8; dx++){ break;
for(int dy=0; dy<8; dy++){ case TYPE_ENEMY_A:
lcdSetPixel(x+dx,y+dy, true); DoChar(x,y-1,game.step?'a':'A');
} break;
} */ case TYPE_ENEMY_B:
DoChar(x,y-1,game.step?'b':'B');
break;
case TYPE_ENEMY_C:
DoChar(x,y-1,game.step?'c':'C');
break;
}
} }
void draw_score() {
font = &Font_7x8;
DoInt(0,0,game.score);
DoInt(RESX-8,0,game.rokets);
font = &Font_Invaders;
DoChar(RESX-16, 0, 'P');
}
void check_end() { void check_end() {
if (game.killed) { if (game.killed) {
game.player = RESX/2+4; game.player = RESX/2+4;
@ -205,25 +274,35 @@ void check_end() {
} }
} }
void checkISP() {
if(gpioGetValue(RB_BTN0)==0 && gpioGetValue(RB_BTN4)==0){
DoString(0,0,"Enter ISP!");
lcdDisplay();
ISPandReset();
}
}
void main_spaceinvaders(void) { void main_spaceinvaders(void) {
gpioSetValue (RB_LED1, CFG_LED_OFF); gpioSetValue (RB_LED1, CFG_LED_OFF);
backlightInit(); backlightInit();
init_enemy(); init_game();
game.rokets = 3;
while (1) { while (1) {
checkISP(); ////checkISP();
lcdFill(0); lcdFill(0);
check_end(); check_end();
move_shot(); move_shot();
move_shots();
move_player(); move_player();
move_enemy(); move_enemy();
draw_player(); draw_score();
draw_player();
draw_enemy(); draw_enemy();
draw_shot(); draw_shots();
draw_status(); // draw_status();
lcdDisplay(); lcdDisplay();
delayms(10); delayms(12);
} }
return; return;
} }

View File

@ -38,16 +38,15 @@ const uint8_t InvadersBitmaps[] = {
0x9c, /* * *** */ 0x9c, /* * *** */
/* Char 67 is 9px wide @ 23 */ /* Char 67 is 8px wide @ 23 */
0x58, /* * ** */ 0x58, /* * ** */
0xbc, /* * **** */ 0xbc, /* * **** */
0x16, /* * ** */ 0x16, /* * ** */
0x3f, /* ****** */ 0x3f, /* ****** */
0x3f, /* ****** */ 0x3f, /* ****** */
0x36, /* ** ** */ 0x16, /* * ** */
0x1c, /* *** */ 0xbc, /* * **** */
0xb8, /* * *** */ 0x58, /* * ** */
0x40, /* * */
/* Char 80 is 7px wide @ 32 */ /* Char 80 is 7px wide @ 32 */
@ -125,7 +124,7 @@ const uint8_t InvadersBitmaps[] = {
const FONT_CHAR_INFO InvadersLengths[] = { const FONT_CHAR_INFO InvadersLengths[] = {
{11}, /* A */ {11}, /* A */
{12}, /* B */ {12}, /* B */
{ 9}, /* C */ { 8}, /* C */
{ 7}, /* P */ { 7}, /* P */
{16}, /* U */ {16}, /* U */
{11}, /* a */ {11}, /* a */