1
Fork 0
mirror of https://github.com/Steffo99/iiiiil-gioco.git synced 2024-11-25 17:34:18 +00:00
iiiiil-gioco/main.cpp

327 lines
8.6 KiB
C++
Raw Normal View History

#include <iostream>
#include <random>
#include <time.h>
#include <conio.h>
2015-12-29 18:06:47 +00:00
#include <stdlib.h>
#define X_MAX 80
#define Y_MAX 23
#define ROOMS 8
#define ROOM_SIZE 7
#define ENEMIES_IN_LEVEL 5
2015-12-27 15:46:41 +00:00
#define WALL 0xB2
#define EMPTY 0xFF
#define PLAYER 0x02
#define ENEMY 'X'
2015-12-27 15:46:41 +00:00
using namespace std;
2015-12-29 18:51:19 +00:00
//Mappa del gioco
unsigned char map[X_MAX][Y_MAX];
//Classe dei nemici
class Enemy
{
public:
int x;
int y;
void move()
{
2015-12-29 19:11:34 +00:00
//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)
{
cout << "S";
map[x][y] = EMPTY;
map[x-1][y] = ENEMY;
x--;
moving = false;
}
break;
case 1: //Destra
if(map[x+1][y] == EMPTY)
{
cout << "D";
map[x][y] = EMPTY;
map[x+1][y] = ENEMY;
x++;
moving = false;
}
break;
case 2: //Su
if(map[x][y-1] == EMPTY)
{
cout << "U";
map[x][y] = EMPTY;
map[x][y-1] = ENEMY;
y--;
moving = false;
}
break;
case 3: //Giù
if(map[x][y+1] == EMPTY)
{
cout << "G";
map[x][y] = EMPTY;
map[x][y+1] = ENEMY;
y++;
moving = false;
}
break;
}
}
}
}
};
2015-12-29 18:51:19 +00:00
//Fai muovere il giocatore
void move(int player[2])
{
int player_x = player[0];
int player_y = player[1];
bool waiting = true;
2015-12-29 18:18:21 +00:00
//Rileva i tasti freccia
while(waiting)
{
if(getch() == 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;
}
}
}
player[0] = player_x;
player[1] = player_y;
}
//Aggiorna la console con la situazione corrente del gioco.
2015-12-29 18:51:19 +00:00
void draw()
{
2015-12-29 18:18:21 +00:00
//Svuota lo schermo della console. Sono sicuro che ci sia un modo molto migliore per farlo, ma non mi viene in mente...
2015-12-29 18:06:47 +00:00
system("cls");
for(int y=0; y<Y_MAX; y++)
{
for(int x=0; x<X_MAX; x++)
{
cout << map[x][y];
}
}
}
//Funzioni per la generazione della mappa
//Inizializza la mappa con spazi vuoti
2015-12-29 18:51:19 +00:00
void init()
{
for(int y=0; y<Y_MAX; y++)
{
for(int x=0; x<X_MAX; x++)
{
2015-12-27 15:46:41 +00:00
map[x][y] = WALL;
}
}
}
//Crea una stanza quadrata
2015-12-29 18:51:19 +00:00
void room(int start_x, int start_y, int end_x, int end_y)
{
for(int y=start_y; y<=end_y; y++)
{
for(int x=start_x; x<=end_x; x++)
{
2015-12-27 15:46:41 +00:00
map[x][y] = EMPTY;
}
}
}
//Crea un corridoio che connetta due punti
2015-12-29 18:51:19 +00:00
void corridor(int start_x, int start_y, int end_x, int end_y, bool verticale)
{
if(verticale)
{
if(start_y > end_y)
{
for(int y=end_y; y<=start_y; y++)
{
2015-12-27 15:46:41 +00:00
map[start_x][y] = EMPTY;
}
}
else
{
for(int y=start_y; y<=end_y; y++)
{
2015-12-27 15:46:41 +00:00
map[start_x][y] = EMPTY;
}
}
if(start_x > end_x)
{
for(int x=end_x; x<=start_x; x++)
{
2015-12-27 15:46:41 +00:00
map[x][end_y] = EMPTY;
}
}
else
{
for(int x=start_x; x<=end_x; x++)
{
2015-12-27 15:46:41 +00:00
map[x][end_y] = EMPTY;
}
}
}
else
{
if(start_x > end_x)
{
for(int x=end_x; x<=start_x; x++)
{
2015-12-27 15:46:41 +00:00
map[x][start_y] = EMPTY;
}
}
else
{
for(int x=start_x; x<=end_x; x++)
{
2015-12-27 15:46:41 +00:00
map[x][start_y] = EMPTY;
}
}
if(start_y > end_y)
{
for(int y=end_y; y<=start_y; y++)
{
2015-12-27 15:46:41 +00:00
map[end_x][y] = EMPTY;
}
}
else
{
for(int y=start_y; y<=end_y; y++)
{
2015-12-27 15:46:41 +00:00
map[end_x][y] = EMPTY;
}
}
}
}
2015-12-29 18:51:19 +00:00
void generate(int player[2], Enemy* list[ENEMIES_IN_LEVEL])
{
int corridor_x;
int corridor_y;
//Creazione delle stanze
for(int r=0; r<ROOMS; r++)
{
int size_x = rand() % ROOM_SIZE + 1;
int size_y = rand() % ROOM_SIZE + 1;
int start_x = rand() % (X_MAX - size_x - 2) + 1;
int start_y = rand() % (Y_MAX - size_y - 2) + 1;
2015-12-29 18:51:19 +00:00
room(start_x, start_y, start_x + size_x, start_y + size_y);
2015-12-29 18:18:21 +00:00
//Se non è la prima stanza, crea un corridoio che connetta quella appena generata con quella precedente
if(r > 0)
{
int link_x = rand() % size_x + 1 + start_x;
int link_y = rand() % size_y + 1 + start_y;
2015-12-29 18:51:19 +00:00
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;
2015-12-29 18:18:21 +00:00
//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; e<ENEMIES_IN_LEVEL; e++)
{
while(true)
{
int x = rand() % (X_MAX - 1) + 1;
int y = rand() % (Y_MAX - 1) + 1;
if(map[x][y] == EMPTY)
{
map[x][y] = ENEMY;
Enemy* created = new Enemy();
created->x = x;
created->y = y;
list[e] = created;
break;
}
}
}
}
2015-12-29 18:51:19 +00:00
//Processa il resto di un turno, dopo il movimento del giocatore.
void tick(Enemy* list[ENEMIES_IN_LEVEL])
{
for(int e=0; e<ENEMIES_IN_LEVEL; e++)
{
2015-12-29 19:11:34 +00:00
cout << "|" << list[e];
2015-12-29 18:51:19 +00:00
list[e]->move();
}
}
int main()
{
int player[2];
Enemy* list[ENEMIES_IN_LEVEL];
srand(0); //TODO: Rendere il seed modificabile...?
2015-12-29 18:51:19 +00:00
init();
generate(player, list);
draw();
2015-12-29 18:18:21 +00:00
//Ciclo principale del gioco
while(true)
{
2015-12-29 18:51:19 +00:00
move(player);
tick(list);
draw();
}
return 0;
}