Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <conio.h>
- using namespace std;
- using namespace System;
- #define FILAS 20
- #define COLUMNAS 20
- enum Terreno { Camino, Pared };
- enum Movimiento {ARRIBA=72, ABAJO=80, IZQUIERDA=75, DERECHA=77};
- //Las balas del protagonista matan al enemigo
- //El enemigo mata al protagonista si lo choca
- //Definir a criterio propio, cuando se gana y cuando se pierde
- class CEscenario {
- //Definir las paredes para que nadie salga de la consola
- short mapa[FILAS][COLUMNAS]={
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,0,0,0,1,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,0,0,0,0,0,1},
- {1,0,0,0,0,1,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,1,1,1,1,1},
- {1,0,0,0,0,1,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,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,1,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,1,1,1,1,1,1,1,1,1,1,1,1}
- };
- public:
- CEscenario() {
- this->dibujar();
- }
- bool esCamino(short x, short y) {
- //suponiendo que en el mapa el valor 0 es camino
- return mapa[y][x] == Camino;
- }
- private:
- void dibujar() {
- for (short f = 0; f < FILAS; f++) {
- for (short c = 0; c < COLUMNAS; c++)
- cout << (this->mapa[f][c]==Pared ? "*" : " ");
- cout << endl;
- }
- }
- };
- class CBala {
- };
- class CEnemigo {
- short x, y;
- public:
- CEnemigo(short x, short y) {
- this->x = x;
- this->y = y;
- this->dibujar();
- }
- ~CEnemigo() { }
- void mover() {
- Random aleatorio;
- this->borrar();
- this->x += aleatorio.Next(-1, 2);
- this->y += aleatorio.Next(-1, 2);
- this->dibujar();
- }
- private:
- void borrar() {
- Console::SetCursorPosition(this->x, this->y);
- cout << " ";
- }
- void dibujar() {
- Console::SetCursorPosition(this->x, this->y);
- cout << "X";
- }
- };
- class CProtagonista {
- short x, y;
- vector<CBala*> balas;
- public:
- CProtagonista()
- : x(1), y(1) {
- this->dibujar();
- }
- ~CProtagonista(){
- for (CBala* bala : balas)
- delete bala;
- }
- void mover(CEscenario* esc, Movimiento mov) {
- this->borrar();
- //Evaluas si se mueve a pared o no
- switch (mov) {
- case ARRIBA : if (esc->esCamino(this->x, this->y - 1)) this->y--; break;
- case ABAJO : if (esc->esCamino(this->x, this->y + 1)) this->y++; break;
- case IZQUIERDA: if (esc->esCamino(this->x - 1, this->y)) this->x--; break;
- case DERECHA : if (esc->esCamino(this->x + 1, this->y)) this->x++; break;
- }
- this->dibujar();
- }
- void disparar() {
- //disparar balas hacia arriba(w)/abajo(s)/izquierda(a)/derecha(d)
- }
- private:
- void borrar() {
- Console::SetCursorPosition(this->x, this->y);
- cout << " ";
- }
- void dibujar() {
- Console::SetCursorPosition(this->x, this->y);
- cout << "O";
- }
- };
- class CJuego {
- CEscenario* escenario;
- CProtagonista* prot;
- vector<CEnemigo*> enemigos;
- public:
- CJuego() {
- this->escenario = new CEscenario;
- this->prot = new CProtagonista();
- this->inicializarEnemigos(5);
- }
- ~CJuego() {
- delete this->prot;
- for (CEnemigo* enemigo : this->enemigos)
- delete enemigo;
- }
- bool jugar() {
- char delay = 0;
- while (true) {
- if (kbhit())
- prot->mover(this->escenario, Movimiento(getch()));
- if (!delay++) this->moverEnemigos();
- }
- return true;
- }
- private:
- void inicializarEnemigos(short n) {
- Random aleatorio;
- for (short i = 0; i < n; i++) {
- short x = aleatorio.Next(15, 21);
- short y = aleatorio.Next(15, 21);
- CEnemigo* nuevoEnemigo = new CEnemigo(x, y);
- this->enemigos.push_back(nuevoEnemigo);
- }
- }
- void moverEnemigos() {
- for (CEnemigo* enemigo : this->enemigos)
- enemigo->mover();
- }
- };
- int main() {
- Console::CursorVisible = false;
- CJuego juego;
- juego.jugar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement