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
- /**/
- #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 bala {
- short x;
- short y;
- bala(short a = 2, short b = 3) {
- x = a;
- y = b;
- }
- ~bala() {
- Console::SetCursorPosition(x, y);
- cout << " ";
- }
- void animar() {
- Console::SetCursorPosition(x, y);
- cout << " ";
- if (y > 0) {
- y--;
- }
- Console::SetCursorPosition(x, y);
- cout << "*";
- }
- };
- 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--; pasos++; break;
- case ABAJO: y++; pasos++; break;
- case IZQUIERDA: x--; pasos++; break;
- case DERECHA: x++; pasos++; break;
- }
- }
- void restriccion() {
- if (x == -1) {
- x = 0;
- }
- else if (y == -1) {
- y = 0;
- }
- }
- };
- void game() {
- Random random;
- personaje* personajeA = new personaje[1];
- 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 i = 0; i < disparar.size(); i++) {
- disparar[i]->animar();
- if (disparar[i]->y == 0) {
- delete[] disparar[i];
- disparar.erase(disparar.begin());
- i--;
- }
- _sleep(100);
- }
- }
- }
- void main() {
- Console::CursorVisible = false;
- Console::SetWindowSize(120, 50);
- game();
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement