/*----------------------------------------------------------------------------- Fitr movements CFiter *fiter : fiter structure pointer int pad_no : controller number -----------------------------------------------------------------------------*/ void fiter_move(CFiter *fiter,int pad_no) { int pos_x; float pos_z; int cnt; int bullet_cnt; switch(fiter->flag){ case FITER_FLAG_START: /* From out of screen to within */ if(fiter->pos.z < 40.0){ fiter->pos.z += 3.0; } else { fiter->pos.z = 40.0; fiter->flag = FITER_FLAG_PLAYER; } break; case FITER_FLAG_PLAYER: /* Normal state. Controller process */ /* Fiter's Z axis movement */ pos_z = fiter->pos.z; pos_z += controllerdata_ptr[pad_no]->stick_y/10; if(pos_z < 0.0) { fiter->pos.z = 0.0; } else if( pos_z >500.0){ fiter->pos.z = 500.0; } else { fiter->pos.z = pos_z; } /* Fiter's X axis movement (rotation angle) */ pos_x = fiter->pos.x; pos_x -= controllerdata_ptr[pad_no]->stick_x/10; fiter->pos.x = (float)(pos_x % 360); /* Fiter's bullet movement */ for(cnt = 0; cnt < FITER_BULLET_NUM; cnt++){ bullet_move(&fiter->bullet[cnt]); } /* bullet shot */ if(controllerdataTriger[pad_no].button & A_BUTTON){ /* Free buffer search. More efficient with list structure, but when bullet number is small, a round robin search is sufficient. */ for(cnt = 0; cnt < FITER_BULLET_NUM; cnt++){ if(fiter->bullet[cnt].flag == FITER_BULLET_FLAG_NONE){ /* 'e�̏�����'u�̐�'� */ bullet_cnt = fiter->bullet_cnt; fiter->bullet_cnt++; fiter->bullet_cnt &= 0x03; fiter->bullet[cnt].flag = FITER_BULLET_FLAG_FIRE; fiter->bullet[cnt].pos.x = fiter->pos.x + gun_pos[bullet_cnt][0]; fiter->bullet[cnt].pos.y = fiter->pos.y + gun_pos[bullet_cnt][1]; fiter->bullet[cnt].pos.z = fiter->pos.z + gun_pos[bullet_cnt][2]; /* Bullet sound. Controller can change sound in easy-to-understand way */ auSndPlay(pad_no+2); break; } } } break; } /* end of switch */ }
int run() { /* Initialize the random number generator */ srand(time(NULL)); bool quit = false; printf("running\n"); if(!init_game()) { exit(2); } head: if(!load_data()) { printf("data load error\n"); exit(2); } init_common_num(); apply_background(); init_player(); draw_player(); SDL_Color textColor = { 255, 255, 255 }; //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //While the user hasn't quit while( quit == false ) { // printf("%d\n" , rand()%2); wait_frame(); //While there's an event to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { quit = true; } if( apear_enemy > ENEMY_NUM) { complete_level(event); } else { if( player.alive == 1) { player_input(event); } } } if(enemy.alive == 0) { init_enemy(); } if(enemy.alive == 1) { enemy_move(); } if(bullet.alive == 1) { bullet_move(); } if(knock(bullet , enemy)) { enemy.image = load_image("data/boom.gif", 1); bullet.image = load_image("data/boom.gif", 1); // draw_enemy(); // draw_bullet(); enemy.alive = 0; bullet.alive = 0; bullet.x = -1000; bullet.y = -1000; score ++; final_score ++; } if(knock(player , enemy)) { enemy.image = load_image("data/boom.gif", 1); player.image = load_image("data/boom.gif", 1); // draw_enemy(); // draw_bullet(); player.alive = 0; enemy.alive = 0; quit = true; } player_move(); apply_background(); draw_enemy(); if(bullet.alive == 1) { draw_bullet(); } draw_player(); if(apear_enemy > ENEMY_NUM) { char text0[256] = ""; sprintf(text0, " LEVEL %d", GAME_LEVEL); message = TTF_RenderText_Solid( font, text0, textColor ); apply_surface( 20 , 50 , message , screen); char text1[256] = "PRESS \"n\" TO NEXT LEVEL"; message = TTF_RenderText_Solid( font, text1, textColor ); apply_surface( 20 , 150 , message , screen); char text2[256] = ""; sprintf(text2, " SCORE:%d", score); message = TTF_RenderText_Solid( font, text2, textColor ); apply_surface( 20 , 250 , message , screen); char text3[256] = ""; if(score ==0 || shoot_time == 0) { sprintf(text3, " ACCURATE:%d" , 0); } else { sprintf(text3, " ACCURATE:%.2lf%%",(double)score/shoot_time*100); } message = TTF_RenderText_Solid( font, text3, textColor ); apply_surface( 20 , 350 , message , screen); } if( SDL_Flip( screen ) == -1 ) { return 1; } if(player.alive == 0) { quit = true; } if(enemy.alive == 0) { init_enemy(); } } char text1[256] = " GAME OVER "; message = TTF_RenderText_Solid( font, text1, textColor ); apply_surface( 20 , 50 , message , screen); char text2[256] = ""; sprintf(text2, " FINAL_SCORE:%d", final_score); message = TTF_RenderText_Solid( font, text2, textColor ); apply_surface( 20 , 150 , message , screen); char text3[256] = " PRESS \"r\" TO RESTART"; message = TTF_RenderText_Solid( font, text3, textColor ); apply_surface( 20 , 250 , message , screen); char text4[256] = " PRESS \"q\" TO EXIT"; message = TTF_RenderText_Solid( font, text4, textColor ); apply_surface( 20 , 350 , message , screen); if( SDL_Flip( screen ) == -1 ) { return 1; } quit = false; while( quit == false ) { // printf("%d\n" , rand()%2); // wait_frame(); //While there's an event to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { quit = true; } if( event.type == SDL_KEYDOWN ) { //Adjust the velocity switch( event.key.keysym.sym ) { case SDLK_r: goto head; break; case SDLK_q: quit = true; break; } } } } //Close the font that was used TTF_CloseFont( font ); //Quit SDL_ttf TTF_Quit(); //Quit SDL SDL_Quit(); }