Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <ctype.h>
- #include <conio.h>
- #include <vector>
- #define ARRIBA 72
- #define IZQUIERDA 75
- #define ABAJO 80
- #define DERECHA 77
- #define ALTO 2
- #define ANCHO 5
- int vidas = 3;
- /**/
- #define ALEATORIO(NUM1, NUM2) rand() % (NUM2 - NUM1 + 1) + NUM1;
- using namespace std;
- using namespace System;
- enum Color { NEGRO, ROJO, AZUL, BLANCO, VERDE, AMARILLO };
- void CambiarColorFondo(Color c) {
- switch (c)
- {
- case NEGRO: Console::BackgroundColor = ConsoleColor::Black; break;
- case ROJO: Console::BackgroundColor = ConsoleColor::Red; break;
- case AZUL: Console::BackgroundColor = ConsoleColor::Blue; break;
- case BLANCO: Console::BackgroundColor = ConsoleColor::White; break;
- case VERDE: Console::BackgroundColor = ConsoleColor::Green; break;
- case AMARILLO: Console::BackgroundColor = ConsoleColor::Yellow; break;
- }
- }
- void CambiarColorLetra(Color c) {
- switch (c)
- {
- {
- case NEGRO: Console::ForegroundColor = ConsoleColor::Black; break;
- case ROJO: Console::ForegroundColor = ConsoleColor::Red; break;
- case AZUL: Console::ForegroundColor = ConsoleColor::Blue; break;
- case BLANCO: Console::ForegroundColor = ConsoleColor::White; break;
- case VERDE: Console::ForegroundColor = ConsoleColor::Green; break;
- case AMARILLO: Console::ForegroundColor = ConsoleColor::Yellow; break;
- }
- }
- }
- typedef struct personaje {
- short x;
- short y;
- int pasos;
- char imagen[ALTO][ANCHO];
- personaje(short a = 50, short b = 35, short c = 0) {
- x = a;
- y = b;
- pasos = c;
- imagen[0][0] = ' ';
- imagen[0][1] = ':';
- imagen[0][2] = '^';
- imagen[0][3] = ':';
- imagen[0][4] = ' ';
- imagen[1][0] = ':';
- imagen[1][1] = '^';
- imagen[1][2] = ':';
- imagen[1][3] = '^';
- imagen[1][4] = ':';
- }
- void animar(char direccion) {
- borrar();
- mover(direccion);
- restriccion();
- dibujar();
- }
- void dibujar() {
- for (int i = 0; i < ALTO; i++) {
- Console::SetCursorPosition(x, y + i);
- for (int j = 0; j < ANCHO; j++) {
- if (j == 1 && i == 0 || j == 3 && i == 0 || j == 0 && i == 1 || j == 2 && i == 1 || j == 4 && i == 1) {
- CambiarColorLetra(ROJO);
- }
- else {
- CambiarColorLetra(BLANCO);
- }
- cout << imagen[i][j];
- }
- }
- }
- void borrar() {
- for (int i = 0; i < ALTO; i++) {
- Console::SetCursorPosition(x, y + i);
- for (int j = 0; j < ANCHO; j++) {
- cout << ' ';
- }
- }
- }
- void mover(char direccion) {
- switch (toupper(direccion)) {
- case ARRIBA: y -= 3; pasos++; break;
- case ABAJO: y += 3; pasos++; break;
- case IZQUIERDA: x -= 3; pasos++; break;
- case DERECHA: x += 3; pasos++; break;
- }
- }
- void restriccion() {
- if (x == -1) {
- x = 0;
- }
- else if (y == -1) {
- y = 0;
- }
- }
- };
- typedef struct comandante {
- short x;
- short y;
- int pasos;
- short dx;
- short dy;
- short retraso;
- int xbala;
- int ybala;
- int dybala;
- short retraso1 = 0;
- int disparar;
- char imagen[3][6];
- comandante(short a = 30, short b = 15, short c = 0) {
- x = a;
- y = b;
- pasos = c;
- dx = 1;
- dy = 1;
- retraso = 0;
- xbala = ALEATORIO(30, 60);
- ybala = y;
- dybala = 1;
- imagen[0][0] = ' ';
- imagen[0][1] = ' ';
- imagen[0][2] = '*';
- imagen[0][3] = '*';
- imagen[0][4] = ' ';
- imagen[0][5] = ' ';
- imagen[1][0] = ' ';
- imagen[1][1] = ' ';
- imagen[1][2] = '*';
- imagen[1][3] = '*';
- imagen[1][4] = ' ';
- imagen[1][5] = ' ';
- imagen[2][0] = '{';
- imagen[2][1] = '¨';
- imagen[2][2] = ' ';
- imagen[2][3] = ' ';
- imagen[2][4] = '¨';
- imagen[2][5] = '}';
- }
- ~comandante() {
- borrar();
- Console::SetCursorPosition(xbala, ybala);
- cout << " ";
- }
- void animar(personaje* &personajeA) {
- if (retraso == 1000) {
- scoreVidas(personajeA);
- borrar();
- mover();
- restriccion();
- dibujar();
- animarBala();
- if (ybala == 45) {
- borrarBala();
- }
- retraso = 0;
- }
- retraso++;
- }
- void dibujar() {
- for (int i = 0; i < 3; i++) {
- Console::SetCursorPosition(x, y + i);
- for (int j = 0; j < 6; j++) {
- if (i < 2) {
- CambiarColorLetra(AZUL);
- }
- else {
- CambiarColorLetra(AMARILLO);
- }
- cout << imagen[i][j];
- }
- }
- }
- void borrar() {
- for (int i = 0; i < 3; i++) {
- Console::SetCursorPosition(x, y + i);
- for (int j = 0; j < 6; j++) {
- cout << ' ';
- }
- }
- }
- void mover() {
- if (x == 1 || x == 100) {
- dx *= -1;
- }
- x += dx;
- }
- void restriccion() {
- if (x == -1) {
- x = 0;
- }
- else if (y == -1) {
- y = 0;
- }
- }
- void scoreVidas(personaje* &personajeA) {
- Console::SetCursorPosition(3, 1);
- cout << "Vidas: " << vidas;
- for (int i = 0; i < 5; ++i) {
- if (xbala == personajeA->x + i && ybala == personajeA->y) {
- vidas--;
- }
- if (vidas == 1 || vidas == 2) {
- personajeA->dibujar();
- }
- }
- }
- void crearBala() {
- Console::SetCursorPosition(xbala, ybala);
- cout << "*";
- }
- void borrarBala() {
- Console::SetCursorPosition(xbala, ybala);
- cout << " ";
- }
- void animarBala() {
- borrarBala();
- if (ybala == 45) {
- disparar = ALEATORIO(30, 60);
- }
- if (ybala == 45) {
- borrarBala();
- }
- if (ybala < 45) {
- ybala += dybala;
- }
- if (x == disparar) {
- xbala = x + 3;
- ybala = y + 3;
- }
- crearBala();
- }
- };
- typedef struct bala {
- short x;
- short y;
- bala(short a = 2, short b = 3) {
- x = a;
- y = b;
- }
- void animar(comandante* &comandanteA) {
- Console::SetCursorPosition(x, y);
- cout << " ";
- if (y > 0) {
- y--;
- }
- Console::SetCursorPosition(x, y);
- cout << "*";
- eliminarEnemigo(comandanteA);
- }
- void eliminarEnemigo(comandante* &comandanteA) {
- }
- ~bala() {
- Console::SetCursorPosition(x, y);
- cout << " ";
- }
- };
- void game() {
- int retraso = 0;
- int retrasov = 0;
- Random random;
- personaje* personajeA = new personaje[1];
- comandante* comandanteA = new comandante[2];
- vector<bala*> disparar;
- system("cls");
- bool continuar = true;
- while (continuar) {
- if (kbhit()) {
- char direccion = getch();
- personajeA[0].animar(direccion);
- if (direccion == 32) {
- disparar.push_back(new bala(personajeA->x + 2, personajeA->y - 1));
- }
- }
- for (int e = 0; e < 2; e++) {
- if (comandanteA[e].y >= 0) {
- comandanteA[1].animar(personajeA);
- }
- for (int i = 0; i < disparar.size(); i++, retraso++) {
- if (retraso == 300) {
- disparar[i]->animar(comandanteA);
- retraso = 0;
- }
- if (disparar[i]->x == comandanteA[e].x + 1 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 2 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 3 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 4 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 5 && disparar[i]->y == comandanteA[e].y) {
- delete[e] comandanteA;
- }
- if (disparar[i]->y == 0) {
- delete disparar[i];
- disparar.erase(disparar.begin() + i);
- i--;
- }
- retraso++;
- }
- }
- }
- }
- void main() {
- Console::CursorVisible = false;
- Console::SetWindowSize(120, 50);
- game();
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement