#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int LevelChest = 3;
bool DungeonCompleted = false;
int map[10][10] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 3, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 2, 2, 3, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 3, 2, 0, 0, 0, 0, 0, 4, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
void gotoxy(int x, int y) {
HANDLE Console_Handle;
Console_Handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(Console_Handle, coord);
}
void hide_cursor() {
HANDLE Console_Handle;
Console_Handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = 0;
cursor.dwSize = 1;
SetConsoleCursorInfo(Console_Handle, &cursor);
}
struct Inventory {
int Keys;
int Chests;
};
struct PLAYER {
int X;
int Y;
Inventory I;
void SetXY(int x, int y) { X = x; Y = y; }
}Player;
void draw_tile(int x, int y) {
switch(map[y][x]) {
case 0:
printf(" ");
break;
case 1:
printf("#");
break;
case 2:
printf("C");
break;
case 3:
printf("L");
break;
case 4:
if(Player.I.Chests >= LevelChest) { printf("E"); } else {printf(" ");}
break;
}
}
void draw_map() {
gotoxy(0, 2);
int px = Player.X;
int py = Player.Y;
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
if(px == x && py == y) {
printf("P");
} else {
draw_tile(x, y);
}
if(x == 9) { printf("\n"); }
}
}
}
void draw_notice(char string[10]) {
gotoxy(0, 0);
for(int i = 0; i < 80; i++) { printf(" "); }
gotoxy(0, 0);
printf("%s", string);
}
void pick_up() {
switch(map[Player.Y][Player.X]) {
case 2:
if(Player.I.Keys > 0) {
map[Player.Y][Player.X] = 0;
Player.I.Keys--;
Player.I.Chests++;
draw_notice("Cofre abierto!");
} else { draw_notice("Necesitas una llave!"); }
break;
case 3:
map[Player.Y][Player.X] = 0;
Player.I.Keys++;
draw_notice("Llave recogida!");
case 4:
if(Player.I.Chests >= LevelChest) {
draw_notice("Felicidades completaste el calabozo! Presiona ESC para salir");
DungeonCompleted = true;
}
}
}
bool passable(int x, int y) {
if(map[x][y] == 1) { return false; }
return true;
}
int move_down() {
if(passable(Player.Y + 1, Player.X)) {
gotoxy(Player.X, Player.Y + 2);
draw_tile(Player.X, Player.Y);
Player.Y += 1;
if(Player.Y > 9) { Player.Y = 9; }
gotoxy(Player.X, Player.Y + 2);
printf("P");
}
}
int move_left() {
if(passable(Player.Y, Player.X - 1)) {
gotoxy(Player.X, Player.Y + 2);
draw_tile(Player.X, Player.Y);
Player.X -= 1;
if(Player.X < 0) { Player.X = 0; }
gotoxy(Player.X, Player.Y + 2);
printf("P");
}
}
int move_right() {
if(passable(Player.Y, Player.X + 1)) {
gotoxy(Player.X, Player.Y + 2);
draw_tile(Player.X, Player.Y);
Player.X += 1;
if(Player.X > 9) { Player.X = 9; }
gotoxy(Player.X, Player.Y + 2);
printf("P");
}
}
int move_up() {
if(passable(Player.Y - 1, Player.X)) {
gotoxy(Player.X, Player.Y + 2);
draw_tile(Player.X, Player.Y);
Player.Y -= 1;
if(Player.Y < 0) { Player.Y = 0; }
gotoxy(Player.X, Player.Y + 2);
printf("P");
}
}
void move_to(int new_x, int new_y) {
Player.SetXY(new_x, new_y);
}
void draw_keys() {
gotoxy(0, 13);
printf("Llaves: %d", Player.I.Keys);
}
int main(void){
char key;
hide_cursor();
move_to(1, 1);
draw_map();
draw_keys();
gotoxy(0, 15);
printf("WASD: Moverse\n");
printf("Enter: Accion\n");
printf("P: Jugador\n");
printf("L: Llave\n");
printf("C: Cofre\n");
printf("E: Escaleras\n");
printf("#: Muros\n");
while(1) {
if(DungeonCompleted == true) {
if(kbhit()) {
key = getch();
if(key == 27) { return 0; }
}
continue;
}
if(kbhit()) {
key = getch();
switch(key) {
case 's':
move_down();
draw_notice("");
break;
case 'a':
move_left();
draw_notice("");
break;
case 'd':
move_right();
draw_notice("");
break;
case 'w':
move_up();
draw_notice("");
break;
case 13:
pick_up();
draw_keys();
if(Player.I.Chests >= LevelChest) { draw_map(); }
break;
case 27:
return 0;
}
}
}
return 0;
}