int main(int argc, char** argv) { #ifndef TARGET_ESC64 //disable line-buffering on stdin struct termios old_tio, new_tio; assert(!tcgetattr(STDIN_FILENO, &old_tio)); new_tio = old_tio; new_tio.c_lflag &= (~ICANON & ~ECHO); assert(!tcsetattr(STDIN_FILENO, TCSANOW, &new_tio)); #endif for(;;) { fputs("\x1B[2J\x1B[H\x1B[?25l", stdout); //clear screen, cursor to home, cursor invis player1.pos.x = 16; player2.pos.x = 60; //initialize / generate gen_terrain(); init_player(&player1); init_player(&player2); curPlayer = &player1; //draw draw_static(); draw_terrain(); draw_player(&player1); draw_player(&player2); draw_stats(&player1); draw_stats(&player2); while(player1.hull > 0 && player2.hull > 0) { if(player_input()) { break; } TERM_DRAWSTR(MSG_X, MSG_Y + 1, "shot fired"); shoot(); curPlayer = curPlayer == &player1 ? &player2 : &player1; } } #ifndef TARGET_ESC64 //restore old settings line-buffering settings //tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); #endif //return 0; }
void start_enemy_battle(playerType *player, objectBase *objectdb, enemyBase *enemydb) { roomType *fighting_ground = player->current_room; enemyType *current_enemy = NULL; objectType *temp_object = NULL; char input_command[INPUT_LENGTH] = { 0 }; char input_parameter[INPUT_LENGTH] = { 0 }; int input_size = 0; int i; for(i=0; i<fighting_ground->n_enemy_ids; i++) { current_enemy = get_enemy_from_id(fighting_ground->enemy_ids[i], enemydb); if( current_enemy == NULL ) return; printf("A %s approaches you for a fight.\n", current_enemy->name); printf("It is %s\n", current_enemy->description); while( ! enemy_dead(current_enemy) ) { printf("Player HP: %d | Enemy HP %d\n", player->health, current_enemy->health); input_size = player_input(player, input_command, input_parameter); if( COMP_STR(input_command, "exit") || COMP_STR(input_command, "quit") || feof(stdin) ) { printf("Thanks for playing, have a nice day!\n"); player->quit = 1; goto LEAVE_BATTLE; } if( COMP_STR(input_command, "help" ) ) { printf("Commands: use pass look exit\n"); goto SKIP_DAMAGE; } if( COMP_STR(input_command, "look" ) ) { printf("You are in a battle with a %s\n", current_enemy->name); printf("It is %s\n", current_enemy->description); goto SKIP_DAMAGE; } else if( COMP_STR(input_command, "use" ) ) { temp_object = get_object_from_name(input_parameter, objectdb); if( temp_object != NULL && object_in_inventory(temp_object, player) ) { if( temp_object->hitpoints > 0 ) { printf("You attack the %s with the %s, doing some damage...\n", current_enemy->name, temp_object->name); current_enemy->health -= temp_object->hitpoints; if( enemy_dead(current_enemy) ) goto SKIP_DAMAGE; } else printf("It has no effect\n"); } else { printf("You don\'t have a %s\n", input_parameter); goto SKIP_DAMAGE; } } else if( COMP_STR(input_command, "pass" ) ) { printf("You do nothing at your peril\n"); } else { if( input_size > 0 ) /* check that the user hasn't simply just pressed enter */ printf("I don't understand that. Type \"help\" for a list of recognised commands\n"); goto SKIP_DAMAGE; } player->health -= current_enemy->damage; SKIP_DAMAGE: ; if( player->health <= 0 ) { printf("%s", COLOUR_RED); printf("You have died!"); printf("%s\n", COLOUR_NONE); player->quit = 1; goto LEAVE_BATTLE; } } printf("You have killed the %s!\n", current_enemy->name); } LEAVE_BATTLE: return; }
void start_game(playerType *player, roomBase *roomdb, objectBase *objectdb, enemyBase *enemydb) { player->quit = 0; player->n_object_ids = 0; player->health = 100; int test_coordinates[2] = { 0, 0 }; int input_size = 0; char input_command[INPUT_LENGTH] = { 0 }; char input_parameter[INPUT_LENGTH] = { 0 }; player->current_room = get_room_from_id(player->starting_room_id, roomdb); set_player_coordinates(player->current_room->coordinates, player); show_room_content(player->current_room, objectdb); while( ! player->quit ) { input_size = player_input(player, input_command, input_parameter); if( COMP_STR(input_command, "exit") || COMP_STR(input_command, "quit") || feof(stdin) ) { printf("Thanks for playing, have a nice day!\n"); player->quit = 1; } else if( COMP_STR(input_command, "look") ) { show_room_content(player->current_room, objectdb); } else if( COMP_STR(input_command, "health") ) { printf("Player HP: %d\n", player->health); } else if( COMP_STR(input_command, "inventory" ) ) { show_inventory(player, objectdb); } else if( COMP_STR(input_command, "examine" ) ) { examine_object(input_parameter, player, objectdb); } else if( COMP_STR(input_command, "take" ) ) { take_object(input_parameter, player, objectdb); } else if( COMP_STR(input_command, "use" ) ) { use_object(input_parameter, player, objectdb, roomdb, enemydb); } else if( COMP_STR(input_command, "north" ) ) { test_coordinates[0] = player->coordinates[0]; test_coordinates[1] = player->coordinates[1] + 1; go_to_room(player, roomdb, test_coordinates, objectdb, enemydb); } else if( COMP_STR(input_command, "south" ) ) { test_coordinates[0] = player->coordinates[0]; test_coordinates[1] = player->coordinates[1] - 1; go_to_room(player, roomdb, test_coordinates, objectdb, enemydb); } else if( COMP_STR(input_command, "east" ) ) { test_coordinates[0] = player->coordinates[0] + 1; test_coordinates[1] = player->coordinates[1]; go_to_room(player, roomdb, test_coordinates, objectdb, enemydb); } else if( COMP_STR(input_command, "west" ) ) { test_coordinates[0] = player->coordinates[0] - 1; test_coordinates[1] = player->coordinates[1]; go_to_room(player, roomdb, test_coordinates, objectdb, enemydb); } else if( COMP_STR(input_command, "help" ) ) { printf("Commands: %s\n", COMMANDS_LIST); } else { if( input_size > 0 ) /* check that the user hasn't simply just pressed enter */ printf("I don't understand that. Type \"help\" for a list of recognised commands\n"); } } }
// プレイヤーキャラを動かす int Character::player_move() { Display display; int Old_playerX = player.chara_x; int Old_playerY = player.chara_y; // キー入力判定 LATEST_HIT_DIRECT = player_input(); if(LATEST_HIT_DIRECT == LEFT) { // 移動先が空間の時はゲームオーバー if(map[player.chara_x - 1][player.chara_y] == BLANK) { return GAME_OVER; } player.chara_x -= 1; } else if(LATEST_HIT_DIRECT == RIGHT) { // 移動先が空間の時はゲームオーバー if(map[player.chara_x + 1][player.chara_y] == BLANK) { return GAME_OVER; } player.chara_x += 1; } else if(LATEST_HIT_DIRECT == UP) { // 移動先が空間時はゲームオーバー if(map[player.chara_x][player.chara_y - 1] == BLANK) { return GAME_OVER; } player.chara_y -= 1; } else if(LATEST_HIT_DIRECT == DOWN) { // 移動先が空間の時はゲームオーバー if(map[player.chara_x][player.chara_y + 1] == BLANK) { return GAME_OVER; } player.chara_y += 1; } chara_map[Old_playerX][Old_playerY] = BLANK; chara_map[player.chara_x][player.chara_y] = PLAYER; // アイテムとヒットした時 if(item_map[player.chara_x][player.chara_y] == ITEM){ hit_item(); item_map[player.chara_x][player.chara_y] = BLANK; LeftItemNum--; } // しっぽの座標更新 if(Player_Length > 0){ tail_rechain(Old_playerX, Old_playerY); } // tailとプレイヤーが重なったらゲームオーバー for(int i = 1; i <= Player_Length; i++){ if(tail_array[i].chara_x == player.chara_x && tail_array[i].chara_y == player.chara_y) { return GAME_OVER; } } return 0; }
int main(int argc, char** argv){ printf("hello world\n"); behaviors_init(); log_file_open("log.txt"); if (SDL_Init(SDL_INIT_EVERYTHING) == -1){ printf("%s\n", SDL_GetError()); return 1; } printf("sdl init success\n"); SDL_Renderer *renderer = NULL; SDL_Window *window = NULL; //Setup our window and renderer window = SDL_CreateWindow("Stick Fighter", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL){ printf("%s\n", SDL_GetError()); return 2; } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (renderer == NULL){ printf("%s\n", SDL_GetError()); return 3; } SDL_Texture *background1 = NULL; SDL_Texture *background2 = NULL; SDL_Texture *image = NULL; //background = sprite_load_image(renderer, "res/background_rcr.png"); background1 = sprite_load_image(renderer, "res/background_rcr.png"); background2 = sprite_load_image(renderer, "res/background_new.png"); SDL_Texture* backgrounds[2]; backgrounds[0] = background1; backgrounds[1] = background2; SDL_Rect bg; //Query the texture to get its width and height to use SDL_QueryTexture(background1, NULL, NULL, &bg.w, &bg.h); float bg_width = bg.w; float bg_height = bg.h; float world_width = bg_width / 10; float world_height = bg_height / 10; printf("bg_width: %f bg_height: %f world_width: %f world_height: %f\n", bg_width, bg_height, world_width, world_height); //printf("bg 1: [%p]\n", backgrounds[0]); //printf("bg 2: [%p]\n", backgrounds[1]); image = sprite_load_image(renderer, "res/output.png"); //printf("after sprite load image\n"); //load the meta info char* filename = "res/animation_meta_info.txt"; DArray meta_info; darray_init(&meta_info); sprite_load_meta_file(filename, &meta_info); darray_print(&meta_info); float interval = 0.0f; float start = 0.0f; int quit = 0; float delay = 1000.0f / FPS; Sprite* player = sprite_create(PLAYER, WALK, 200, 300, delay, image); player->x_speed = 0; player->y_speed = 0; Sprite* enemy = sprite_create(ENEMY, WALK, 0, 300, delay, image); enemy->x_speed = 2; enemy->y_speed = 2; enemy->advance_frame = 1; //set white background SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); pixel_to_world(bg_width, bg_height, world_width, world_height, enemy->x, enemy->y, &enemy->location->coords->x, &enemy->location->coords->y); int dest_x = 0; int dest_y = 0; //TODO: //animations for punch and basic kick //animation for getting hit //animations for walk left and run left //2 more ai behaviors //health bars above units and health stats //dying animation //dying logic //generate enemies off screen and have them walk on to screen //redo tornado kick and spinning back kick so stick figure is same size //figure out how to color stick figure without having to make new sprites on sheet...need to make figures white to set modulation //add rolling punch move //add game state which keeps track of game state //add buy moves screen in between levels //add a generator which generates guys for 100 levels int i = 0; while (!quit){ printf("iteration: %i\n", i); start = SDL_GetTicks(); quit = player_input(player, &meta_info); //printf("quit: %i\n", quit); //log_msg(LOG_DEBUG, "after player input: current frame: %i\n", player->current_frame[HIT]); //pixel location to world location pixel_to_world(bg_width, bg_height, world_width, world_height, player->x, player->y, &player->location->coords->x, &player->location->coords->y); //world location to pixel location world_to_pixel(bg_width, bg_height, world_width, world_height, enemy->location->coords->x, enemy->location->coords->y, &dest_x, &dest_y); //boundry check boundry_check(SCREEN_WIDTH, SCREEN_HEIGHT, enemy, &meta_info, &dest_x, &dest_y); //printf("2nd before moveto = enemy x:%i enemy y:%i dest_x:%i dest_y:%i\n", enemy->x, enemy->y, dest_x, dest_y); //update enemy sprite by sprite's speed moveto_coordinates(enemy, dest_x, dest_y); //are we at the original world location in pixel coordinates? int arrived = within_coordinates(enemy, dest_x, dest_y); printf("arrived: %i\n", arrived); if(arrived == 1) { //wander(enemy); // we reached last behavior's destination so do new AI behavior wander_stall_attack(player, enemy, &meta_info, 3); } sprite_update_bounding_box(player, &meta_info); sprite_update_bounding_box(enemy, &meta_info); //check collision int collision = sprite_check_collision(player, enemy); //printf("collision: %i\n", collision); if(collision) { sprite_handle_collision(player, enemy, &meta_info); } //handle collision //if animation is an attack then check frame range that triggers a hit //check if attack animation triggers a hi or low hit //if sequence of attacks is within delta time then 4th hit triggers a knockdown //if attack would make health below 0 then it triggers a knockdown //handle opposite attack from opposing sprite //if is in attack animation then check frame range for a hit //update animation frame sprite_update(player, &meta_info, renderer); sprite_update(enemy, &meta_info, renderer); //Rendering SDL_RenderClear(renderer); //printf("player->x: %i\n", player->x); //printf("moving right: %i\n", player->moving_right); //printf("moving left: %i\n", player->moving_left); int current_background = 0; sprite_draw_background(renderer, backgrounds, 2, ¤t_background, player->x, player->moving_right, player->moving_left, &player->scroll); sprite_draw_health_bar(renderer, player, &meta_info, 100); sprite_draw_health_bar(renderer, enemy, &meta_info, 100); //draw sprite sprite_render_frame(SCREEN_WIDTH, SCREEN_HEIGHT, player, &meta_info, renderer, 0); sprite_render_frame(bg_width, bg_height, enemy, &meta_info, renderer, 0); //Update the screen SDL_RenderPresent(renderer); interval = SDL_GetTicks() - start; if(interval > 0) { //float fps = 1.0f / (interval / 1000.0f); //printf("%f fps\n", fps); } i++; } //SDL_Delay(4000); //Destroy the various items sprite_destroy(player); sprite_destroy(enemy); darray_destroy(&meta_info); SDL_DestroyTexture(backgrounds[0]); SDL_DestroyTexture(backgrounds[1]); SDL_DestroyTexture(image); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); printf("after destory text rend win\n"); SDL_Quit(); log_file_close(); return 0; }
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(); }