invaders stuff

This commit is contained in:
iggy 2011-08-02 13:09:58 +02:00
parent 2957ee4f21
commit d5dd653fbe
1 changed files with 154 additions and 78 deletions

View File

@ -17,7 +17,7 @@ void delayms(uint32_t ms);
#define ENEMY_COLUMNS 6 #define ENEMY_COLUMNS 6
#define DISABLED 255 #define DISABLED 255
#define UFO_PROB 100 #define UFO_PROB 1000
#define TYPE_PLAYER 1 #define TYPE_PLAYER 1
#define TYPE_ENEMY_A 2 #define TYPE_ENEMY_A 2
@ -42,7 +42,7 @@ struct gamestate {
bool step; bool step;
uint32_t score; uint32_t score;
char level; char level;
char rokets; int8_t 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];
uint8_t bunker[BUNKERS][BUNKER_WIDTH]; uint8_t bunker[BUNKERS][BUNKER_WIDTH];
@ -61,34 +61,87 @@ void draw_player();
void draw_enemy(); void draw_enemy();
void draw_shots(); void draw_shots();
void draw_ufo(); void draw_ufo();
void screen_intro();
void screen_gameover();
void screen_level();
bool check_bunker(char xpos, char ypos, int8_t shift);
void ram(void) { void ram(void) {
gpioSetValue (RB_LED1, CFG_LED_OFF); //gpioSetValue (RB_LED1, CFG_LED_OFF);
backlightInit(); //backlightInit();
while(1) {
init_game(); screen_intro();
game.rokets = 3; game.rokets = 3;
while (1) { game.level = 1;
////checkISP(); init_game();
lcdFill(0); screen_level();
check_end(); while (game.rokets>=0) {
move_ufo(); ////checkISP();
move_shot(); lcdFill(0);
move_shots(); check_end();
move_player(); move_ufo();
move_enemy(); move_shot();
draw_score(); move_shots();
draw_ufo(); move_player();
draw_bunker(); move_enemy();
draw_player(); draw_score();
draw_enemy(); draw_ufo();
draw_shots(); draw_bunker();
// draw_status(); draw_player();
lcdDisplay(); draw_enemy();
delayms(12); draw_shots();
} // draw_status();
lcdDisplay();
delayms(12);
}
screen_gameover();
}
return; return;
} }
void screen_intro() {
char key=0;
while(key==0) {
lcdFill(0);
font = &Font_Invaders;
DoString(28,25,"ABC");
font = &Font_7x8;
DoString (28,40,"SPACE");
DoString (18,50,"INVADERS");
//DoString (20,RESY-24, "Highscore");
DoString (0, 0, "12345");
DoString (0, 9, "iggy");
lcdDisplay();
delayms_queue(50);
key=getInput();
}
}
void screen_gameover() {
char key =0;
while(key==0) {
lcdFill(0);
font = &Font_7x8;
DoString (12,32, "GAME OVER");
DoInt (0,0, game.score);
DoString (0,9,"HIGHSCORE!");
lcdDisplay();
delayms_queue(50);
key=getInput();
}
}
void screen_level() {
lcdFill(0);
draw_score();
font = &Font_7x8;
int dx = DoString(20,32, "Level ");
DoInt(dx,32,game.level);
lcdDisplay();
delayms(500);
}
void init_game(void) { void init_game(void) {
game.player = RESX/2-4; game.player = RESX/2-4;
game.shot_x = DISABLED; game.shot_x = DISABLED;
@ -132,6 +185,25 @@ void init_enemy() {
} }
} }
bool check_bunker(char xpos, char ypos, int8_t shift){
for (int b=0; b<BUNKERS; b++) {
if (xpos>BUNKER_X[BUNKERS-1-b] &&
xpos<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
ypos<RESY-8 &&
ypos>RESY-16) {
int offset = BUNKER_WIDTH - (xpos-BUNKER_X[BUNKERS-1-b]);
if (game.bunker[b][offset]!=0) {
if (shift>0)
game.bunker[b][offset]&=game.bunker[b][offset]<<shift;
else
game.bunker[b][offset]&=game.bunker[b][offset]>>-shift;
return true;
}
}
}
return false;
}
void move_shot() { void move_shot() {
//No shot, do nothing //No shot, do nothing
if(game.shot_x == DISABLED) { if(game.shot_x == DISABLED) {
@ -145,18 +217,20 @@ void move_shot() {
} }
//check for collision with bunker //check for collision with bunker
for (int b=0; b<BUNKERS; b++) { // for (int b=0; b<BUNKERS; b++) {
if (game.shot_x>BUNKER_X[BUNKERS-1-b] && // if (game.shot_x>BUNKER_X[BUNKERS-1-b] &&
game.shot_x<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH && // game.shot_x<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
game.shot_y<RESY-8 && // game.shot_y<RESY-8 &&
game.shot_y>RESY-16) { // game.shot_y>RESY-16) {
int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]); // int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]);
if (game.bunker[b][offset]!=0) { // if (game.bunker[b][offset]!=0) {
game.bunker[b][offset]&=game.bunker[b][offset]<<1; // game.bunker[b][offset]&=game.bunker[b][offset]<<1;
game.shot_x=DISABLED; // game.shot_x=DISABLED;
} // }
} // }
} // }
if (check_bunker(game.shot_x,game.shot_y-5,1 ))
game.shot_x=DISABLED;
//check for collision with enemy, kill enemy if //check for collision with enemy, kill enemy if
for (int row=0; row<ENEMY_ROWS; row++) { for (int row=0; row<ENEMY_ROWS; row++) {
@ -185,6 +259,9 @@ void move_shot() {
game.shot_y -= 2; game.shot_y -= 2;
} }
void move_shots() { void move_shots() {
for (char col = 0; col<ENEMY_COLUMNS; col++){ for (char col = 0; col<ENEMY_COLUMNS; col++){
//No shot, maybe generate //No shot, maybe generate
@ -206,18 +283,21 @@ void move_shots() {
return; return;
} }
//check for collision with bunker //check for collision with bunker
for (int b=0; b<BUNKERS; b++) { if (check_bunker(game.shots_x[col],game.shots_y[col],-1))
if (game.shots_x[col]>BUNKER_X[BUNKERS-1-b] && game.shots_x[col]=DISABLED;
game.shots_x[col]<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
game.shots_y[col]<RESY-8 && // for (int b=0; b<BUNKERS; b++) {
game.shots_y[col]>RESY-16) { // if (game.shots_x[col]>BUNKER_X[BUNKERS-1-b] &&
int offset = BUNKER_WIDTH - (game.shots_x[col]-BUNKER_X[BUNKERS-1-b])-1; // game.shots_x[col]<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
if (game.bunker[b][offset]!=0) { // game.shots_y[col]<RESY-8 &&
game.bunker[b][offset]&=game.bunker[b][offset]>>1; // game.shots_y[col]>RESY-16) {
game.shots_x[col]=DISABLED; // 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 //check for collision with player
if (game.shots_y[col] >= RESY-13 && if (game.shots_y[col] >= RESY-13 &&
@ -273,22 +353,26 @@ void move_enemy() {
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];
if (pos != DISABLED) { if (pos != DISABLED) {
//Check collision with player //Check collision with player
if(game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8){ if((game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8) ||
game.killed = true; game.enemy_row_y[row]+8 >= POS_PLAYER_Y+8) {
} for(int row=0; row<ENEMY_ROWS; row++) {
game.enemy_row_y[row] = 10 + (40/ENEMY_ROWS)*row;
//check for collision with bunker, dirty
for (int b=0; b<BUNKERS; b++) {
if (pos>=BUNKER_X[BUNKERS-1-b] &&
pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
game.enemy_row_y[row]+8<RESY-8 &&
game.enemy_row_y[row]+8>RESY-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;
}
} }
} game.killed = true;
}
check_bunker(pos,game.enemy_row_y[row]+8,-2);
//check for collision with bunker, dirty
// for (int b=0; b<BUNKERS; b++) {
// if (pos>=BUNKER_X[BUNKERS-1-b] &&
// pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.enemy_row_y[row]+8<RESY-8 &&
// game.enemy_row_y[row]+8>RESY-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 //Are we at the beginning or end? Direction change
@ -393,24 +477,16 @@ void check_end() {
game.rokets--; game.rokets--;
delayms(500); delayms(500);
game.player = RESX/2+4; game.player = RESX/2+4;
for(int row=0; row<ENEMY_ROWS; row++) {
game.enemy_row_y[row] = 10 + (40/ENEMY_ROWS)*row;
}
for(int col=0; col<ENEMY_COLUMNS; col++) { for(int col=0; col<ENEMY_COLUMNS; col++) {
game.shots_x[col] = DISABLED; game.shots_x[col] = DISABLED;
} }
game.killed = false; game.killed = false;
} }
if (game.alive == 0) { if (game.alive == 0) {
delayms(500);
game.level++;
init_game();
screen_level();
} }
} }
void checkISP() {
if(gpioGetValue(RB_BTN0)==0 && gpioGetValue(RB_BTN4)==0){
DoString(0,0,"Enter ISP!");
lcdDisplay();
ISPandReset();
}
}