#include #include #include #include #include #define X_MAX 80 #define Y_MAX 23 #define ROOMS 8 #define ROOM_SIZE 7 #define ENEMIES_IN_LEVEL 5 #define WALL 0xB2 #define EMPTY 0xFF #define PLAYER 0x02 #define ENEMY 'X' using namespace std; //Mappa del gioco unsigned char map[X_MAX][Y_MAX]; //Classe dei nemici class Enemy { public: int x; int y; void move() { //Se intorno c'è il giocatore if(map[x-1][y] == PLAYER || map[x+1][y] == PLAYER || map[x][y-1] == PLAYER || map[x][y+1] == PLAYER) { //ATTACCO! } else { //Muoviti in una direzione casuale bool moving = true; while(moving) { int direction = rand() % 4; switch(direction) { case 0: //Sinistra if(map[x-1][y] == EMPTY) { map[x][y] = EMPTY; map[x-1][y] = ENEMY; x--; moving = false; } break; case 1: //Destra if(map[x+1][y] == EMPTY) { map[x][y] = EMPTY; map[x+1][y] = ENEMY; x++; moving = false; } break; case 2: //Su if(map[x][y-1] == EMPTY) { map[x][y] = EMPTY; map[x][y-1] = ENEMY; y--; moving = false; } break; case 3: //Giù if(map[x][y+1] == EMPTY) { map[x][y] = EMPTY; map[x][y+1] = ENEMY; y++; moving = false; } break; } } } } }; //Fai muovere il giocatore void move(int player[2]) { int player_x = player[0]; int player_y = player[1]; bool waiting = true; //Rileva i tasti freccia while(waiting) { unsigned char input = getch(); if(input == 224) { switch(getch()) { case 72: //Freccia su if(map[player_x][player_y-1] == EMPTY) { map[player_x][player_y] = EMPTY; map[player_x][player_y-1] = PLAYER; player_y--; waiting = false; } break; case 80: //Freccia giù if(map[player_x][player_y+1] == EMPTY) { map[player_x][player_y] = EMPTY; map[player_x][player_y+1] = PLAYER; player_y++; waiting = false; } break; case 75: //Freccia sinistra if(map[player_x-1][player_y] == EMPTY) { map[player_x][player_y] = EMPTY; map[player_x-1][player_y] = PLAYER; player_x--; waiting = false; } break; case 77: //Freccia destra if(map[player_x+1][player_y] == EMPTY) { map[player_x][player_y] = EMPTY; map[player_x+1][player_y] = PLAYER; player_x++; waiting = false; } break; } } else if(input == 115) //S { //Salta un turno waiting = false; } } player[0] = player_x; player[1] = player_y; } //Aggiorna la console con la situazione corrente del gioco. void draw() { //Svuota lo schermo della console. Sono sicuro che ci sia un modo molto migliore per farlo, ma non mi viene in mente... system("cls"); for(int y=0; y end_y) { for(int y=end_y; y<=start_y; y++) { map[start_x][y] = EMPTY; } } else { for(int y=start_y; y<=end_y; y++) { map[start_x][y] = EMPTY; } } if(start_x > end_x) { for(int x=end_x; x<=start_x; x++) { map[x][end_y] = EMPTY; } } else { for(int x=start_x; x<=end_x; x++) { map[x][end_y] = EMPTY; } } } else { if(start_x > end_x) { for(int x=end_x; x<=start_x; x++) { map[x][start_y] = EMPTY; } } else { for(int x=start_x; x<=end_x; x++) { map[x][start_y] = EMPTY; } } if(start_y > end_y) { for(int y=end_y; y<=start_y; y++) { map[end_x][y] = EMPTY; } } else { for(int y=start_y; y<=end_y; y++) { map[end_x][y] = EMPTY; } } } } void generate(int player[2], Enemy* list[ENEMIES_IN_LEVEL]) { int corridor_x; int corridor_y; //Creazione delle stanze for(int r=0; r 0) { int link_x = rand() % size_x + 1 + start_x; int link_y = rand() % size_y + 1 + start_y; corridor(link_x, link_y, corridor_x, corridor_y, rand() % 2); } corridor_x = rand() % size_x + start_x; corridor_y = rand() % size_y + start_y; //Posiziona il giocatore se è l'ultima stanza if(r == ROOMS - 1) { player[0] = corridor_x; player[1] = corridor_y; map[corridor_x][corridor_y] = PLAYER; } } for(int e=0; ex = x; created->y = y; list[e] = created; break; } } } } //Processa il resto di un turno, dopo il movimento del giocatore. void tick(Enemy* list[ENEMIES_IN_LEVEL]) { for(int e=0; emove(); } } int main() { int player[2]; Enemy* list[ENEMIES_IN_LEVEL]; srand(0); //TODO: Rendere il seed modificabile...? init(); generate(player, list); draw(); //Ciclo principale del gioco while(true) { move(player); tick(list); draw(); } return 0; }