void targetNearestAsteroid(Advanced2D::Sprite* asteroid) { Advanced2D::Vector3<float> target = asteroid->getPosition(); float dist = static_cast<float>(ship_position.distance2d(target)); if (dist < nearest_distance) { nearest_asteroid = target; nearest_distance = dist; target_lead.mX = (asteroid->getVelocity().mX * 0.01f); target_lead.mY = (asteroid->getVelocity().mY * 0.01f); nearest_asteroid.mX = (nearest_asteroid.mX + target_lead.mX); nearest_asteroid.mY = (nearest_asteroid.mY + target_lead.mY); ship_angle = (90 + Advanced2D::Mathematic<float>::toDegrees( Advanced2D::Mathematic<float>::angleToTarget(ship_position, nearest_asteroid))); } if ((nearest_distance < 1200.0f) && fireTimer.stopwatch(100)) { firebullet(); } }
int main(int ac, char *av[]) { pthread_t thrds[NUMALL]; /* the threads */ void *animate(); /* the function */ int num_msg ; /* number of strings */ int c; /* user input */ int i; setup(elements); /* create all the threads */ for(i=0 ; i<NUMALL; i++) if ( pthread_create(&thrds[i], NULL, animate, &elements[i])){ fprintf(stderr,"error creating thread"); endwin(); exit(0); } moveplayer('.'); /* process user input */ while(1) { c = getch(); if (c == 'Q' || escape >= NUMLOSE) break; if (c == ' ') firebullet(); if (c == ',' || c == '.') moveplayer(c); } /* cancel all the threads */ pthread_mutex_lock(&drawlock); for (i=0; i<NUMALL; i++ ) pthread_cancel(thrds[i]); endwin(); printf("Your final score was: %d\n", score); return 0; }
void run(){ SDL_Window* window; SDL_Renderer* renderer; SDL_Surface* mover; SDL_Texture *tmover, *tbullet; SDL_Rect rmover; SDL_Event e; int quit = 0, mx, my, cx, cy; double rotate = 0; Bullet **bulletarray; const Uint8* currentKeyStates; /* Creating Window */ SDL_Init(SDL_INIT_EVERYTHING); window = SDL_CreateWindow("Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window, -1, 0); SDL_SetRenderDrawColor(renderer, 90, 90, 90, 0); bulletarray = initialiseshoot(renderer, &tbullet); /* Load Player */ mover = SDL_LoadBMP("mover2.bmp"); tmover = SDL_CreateTextureFromSurface(renderer, mover); SDL_FreeSurface(mover); mover = NULL; rmover.x = 320; rmover.y = 240; rmover.h = PLAYER_HEIGHT; rmover.w = PLAYER_WIDTH; /*Event loop*/ do{ SDL_PollEvent(&e); if(e.type == SDL_QUIT){ quit = 1; } /* Moving Direction */ currentKeyStates = SDL_GetKeyboardState(NULL); if( currentKeyStates[ SDL_SCANCODE_W ] ) { rmover.y -= 2; } if( currentKeyStates[ SDL_SCANCODE_S ] ) { rmover.y += 2; } if( currentKeyStates[ SDL_SCANCODE_A ] ) { rmover.x -= 2; } if( currentKeyStates[ SDL_SCANCODE_D ] ) { rmover.x += 2; } /* Rotation Angle */ SDL_GetMouseState(&mx, &my); cy = rmover.y + PLAYER_HEIGHT/2; cx = rmover.x + PLAYER_WIDTH/2; rotate = (atan2(my-cy, mx-cx))*57.295; /* fire bullet */ if(e.type == SDL_MOUSEBUTTONDOWN){ if(e.button.button == SDL_BUTTON_LEFT){ firebullet(bulletarray, cx, cy); } } deletebulletatboundary(bulletarray); bulletmovement(bulletarray); /* Update Screen */ SDL_RenderClear(renderer); renderbullets(renderer, tbullet, bulletarray); SDL_RenderCopyEx(renderer, tmover, NULL, &rmover, rotate, NULL, SDL_FLIP_NONE); SDL_RenderPresent(renderer); SDL_Delay(20); }while(quit == 0); /* Memory Cleanup */ SDL_DestroyTexture(tmover); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); quitshoot(tbullet, bulletarray); tmover = NULL; renderer = NULL; window = NULL; SDL_Quit(); }