Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <time.h>
- using namespace std;
- using namespace System;
- #define IZQUIERDA 'A'
- #define DERECHA 'D'
- #define DISPARAR 32
- #define LIMITEX 20
- #define ALTO 2
- #define ANCHO 3
- #define ALEATORIO(INI, FIN) rand() % (FIN - INI + 1) + INI
- enum Color { NEGRO, BLANCO, AZUL, ROJO, VERDE, AMARILLO };
- void cambiarColorFondo(Color miColor) {
- switch (miColor)
- {
- case NEGRO: Console::BackgroundColor = ConsoleColor::Black; break;
- case BLANCO: Console::BackgroundColor = ConsoleColor::White; break;
- case AZUL: Console::BackgroundColor = ConsoleColor::Blue; break;
- case ROJO: Console::BackgroundColor = ConsoleColor::Red; break;
- case VERDE: Console::BackgroundColor = ConsoleColor::Green; break;
- case AMARILLO: Console::BackgroundColor = ConsoleColor::Yellow; break;
- }
- }
- void cambiarColorCaracter(Color miColor) {
- switch (miColor)
- {
- case NEGRO: Console::ForegroundColor = ConsoleColor::Black; break;
- case BLANCO: Console::ForegroundColor = ConsoleColor::White; break;
- case AZUL: Console::ForegroundColor = ConsoleColor::Blue; break;
- case ROJO: Console::ForegroundColor = ConsoleColor::Red; break;
- case VERDE: Console::ForegroundColor = ConsoleColor::Green; break;
- case AMARILLO: Console::ForegroundColor = ConsoleColor::Yellow; break;
- }
- }
- struct Piedra {
- short x = 0, y = 0;
- bool movimiento = false;
- bool colision(short x1, short y1, short ancho, short largo) {
- if (x1 <= x && x<x1 + ancho && y1 <= y && y<y1 + largo){
- return true;
- }
- return false;
- }
- void posicionar(short _x, short _y) {
- movimiento = true;
- x = _x;
- y = _y;
- }
- void animar() {
- if (movimiento) {
- borrar();
- mover();
- dibujar();
- }
- }
- void borrar() {
- cambiarColorCaracter(NEGRO);
- Console::SetCursorPosition(x, y);
- if (y>= 0)
- cout << " ";
- }
- void mover() {
- if (y >= 1)y--;
- }
- void dibujar() {
- cambiarColorCaracter(AMARILLO);
- Console::SetCursorPosition(x, y);
- cout << "|";
- }
- };
- struct personaje {
- short x;
- short y;
- char nave[2][3]{ { ' ', '^', ' ' },{ '>', 'x', '<' } };
- short cantpiedra;
- Piedra* piedrita;
- personaje(short _x = 0, short _y = 0) {
- x = _x;
- y = _y;
- cantpiedra = 0;
- piedrita = new Piedra[cantpiedra];
- }
- //MOVIMIENTOS DE LA PIEDRA
- void moverPiedritas() {
- for (short i = 0; i < cantpiedra; i++){
- piedrita[i].animar();
- }
- }
- void eliminarPiedrita(short posicion) {
- Piedra* tem = new Piedra[cantpiedra - 1];
- for (short i = 0; i < cantpiedra; i++){
- if (i == posicion){
- for (short j = i + 1; j < cantpiedra; j++){
- tem[j - 1] = piedrita[j];
- }
- break;
- }
- else{
- tem[i] = piedrita[i];
- }
- }
- delete piedrita;
- piedrita = tem;
- cantpiedra--;
- }
- void añadirPiedrita() {
- Piedra* tempo = new Piedra[cantpiedra + 1];
- Piedra nuevo;
- nuevo.posicionar(x + 1, y - 1);
- for (short i = 0; i < cantpiedra; i++){
- tempo[i] = piedrita[i];
- }
- tempo[cantpiedra] = nuevo;
- delete piedrita;
- piedrita = tempo;
- cantpiedra++;
- }
- void interactuar(char direccion) {
- if (direccion == DISPARAR) {
- añadirPiedrita();
- }
- else{
- animar(direccion);
- }
- }
- //MOVIMIENTOS DE LA NAVE
- void animar(char direccion) {
- borrar();
- mover(direccion);
- dibujar();
- }
- void borrar() {
- cambiarColorCaracter(NEGRO);
- for (short f = 0; f < ALTO; ++f) {
- Console::SetCursorPosition(x, y + f);
- for (short c = 0; c < ANCHO; ++c) {
- cout << " ";
- }
- }
- }
- void mover(char direccion) {
- switch (toupper(direccion)) {
- case IZQUIERDA: if (x >= 3) x--; break;
- case DERECHA: if (x <= 57) x++; break;
- }
- }
- void dibujar() {
- cambiarColorCaracter(AZUL);
- for (short i = 0; i < ALTO; i++) {
- Console::SetCursorPosition(x, y + i);
- for (short j = 0; j < ANCHO; j++) {
- cout << nave[i][j];
- }
- }
- cambiarColorCaracter(NEGRO);
- }
- };
- //ESTRUCTURA DE AVISPAS:
- struct avispas {
- float x;
- float y;
- bool vive;
- char pi[3]{ '>', 'I', '<' };
- float dx;
- float dy;
- avispas(short _x = 4, short _y = 4) {
- x = _x;
- y = _y;
- dx = 1;
- dy = 1;
- vive = true;
- }
- void animar() {
- borrar();
- mover();
- dibujar();
- }
- void borrar() {
- cambiarColorCaracter(NEGRO);
- Console::SetCursorPosition(x, y);
- for (int j = 0; j < 3; j++) {
- cout << " ";
- }
- }
- void mover() {
- if (x == 0 || x == 60) dx *= -1;
- if (y == 0 || y == 18) dy *= -1;
- x += dx;
- y += dy;
- }
- void dibujar() {
- cambiarColorCaracter(ROJO);
- Console::SetCursorPosition(x, y);
- for (int j = 0; j < 3; j++) {
- cout << pi[j];
- }
- cambiarColorCaracter(NEGRO);
- }
- };
- struct mariposas {
- float x;
- float y;
- bool vive;
- char pi[3]{'8', 'i', '8' };
- float dx;
- float dy;
- mariposas(short _x = 4, short _y = 4) {
- x = _x;
- y = _y;
- dx = 1;
- dy = 1;
- vive = true;
- }
- void animar() {
- borrar();
- mover();
- dibujar();
- }
- void borrar() {
- cambiarColorCaracter(NEGRO);
- Console::SetCursorPosition(x, y);
- for (int i = 0; i < 3; i++) {
- cout << " ";
- }
- }
- void mover() {
- if (x == 0 || x == 60) dx *= -1;
- if (y == 0 || y == 18) dy *= -1;
- x += dx;
- y += dy;
- }
- void dibujar() {
- cambiarColorCaracter(VERDE);
- Console::SetCursorPosition(x, y);
- for (int j = 0; j < 3; j++) {
- cout << pi[j];
- }
- cambiarColorCaracter(NEGRO);
- }
- };
- // FUNCION JUGAR:
- void jugar() {
- short cantavispas = 5;short cantmariposas = 5;
- personaje p = personaje(35, 25);
- avispas* F = new avispas[cantavispas];
- mariposas *ma = new mariposas[cantmariposas];
- for (int i = 0; i < 5; i++){
- int yAux = ALEATORIO(1, 15);
- int xAux = ALEATORIO(14, 18);
- F[i] = avispas(xAux, yAux);
- }
- for (int i = 0; i < 5; i++) {
- int xAux = ALEATORIO(1, 15) ;
- int yAux = ALEATORIO(14, 18);
- ma[i] = mariposas(xAux, yAux);
- }
- bool continuar = true;
- while (continuar) {
- if (_kbhit()) {
- char direccion = getch();
- p.interactuar(direccion);// imprime bala o mueve la nave
- }
- p.moverPiedritas();
- for (short i = 0; i < p.cantpiedra; i++){
- if (p.piedrita[i].y <= 0){
- p.piedrita[i].borrar();
- p.eliminarPiedrita(i);
- i--;
- }
- }
- //INICIALIZA AVISPA
- for (int i = 0; i < cantavispas; i++) {
- F[i].animar();
- }
- //INICIALIZA MARIPOSA
- for (int i = 0; i < cantmariposas; i++) {
- ma[i].animar();
- }
- //para avispa
- for (short i = 0; i < p.cantpiedra; i++){
- for (short j = 0; j < cantavispas; j++){
- if (p.piedrita[i].colision(F[j].x, F[j].y, 3, 2)){
- p.piedrita[i].borrar();
- p.eliminarPiedrita(i);
- F[j].borrar();
- avispas* tem = new avispas[cantavispas - 1];
- for (short a = 0; a < cantavispas; a++){
- if (a == j){
- for (short j = a + 1; j < cantavispas; j++){
- tem[j - 1] = F[j];
- }
- break;
- }
- else{
- tem[a] = F[a];
- }
- }
- delete[] F;
- F = tem;
- cantavispas--;
- j--;
- i--;
- }
- }
- }
- // para mariposa
- for (short k = 0; k < p.cantpiedra; k++) {
- for (short j = 0; j <cantmariposas; j++) {
- if (p.piedrita[k].colision(ma[j].x, ma[j].y, 3, 1)) {
- p.piedrita[k].borrar();
- p.eliminarPiedrita(k);
- ma[j].borrar();
- mariposas* tem = new mariposas[cantmariposas - 1];
- for (short a = 0; a < cantmariposas; a++) {
- if (a == j) {
- for (short j = a + 1; j < cantmariposas; j++) {
- tem[j - 1] = ma[j];
- }
- break;
- }
- else {
- tem[a] = ma[a];
- }
- }
- delete[] ma;
- ma = tem;
- cantmariposas--;
- j--;
- k--;
- }
- }
- }
- _sleep(60);
- }
- delete[] F;
- }
- int main() {
- Console::CursorVisible = false;
- Console::SetWindowSize(70, 30);
- cout << "Presiona cualquier tecla para empezar" << endl;
- system("pause>0");
- Console::Clear();
- jugar();
- system("pause>0");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement