Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- using namespace std;
- using namespace System;
- #define LIMITE_ANCHO 100
- #define LIMITE_ALTO 25
- #define LIMITER 20
- #define DISPARO 32
- enum Direccion { Arriba = 72, Abajo = 80, Izquierda = 75, Derecha = 77 };
- enum Color { Negro, Rojo, Verde, Azul, Blanco, Celeste, Amarillo, Gris, Magenta, DarkRed, DarkBlue, DarkGreen, DarkCyan, DarkYellow, DarkGray, DarkMagenta };
- int posicionx = 0;
- int posiciony = 0;
- int posicionnavex = 0;
- int posicionnavey = 0;
- int posicionestrellax = 0;
- int posicionestrellay = 0;
- short aleartorio(short ini, short fin) {
- return rand() % (fin - ini + 1) + ini;
- }
- void colorearCaracter(Color color) {
- switch (color) {
- case Negro: Console::ForegroundColor = ConsoleColor::Black; break;
- case Rojo: Console::ForegroundColor = ConsoleColor::Red; break;
- case Verde: Console::ForegroundColor = ConsoleColor::Green; break;
- case Azul: Console::ForegroundColor = ConsoleColor::Blue; break;
- case Blanco: Console::ForegroundColor = ConsoleColor::White; break;
- case Celeste:Console::ForegroundColor = ConsoleColor::Cyan; break;
- case Amarillo:Console::ForegroundColor = ConsoleColor::Yellow; break;
- case Gris:Console::ForegroundColor = ConsoleColor::Gray; break;
- case Magenta:Console::ForegroundColor = ConsoleColor::Magenta; break;
- case DarkRed:Console::ForegroundColor = ConsoleColor::DarkRed; break;
- case DarkBlue:Console::ForegroundColor = ConsoleColor::DarkBlue; break;
- case DarkGreen:Console::ForegroundColor = ConsoleColor::DarkGreen; break;
- case DarkCyan:Console::ForegroundColor = ConsoleColor::DarkCyan; break;
- case DarkYellow:Console::ForegroundColor = ConsoleColor::DarkYellow; break;
- case DarkGray:Console::ForegroundColor = ConsoleColor::DarkGray; break;
- case DarkMagenta:Console::ForegroundColor = ConsoleColor::DarkMagenta; break;
- }
- }
- void colorearFondo(Color color) {
- switch (color) {
- case Negro: Console::BackgroundColor = ConsoleColor::Black; break;
- case Rojo: Console::BackgroundColor = ConsoleColor::Red; break;
- case Verde: Console::BackgroundColor = ConsoleColor::Green; break;
- case Azul: Console::BackgroundColor = ConsoleColor::Blue; break;
- case Blanco: Console::BackgroundColor = ConsoleColor::White; break;
- case Celeste:Console::BackgroundColor = ConsoleColor::Cyan; break;
- case Amarillo:Console::BackgroundColor = ConsoleColor::Yellow; break;
- case Gris:Console::BackgroundColor = ConsoleColor::Gray; break;
- case Magenta:Console::BackgroundColor = ConsoleColor::Magenta; break;
- case DarkRed:Console::BackgroundColor = ConsoleColor::DarkRed; break;
- case DarkBlue:Console::BackgroundColor = ConsoleColor::DarkBlue; break;
- case DarkGreen:Console::BackgroundColor = ConsoleColor::DarkGreen; break;
- case DarkCyan:Console::BackgroundColor = ConsoleColor::DarkCyan; break;
- case DarkYellow:Console::BackgroundColor = ConsoleColor::DarkYellow; break;
- case DarkGray:Console::BackgroundColor = ConsoleColor::DarkGray; break;
- case DarkMagenta:Console::BackgroundColor = ConsoleColor::DarkMagenta; break;
- }
- }
- /************************************************************************************************************************/
- typedef struct Bala {
- short _x, _y;
- char _img;
- Bala(short x = 0, short y = 0) {
- _x = x;
- _y = y;
- _img = '|';
- }
- void animar() {
- borrar();
- mover();
- dibujar();
- }
- void borrar() {
- Console::SetCursorPosition(_x, _y);
- cout << ' ';
- }
- void mover() {
- if (_y > 1) _y--;
- }
- void dibujar() {
- Console::SetCursorPosition(_x, _y);
- if (_y == 1) {
- cout << " ";
- }
- else
- cout << _img;
- }
- };
- typedef struct Nave {
- short _x, _y;
- short _n;
- short retraso = 0;
- short _vidas = 3;
- Bala** balas;
- const short _alto = 2, _ancho = 5;
- const char _img[2][5] = { { ' ', ' ', char(250), ' ', ' ' },
- { '(', '-', 'o', '-', ')' } };
- Nave(short x, short y, short n) {
- _x = x;
- _y = y;
- _n = n;
- cargarBalas();
- }
- ~Nave() {
- delete[] balas;
- }
- void animar(Direccion dir) {
- borrar();
- mover(dir);
- dibujar();
- }
- void borrar() {
- for (short f = 0; f < _alto; ++f) {
- Console::SetCursorPosition(_x, _y + f);
- for (short c = 0; c < _ancho; ++c) {
- cout << " ";
- }
- }
- }
- void mover(Direccion dir) {
- switch (dir) {
- case Arriba: if (_y - 1 > 0) _y--; break;
- case Abajo: if (_y + 1 < LIMITE_ALTO) _y++; break;
- case Izquierda: if (_x - 1 > 0) _x--; break;
- case Derecha: if (_x + 1 < LIMITE_ANCHO) _x++; break;
- posicionnavey = _y;
- posicionnavex = _x;
- }
- }
- void dibujar() {
- for (short f = 0; f < _alto; ++f) {
- Console::SetCursorPosition(_x, _y + f);
- for (short c = 0; c < _ancho; ++c) {
- cout << _img[f][c];
- }
- }
- }
- void cargarBalas() {
- balas = new Bala*[_n];
- for (short i = 0; i < _n; ++i)
- balas[i] = new Bala();
- }
- Bala* disparar() {
- if (_n == 0)
- return NULL;
- else {
- Bala* bala = balas[_n - 1];
- bala->_x = _x + 2;
- bala->_y = _y - 1;
- balas[_n - 1] = NULL;
- --_n;
- return bala;
- }
- }
- };
- typedef struct NaveEnemiga {
- short _x, _y, _dx, _dy, _retraso;
- char _img[1][5] = { '<','|','*', '|' ,'>' };
- short _vidas = 1;
- short _n;
- public:
- NaveEnemiga(short x = 0, short y = 0, short dx = 0, short dy = 0) {
- _x = x;
- _y = y;
- _dx = dx;
- _dy = dy;
- _retraso = 0;
- }
- void animar() {
- if (_retraso == 20) {
- borrar();
- mover();
- dibujar();
- _retraso = 0;
- }
- _retraso++;
- }
- void borrar() {
- for (short f = 0; f < 1; ++f) {
- Console::SetCursorPosition(_x, _y + f);
- for (short c = 0; c < 5; ++c) {
- cout << " ";
- }
- }
- }
- void mover() {
- if (_x + _dx < 1 || _x + _dx > LIMITE_ANCHO - 1) _dx *= -1;
- if (_y + _dy < 1 || _y + _dy > LIMITE_ALTO - 1) _dy *= -1;
- _x += _dx;
- }
- void dibujar() {
- for (short f = 0; f < 1; ++f) {
- Console::SetCursorPosition(_x, _y + f);
- for (short c = 0; c < 5; ++c) {
- cout << _img[f][c];
- }
- }
- }
- };
- void Marco() {
- colorearCaracter(Magenta);
- for (int i = 0; i <= LIMITE_ALTO + 1; ++i) {
- for (int j = 0; j <= LIMITE_ANCHO + 4; ++j)
- if (i == 0 || i == LIMITE_ALTO + 1 || j == 0 || j == LIMITE_ANCHO + 4)
- cout << "*";
- else
- cout << " ";
- cout << endl;
- }
- colorearCaracter(Blanco);
- }
- void imprimirMensaje(Nave &nave, int & contador, short &score) {
- Console::SetCursorPosition(LIMITE_ANCHO + 5, 0);
- cout << "Vidas: " << nave._vidas;
- Console::SetCursorPosition(LIMITE_ANCHO + 5, 2);
- if (contador <= 3)
- colorearCaracter(Rojo);
- cout << "Municion: " << contador;
- colorearCaracter(Blanco);
- Console::SetCursorPosition(LIMITE_ANCHO + 5, 4);
- cout << "Score: " << score;
- }
- inline bool choque(Nave &nave, NaveEnemiga &enemiga) {
- return nave._x == enemiga._x && nave._y == enemiga._y;
- }
- typedef struct Estrellas {
- short _x, _y, _dy, _retraso, _n;
- char _img = '*';
- public:
- Estrellas() {
- _x = aleartorio(1,99);
- _y = aleartorio(1,24);
- _dy = 1;
- _retraso = 0;
- }
- void animar() {
- if (_retraso == 3) {
- borrar();
- mover();
- dibujar();
- _retraso = 0;
- }
- _retraso++;
- }
- void borrar() {
- Console::SetCursorPosition(_x, _y);
- cout << " ";
- }
- void mover() {
- if (_y + _dy < 1 || _y + _dy > LIMITE_ALTO) _y = 1;
- _y += _dy;
- }
- void dibujar() {
- Console::SetCursorPosition(_x, _y);
- cout << _img;
- }
- };
- void Pantalla_de_Incio() {
- colorearCaracter(DarkCyan);
- Console::SetCursorPosition(11, 4);
- cout << "*************** ********** **** ********** *************** **********" << endl;
- Console::SetCursorPosition(10, 5);
- cout << "*************** *********** **** *********** *************** ***********" << endl;
- Console::SetCursorPosition(9, 6);
- cout << "**** **** **** **** **** **** **** **** ****" << endl;
- Console::SetCursorPosition(8, 7);
- cout << "**** **** **** **** **** **** **** **** ****" << endl;
- Console::SetCursorPosition(7, 8);
- cout << "**** **** **** **** **** **** **** **** ****" << endl;
- Console::SetCursorPosition(6, 9);
- cout << "*************** *************** **** *************** *************** ***************" << endl;
- Console::SetCursorPosition(5, 10);
- cout << "*************** **************** **** **************** *************** ****************" << endl;
- Console::SetCursorPosition(4, 11);
- cout << "**** **** **** **** **** **** **** **** **** **** ****" << endl;
- Console::SetCursorPosition(3, 12);
- cout << "**** **** **** **** **** **** **** **** **** **** ****" << endl;
- Console::SetCursorPosition(2, 13);
- cout << "*************** **** **** **************** **** **** *************** **** ****" << endl;
- Console::SetCursorPosition(1, 14);
- cout << "*************** **** **** **************** **** **** *************** **** ****" << endl << endl;
- colorearCaracter(Blanco);
- Console::SetCursorPosition(38, 16);
- cout << "Press any key to continue...";
- system("pause>0");
- system("cls");
- _getch;
- }
- /*************************************************************/
- void jugar() {
- int a;
- short Nivel1 = 0, Nivel2 = 0, Nivel3 = 0, Nivel4 = 0;
- short retraso = 0;
- short score = 0;
- short nEstrellas = 10;
- short nBalas = 10;
- int municion = nBalas;
- short nBalasActual = 0;
- Bala** balas = new Bala*[nBalas];
- for (short i = 0; i < nBalas; ++i)
- balas[i] = NULL;
- Nave* nave = new Nave(LIMITE_ANCHO / 2 - 3, LIMITE_ALTO - 3, nBalas);
- NaveEnemiga enemiga1 = NaveEnemiga(3, 9, -1, +1);
- NaveEnemiga enemiga2 = NaveEnemiga(9, 17, +1, -1);
- NaveEnemiga ene1 =NaveEnemiga (3,3,1,1);
- NaveEnemiga ene2 = NaveEnemiga(80, 15, 1, 1);
- Estrellas* es = new Estrellas[30];
- nave->animar(Direccion(Arriba));
- while (true) {
- if (Nivel1 == 1) {
- ene1._retraso = 10;
- ene1.animar();
- ene2.animar();
- }
- if (score==200&& Nivel1==0) { //Nivel 1 a Nivel 2
- Console::SetCursorPosition(LIMITE_ANCHO / 2-7, LIMITE_ALTO / 2);
- cout << "Nivel 2";
- if (Nivel2 == 100) {
- Console::SetCursorPosition(LIMITE_ANCHO / 2-7, LIMITE_ALTO / 2);
- cout << " ";
- Nivel1 = 1;;
- }
- Nivel2++;
- }
- {for (int i = 0; i < 30; ++i) {
- if ((nave->_x == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 1 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 2 == es[i]._x&& nave->_y == es[i]._y)
- || (nave->_x + 3 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 4 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 2 == es[i]._x&& nave->_y - 1 == es[i]._y)
- || (nave->_x + 1 == es[i]._x&& nave->_y - 1 == es[i]._y) || (nave->_x + 3 == es[i]._x&& nave->_y - 1 == es[i]._y) ||
- (nave->_x + 4 == es[i]._x&& nave->_y - 1 == es[i]._y) || (nave->_x == es[i]._x&& nave->_y - 1 == es[i]._y) ||
- (enemiga1._x == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x + 1 == es[i]._x && enemiga1._y - 1 == es[i]._y) ||
- (enemiga1._x + 2 == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x + 3 == es[i]._x && enemiga1._y - 1 == es[i]._y) ||
- (enemiga1._x + 4 == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 1 == es[i]._x && enemiga1._y == es[i]._y) ||
- (enemiga1._x + 2 == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 3 == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 4 == es[i]._x && enemiga1._y == es[i]._y) ||
- (enemiga2._x == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x + 1 == es[i]._x && enemiga2._y - 1 == es[i]._y) ||
- (enemiga2._x + 2 == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x + 3 == es[i]._x && enemiga2._y - 1 == es[i]._y) ||
- (enemiga2._x + 4 == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 1 == es[i]._x && enemiga2._y == es[i]._y) ||
- (enemiga2._x + 2 == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 3 == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 4 == es[i]._x && enemiga2._y == es[i]._y))
- es[i].borrar();
- else
- es[i].animar();
- }
- imprimirMensaje(*nave, municion, score);
- if (municion == 0) {
- system("cls"); break;
- }
- if (enemiga1._vidas > 0) {
- enemiga1.animar();
- for (int i = 0; i < nBalas; ++i) {
- if (balas[i] != NULL) {
- if ((balas[i]->_x == enemiga1._x + 2 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 1 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 3 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 4 && balas[i]->_y == enemiga1._y)) {
- enemiga1._vidas = 0;
- score += 100;
- retraso = 0;
- }
- }
- }
- }
- if (enemiga1._vidas == 0) {
- enemiga1.borrar();
- Console::SetCursorPosition(enemiga1._x + 1, enemiga1._y);
- cout << "100";
- if (retraso == 30) {
- Console::SetCursorPosition(enemiga1._x + 1, enemiga1._y);
- cout << " ";
- enemiga1._vidas = -1;
- }
- retraso++;
- if (enemiga1._vidas == -1)
- retraso = 0;
- }
- if (enemiga2._vidas > 0) {
- enemiga2.animar();
- for (int i = 0; i < nBalas; ++i) {
- if (balas[i] != NULL) {
- if ((balas[i]->_x == enemiga2._x + 2 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 1 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 3 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 4 && balas[i]->_y == enemiga2._y)) {
- enemiga2._vidas = 0;
- score += 100;
- retraso = 0;
- }
- }
- }
- }
- if (enemiga2._vidas == 0) {
- enemiga2.borrar();
- Console::SetCursorPosition(enemiga2._x + 1, enemiga2._y);
- cout << "100";
- if (retraso == 30) {
- Console::SetCursorPosition(enemiga2._x + 1, enemiga2._y);
- cout << " ";
- enemiga2._vidas = -1;
- }
- retraso++;
- if (enemiga2._vidas == -1)
- retraso = 0;
- }
- if (choque(*nave, enemiga1) || choque(*nave, enemiga2)) {
- if (enemiga1._vidas <= 0)
- nave->_vidas = nave->_vidas;
- else
- nave->_vidas--;
- }
- if (kbhit()) {
- char tecla = getch();
- if (tecla == DISPARO) {
- balas[nBalasActual] = nave->disparar();
- municion--;
- nBalasActual++;
- }
- if (Direccion(tecla) == Direccion::Arriba ||
- Direccion(tecla) == Direccion::Abajo ||
- Direccion(tecla) == Direccion::Izquierda ||
- Direccion(tecla) == Direccion::Derecha) {
- nave->animar(Direccion(tecla));
- }
- if (tecla == 13) {
- Console::SetCursorPosition(LIMITE_ANCHO / 2 - 4, LIMITE_ALTO / 2);
- cout << "PAUSE";
- system("pause>0");
- if (tecla == 13) {
- Console::SetCursorPosition(LIMITE_ANCHO / 2 - 4, LIMITE_ALTO / 2);
- cout << " ";
- }
- else
- system("pause>0");
- }
- }
- for (short i = 0; i < nBalasActual; ++i) {
- balas[i]->animar();
- }
- _sleep(10);
- }
- }
- delete[] es;
- Console::SetCursorPosition(LIMITE_ANCHO / 2, LIMITE_ALTO / 2);
- cout << "GAME OVER";
- Console::SetCursorPosition(LIMITE_ANCHO, LIMITE_ALTO);
- cin >> a;
- if (a == 1890)
- system("cls");
- Console::SetCursorPosition(LIMITE_ANCHO / 2, LIMITE_ALTO / 2);
- cout << "You win";
- system("pause>0");
- }
- /*****************************************************************/
- int main() {
- srand(time(NULL));
- Console::CursorVisible = false;
- Pantalla_de_Incio();
- Marco();
- jugar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement