void crear(int x , int y, std::vector< std::vector<int> >& visited, std::vector< std::vector<int> >& mh, std::vector< std::vector<int> >& mv ) { std::pair <int,int> pos; pos.first = x; pos.second = y; if (visited[y][x]){ return; }else{ visited[y][x] = 1; std::vector< std::pair<int,int> > vecinos; vecinos = dameVecinos(pos); for(int i =0; i<4;i++){ if(esPosicionValida(vecinos[i]) && !visited[vecinos[i].second][vecinos[i].first]){ //si no fue visitada, rompo la pared y me llamo recursivamente //la fila de arista a la derech es la posicion y if (right_move(x, vecinos[i].first)){ mh[y][x] = 1 ;} if (left_move(x, vecinos[i].first)){ mh[y][x-1] = 1;} if (up_move(y, vecinos[i].second)){ mv[y-1][x] = 1;} if (down_move(y, vecinos[i].second)){ mv[y][x] = 1;} crear(vecinos[i].first,vecinos[i].second,visited, mh, mv); } } } }
void main_loop(void) { Doubly_linked_node *iterator; int direction, dx, dy, count; int x, y; uint32_t last_time; uint32_t current_time; uint32_t ellapsed_time; uint32_t start_time; Point *save_eye; float speed; last_time = SDL_GetTicks(); while (!conf->key[SDLK_ESCAPE] && !conf->quit) { start_time = SDL_GetTicks(); save_eye = point_new(conf->eye->x, conf->eye->y, conf->eye->z); speed = (conf->free_fly) ? 5 : 0.5; update_event(); if (!conf->viewMode && conf->key[SDLK_F1]) { conf->key[SDLK_F1] = 0; conf->free_fly = !conf->free_fly; if (!conf->free_fly) { save_eye->x = CELL_SIZE / 2; save_eye->y = CELL_SIZE / 2; save_eye->z = CHARACTER_SIZE; } else { conf->jump_duration = 0; } } if (!conf->viewMode && (conf->key[SDLK_LSHIFT] || conf->key[SDLK_LALT])) { if ( conf->jump_duration == 0 ) { /* j'ai du mal a courir quand je saute */ speed = (conf->free_fly) ? 7.51337 : 0.91337; } } if (!conf->viewMode && conf->key[SDLK_LCTRL]) { speed = (conf->free_fly) ? 0.1 : 0.3; } if (conf->key[SDLK_F2]) { conf->key[SDLK_F2] = 0; conf->time = !conf->time; } if (conf->key[SDLK_F3]) { conf->key[SDLK_F3] = 0; conf->display = !conf->display; } if (!conf->viewMode && conf->key[SDLK_F4]) { conf->key[SDLK_F4] = 0; conf->quadTreeView = !conf->quadTreeView; } if (conf->key[SDLK_F5]) { conf->key[SDLK_F5] = 0; if (!strcmp(conf->music, "music/music2.mp3")) { conf->music = "music/music.mp3"; music_new(); } else if (!strcmp(conf->music, "music/music.mp3")) { conf->music = "music/music2.mp3"; music_new(); } } if (!conf->viewMode && (conf->key[SDLK_UP] || conf->key[SDLK_z])) { forward_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_DOWN] || conf->key[SDLK_s])) { backward_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_RIGHT] || conf->key[SDLK_d])) { right_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_LEFT] || conf->key[SDLK_q])) { left_move(save_eye, speed); } if (!conf->viewMode && conf->key[SDLK_a]) { conf->key[SDLK_a] = 0; conf->theta += 180; } if (!conf->viewMode && conf->key[SDLK_r]) { conf->key[SDLK_r] = 0; conf->life = MAX_HEALTH; save_eye->x = CELL_SIZE / 2; save_eye->y = CELL_SIZE / 2; save_eye->z = CHARACTER_SIZE; portals->bleu->actif = 0; portals->orange->actif = 0; conf->timer = SDL_GetTicks(); } if (conf->key[SDLK_KP2] || (conf->key[SDLK_n])) { if (conf->free_fly) { save_eye->z -= 3; } else if (conf->viewMode) { save_eye->z -= 10; } } if (conf->key[SDLK_KP8] || (conf->key[SDLK_SPACE])) { if (!conf->free_fly && !conf->viewMode) { conf->key[SDLK_KP8] = 0; conf->key[SDLK_SPACE] = 0; if ( !conf->viewMode && conf->jump_duration == 0 ) { conf->jump_duration = 120; } } else if (conf->free_fly){ save_eye->z += 3; } else { save_eye->z += 10; } } conf->eye->z += jump(save_eye); if ( conf->key[SDLK_p] ) { conf->key[SDLK_p] = 0; if(Mix_PausedMusic() == 1) { Mix_ResumeMusic(); } else { Mix_PauseMusic(); } } if ( conf->key[SDLK_PLUS] || conf->key[SDLK_m] || conf->key[SDLK_KP_PLUS] ) { change_volume(CHANG_VOL); } if ( conf->key[SDLK_MINUS] || conf->key[SDLK_l] || conf->key[SDLK_KP_MINUS] ) { change_volume(-CHANG_VOL); } if (!conf->mousebutton[SDL_BUTTON_LEFT] && !conf->mousebutton[SDL_BUTTON_RIGHT]) { conf->shoot = 0; } if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_LEFT]) { conf->mousebutton[SDL_BUTTON_LEFT] = 0; conf->mousebutton[SDL_BUTTON_RIGHT] = 0; conf->shoot = 1; } if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_RIGHT]) { conf->mousebutton[SDL_BUTTON_LEFT] = 0; conf->mousebutton[SDL_BUTTON_RIGHT] = 0; conf->shoot = 2; } if (!conf->viewMode && portals->orange->actif && portals->bleu->actif ) { if ( abs(save_eye->x - portals->bleu->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->bleu->portail->y) < TRIGGER_DISTANCE) { save_eye->x = portals->orange->portail->x - (sin(portals->orange->rotation) * PUSH_DISTANCE); save_eye->y = portals->orange->portail->y + (cos(portals->orange->rotation) * PUSH_DISTANCE); conf->theta += 180 + ( portals->orange->rotation - portals->bleu->rotation); } else if ( abs(save_eye->x - portals->orange->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->orange->portail->y) < TRIGGER_DISTANCE) { save_eye->x = portals->bleu->portail->x - (sin(portals->bleu->rotation) * PUSH_DISTANCE); save_eye->y = portals->bleu->portail->y + (cos(portals->bleu->rotation) * PUSH_DISTANCE); conf->theta += 180 + ( portals->bleu->rotation - portals->orange->rotation); } } if (COORD((int)(save_eye->x / CELL_SIZE), (int)(save_eye->y / CELL_SIZE)) != COORD((int)((conf->eye)->x / CELL_SIZE), (int)((conf->eye)->y / CELL_SIZE))) { iterator = mwl->last; while (1) { if ((iterator->object)->type == MOVING_WALL) { x = (((int)((iterator->object)->anchor)->x ) / CELL_SIZE); y = (((int)((iterator->object)->anchor)->y ) / CELL_SIZE); count = 0; direction = rand() % 4; while (count < 4) { dx = 0; dy = 0; switch (direction) { case 0: dx = 1; break; case 1: dy = 1; break; case 2: dx = -1; break; default: dy = -1; break; } if(COORD((x+dx),(y+dy)) == COORD(((int)save_eye->x / CELL_SIZE), ((int)save_eye->y / CELL_SIZE)) || !IS_PLAYABLE(COORD((x+dx),(y+dy))) || (dx == 1 && END_RIGHT(COORD(x,y))) || (dy == 1 && END_TOP(COORD(x,y))) || (dx == -1 && END_LEFT(COORD(x,y))) || (dy == -1 && END_BOTTOM(COORD(x,y))) ) { direction = (direction + 1) % 4; ++count; } else { if(laby->matrix[COORD(x,y)] == SPIKES_MW) { laby->matrix[COORD(x,y)] = SPIKES; } else if (laby->matrix[COORD(x,y)] == MOVING_WALL) { laby->matrix[COORD(x,y)] = PASS; } if(laby->matrix[COORD((x+dx),(y+dy))] == PASS) { laby->matrix[COORD((x+dx),(y+dy))] = MOVING_WALL; } else if (laby->matrix[COORD((x+dx),(y+dy))] == SPIKES) { laby->matrix[COORD((x+dx),(y+dy))] = SPIKES_MW; } ((iterator->object)->anchor)->x += (dx * CELL_SIZE); ((iterator->object)->anchor)->y += (dy * CELL_SIZE); count = 4; } } } if (iterator->next != NULL) { iterator = iterator->next; } else { break; } } } /*fprintf(stderr, "%d %d\n", conf->mousex - SCREEN_MID_WIDTH, conf->mousey - SCREEN_MID_HEIGHT);*/ if (!conf->viewMode && (( save_eye->x > 2 && save_eye->y > 2 && save_eye->x < (CELL_SIZE * WIDTH) - 2 && save_eye->y < (CELL_SIZE * HEIGHT) - 2 /*&& save_eye->z <= CHARACTER_SIZE && save_eye->z > CHARACTER_SIZE - 3*/ && IS_PLAYABLE(COORD((int)(save_eye->x / CELL_SIZE),(int)(save_eye->y / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x + 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y + 2) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x - 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y - 2) / CELL_SIZE))) ) || conf->free_fly == 1 )) { conf->eye->x = save_eye->x; conf->eye->y = save_eye->y; conf->eye->z = save_eye->z; } /* Mouse motion */ if(!conf->viewMode) { conf->theta -= (conf->mousex - SCREEN_MID_WIDTH) * SENSITIVITY; conf->phi -= (conf->mousey - SCREEN_MID_HEIGHT) * SENSITIVITY; SDL_WarpMouse(SCREEN_MID_WIDTH, SCREEN_MID_HEIGHT); modify_direction(); change_center(); } else { conf->eye->x = conf->center->x + CELL_SIZE * WIDTH * cos(conf->theta * M_PI / 180); conf->eye->y = conf->center->y + CELL_SIZE * HEIGHT * sin(conf->theta * M_PI / 180); conf->theta += 0.5; if (conf->theta >= 360) { conf->theta = 0; } } if (conf->life <= 0) { fprintf(stderr," .,---.\n"); fprintf(stderr," ,/XM#MMMX;,\n"); fprintf(stderr," -%%##########M%%,\n"); fprintf(stderr," -@######%% $###@=\n"); fprintf(stderr," .,--, -H#######$ $###M:\n"); fprintf(stderr," ,;$M###MMX; .;##########$;HM###X=\n"); fprintf(stderr," ,/@##########H= ;################+\n"); fprintf(stderr,"-+#############M/, %%##############+\n"); fprintf(stderr,"%%M###############= /##############:\n"); fprintf(stderr,"H################ .M#############;.\n"); fprintf(stderr,"@###############M ,@###########M:.\n"); fprintf(stderr,"X################, -$=X#######@:\n"); fprintf(stderr,"/@##################%%- +######$-\n"); fprintf(stderr,".;##################X .X#####+,\n"); fprintf(stderr," .;H################/ -X####+.\n"); fprintf(stderr," ,;X##############, .MM/\n"); fprintf(stderr," ,:+$H@M#######M#$- .$$=\n"); fprintf(stderr," .,-=;+$@###X: ;/=.\n"); fprintf(stderr," .,/X$; .::,\n"); fprintf(stderr," ., ..\n"); fprintf(stderr,"Haw Haw ! You lose !\n"); conf->quit = 1; } if (IS_EXIT(COORD((int)(conf->eye->x / CELL_SIZE), (int)(conf->eye->y / CELL_SIZE))) && !conf->free_fly) { if (!conf->win) { fprintf(stderr, "\n #,\n"); fprintf(stderr, " ###\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ####\n"); fprintf(stderr, " |\n"); fprintf(stderr, " #####\n"); fprintf(stderr, " ######\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##########\n"); fprintf(stderr, " ## #############\n"); fprintf(stderr, " ####### ###############\n"); fprintf(stderr, " #############################\n"); fprintf(stderr, " .###################################\n"); fprintf(stderr, " #####################################;\n"); fprintf(stderr, " ## ##.\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " #####################################\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ###\n"); fprintf(stderr, " ##### #####\n"); fprintf(stderr, " ### ##################################### ###\n"); fprintf(stderr, " ### ## ## ###\n"); fprintf(stderr, " ## ## ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ## ##\n"); fprintf(stderr, " ## ##################################### ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " #### ####\n"); fprintf(stderr, " ###### ######\n"); fprintf(stderr, " ###############################\n"); fprintf(stderr, "The cake was not a lie !\n"); } if (strcmp(conf->music, "music/music3.mp3")) { conf->music = "music/music3.mp3"; music_new(); } conf->win = 1; } /* Display with FPS care */ current_time = SDL_GetTicks(); ellapsed_time = current_time - last_time; last_time = current_time; display(); ellapsed_time = SDL_GetTicks() - start_time; if (ellapsed_time < 25) { SDL_Delay(25 - ellapsed_time); } point_free(save_eye); } }
int main(){ // /* int m; m=getch(); while(m){ m=getch(); printf("%d\n",m); }*/ int temp=0, i,j;//以下所有代码,I表示行,j表示列 /*获取控制台光标,并隐藏*/ CONSOLE_CURSOR_INFO cci; HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; system("color 5F");//改变控制台窗口背景色和文字颜色 system("mode con cols=30 lines=11");//定义控制台窗口大小 randDete(); randDete(); cci.bVisible = FALSE;//隐藏输入光标 cci.dwSize = sizeof(cci); SetConsoleCursorInfo(handle, &cci); printf_array(); /*游戏主要消息循环*/ while(1){ temp=getch();//等待用户按下键操作 system("cls"); printf_array(); is_moved=0; /*判断输入是或否合法*/ if(temp!=UP&&temp!=DOWN&&temp!=LEFT&&temp!=RIGHT&&temp!=ESC) //printf_array(); continue; else{ /*方向键主要操作*/ if(temp==RIGHT){ right_move(); if(is_moved==1){ randDete(); } printf_array(); printf("\n"); } if(temp==DOWN){ down_move(); if(is_moved==1){ randDete(); } printf_array(); printf("\n"); } if(temp==LEFT){ left_move(); if(is_moved==1){ randDete(); } printf_array(); printf("\n"); } if(temp==UP){ up_move(); if(is_moved==1){ randDete(); } printf_array(); printf("\n"); } /*使用ESC键重置游戏*/ if(temp==ESC){ for (i=0;i<4;i++) { for (j=0;j<4;j++) { array[i][j]=0; } } randDete(); randDete(); printf_array(); printf("\n"); } } } return 0; }