Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- using namespace std;
- using namespace System;
- #define Arriba 'w'
- #define Abajo 's'
- #define Izquierda 'a'
- #define Derecha 'd'
- #define Blanco 0
- #define Negro 1
- #define Rojo 2
- void configurarColorFondo(short color) {
- switch (color)
- {
- case Blanco: Console::BackgroundColor = ConsoleColor::White; break;
- case Negro: Console::BackgroundColor = ConsoleColor::Black; break;
- }
- }
- void configurarColorTexto(short color) {
- switch (color)
- {
- case Blanco: Console::ForegroundColor = ConsoleColor::White; break;
- case Negro: Console::ForegroundColor = ConsoleColor::Black; break;
- case Rojo: Console::ForegroundColor = ConsoleColor::Red; break;
- }
- }
- class CEscenario {
- //Caracteristicas
- const short Filas = 20;
- const short Columnas = 20;
- short Mapa[20][20] = {
- { 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- { 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1 }
- };
- public:
- //Funciones
- CEscenario() {
- this->mostrar();
- }
- bool esCamino(short x, short y) {
- return (x >= 0 && x < 20 &&
- y >= 0 && y < 20 &&
- Mapa[y][x] == Blanco);
- }
- private:
- void mostrar() {
- for (short fila = 0; fila < Filas; fila++) {
- for (short columna = 0; columna < Columnas; columna++) {
- Console::SetCursorPosition(columna, fila);
- short color = Mapa[fila][columna];
- configurarColorFondo(color);
- cout << " ";
- }
- }
- }
- };
- class CEnemigo {
- private:
- //Caracteristicas
- short x, y;
- short dx = 1;
- short retraso = 5000, cont = 0;
- //Funciones
- public:
- CEnemigo(CEscenario &esc) {
- this->asignarLugar(esc);
- _sleep(500);
- }
- void mover(CEscenario &esc) {
- if (cont == retraso) {
- borrar();
- if (!esc.esCamino(x + dx, y)) dx *= -1;
- x += dx;
- dibujar();
- cont = 0;
- }
- cont++;
- }
- short getX() {return this->x; }
- short getY() { return this->y; }
- private:
- void asignarLugar(CEscenario &esc) {
- Random r;
- do {
- x = r.Next(0, 20);
- y = r.Next(0, 20);
- } while (!esc.esCamino(x, y));
- }
- void borrar() {
- Console::SetCursorPosition(x, y);
- configurarColorFondo(Blanco);
- cout << " ";
- }
- void dibujar() {
- Console::SetCursorPosition(x, y);
- configurarColorTexto(Negro);
- cout << "*";
- }
- };
- class CProtagonista {
- //Caracteristicas
- short x = 1, y = 1;
- //Funciones
- public:
- CProtagonista(short x, short y) {
- this->x = x;
- this->y = y;
- this->dibujar();
- }
- void mover(CEscenario &esc, char mov) {
- borrar();
- if (mov == Arriba && esc.esCamino(x, y - 1)) y--;
- else if (mov == Abajo && esc.esCamino(x, y + 1)) y++;
- else if (mov == Derecha && esc.esCamino(x + 1, y)) x++;
- else if (mov == Izquierda && esc.esCamino(x - 1, y)) x--;
- dibujar();
- }
- void matar(CEnemigo* &e) {
- if (this->x == e->getX() && this->y == e->getY()) {
- delete e;
- e = NULL;
- }
- }
- private:
- void borrar() {
- Console::SetCursorPosition(x, y);
- configurarColorFondo(Blanco);
- cout << " ";
- }
- void dibujar() {
- Console::SetCursorPosition(x, y);
- configurarColorTexto(Rojo);
- cout << char(1);
- }
- };
- void jugar() {
- Console::CursorVisible = false;
- CEscenario esc;
- CProtagonista prot = CProtagonista(1,1);
- CEnemigo *e1 = new CEnemigo(esc);
- CEnemigo *e2 = new CEnemigo(esc);
- configurarColorFondo(Blanco);
- while (true) {
- //Si se apretó alguna tecla
- if (kbhit())
- prot.mover(esc, getch());
- //Si el enemigo e1 está vivo
- if (e1) {
- e1->mover(esc);
- prot.matar(e1);
- }
- //Si el enemigo e2 está vivo
- if (e2) {
- e2->mover(esc);
- prot.matar(e2);
- }
- }
- }
- int main() {
- jugar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement