SHIP * ship_update(SHIP *ship, bool keys[], ALLEGRO_TIMER *timer) { LIST *head = NULL; /* move forward */ if(keys[KEY_UP]) ship_accelerate(ship); /* rotate */ if(keys[KEY_LEFT]) ship_rotate(ship, -3); if(keys[KEY_RIGHT]) ship_rotate(ship, 3); /* hyperspace */ if(keys[KEY_LCONTROL]) ship_hyperspace(ship); /* shoot */ if(keys[KEY_SPACE]) ship_fire(ship, timer); if(ship->explosion) { animation_update(ship->explosion); /* if the animation is complete, create a new ship */ if(ship->explosion->current_frame >= ship->explosion->n_frames - 1) { /* FIXME: need preemptive collision detection, wait() */ SHIP *old = ship; ship = ship_create(); ship_free(old); } return ship; } /* ship missile positions */ head = list_first(ship->missiles); while(head) { missile_update((MISSILE *) head->data, timer); head = head->next; } ship->position->x += ship->velocity->x; ship->position->y += ship->velocity->y; wrap_position(ship->position); /* slow down over time */ ship_drag(ship); return ship; }
int game(int type){ int game_over = 0; { surface *background = new_image("background.png"); apply_texture(background, window); destroy_surface(background); } initialize_cameras();initialize_star();initialize_ships(2);initialize_dust(); initialize_shot();initialize_ai(); play_music("music.ogg"); draw_tank(0, (window_width / 2 - 421), window_height / 2 - 150); draw_tank(1, window_width / 2 + 401, window_height / 2 - 150); // Main loop for(;;){ int i; get_input(); if(keyboard[ESC] || game_over){ break; } for(i = 0; i < 2; i ++) if(ship[i].status == DEAD){ struct timeval now; gettimeofday(&now, NULL); if((int) (now.tv_sec - ship[i].time.tv_sec) > 2) game_over = 1; } erase_ships(); erase_shot(); if(type != CPU_X_CPU){ // Rotation: 10 Propulse: 20 Fire: 20 Hyper: 180 int my_ship; // Player 1 moves if(type == CPU_X_PLR){ ai_play(0); my_ship = 1; } else if(type == PLR_X_CPU){ my_ship = 0; ai_play(1); } else my_ship = 0; if(keyboard[LEFT]) rotate_ship(my_ship, LEFT); if(keyboard[RIGHT]) rotate_ship(my_ship, RIGHT); if(keyboard[UP]) propulse_ship(my_ship); if(keyboard[DOWN]) ship_fire(my_ship); if(keyboard[RIGHT_CTRL]) goto_hyperspace(my_ship); } else{ ai_play(0); ai_play(1); } if(type == PLR_X_PLR){ // Player 2 moves if(keyboard[A]) rotate_ship(1, LEFT); if(keyboard[D]) rotate_ship(1, RIGHT); if(keyboard[W]) propulse_ship(1); if(keyboard[L]) blow_up(1); if(keyboard[S]) ship_fire(1); if(keyboard[LEFT_CTRL]) goto_hyperspace(1); } update_star(); // 6 update_ships(); // 2 update_dust(); // 145 update_shot(); // 16 film_ships(); // Até 16 film_dust(); // 20 film_shot(); // Até 20 weaver_rest(10000000); } stop_music(); destroy_star(); destroy_ships(); destroy_cameras(); destroy_dust(); destroy_shot(); clean_keyboard(); return 0; }