Xavistian

Ejercicio última clase

Jun 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. // ConsoleApplication8.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <conio.h>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace System;
  8.  
  9. struct Enemigos
  10. {
  11.     int x;
  12.     int y;
  13.     int dex;
  14. };
  15.  
  16. void DibujaMatriz(int matriz[20][20])
  17. {
  18.     Console::BackgroundColor = ConsoleColor::White;
  19.     for (int i = 0; i < 20; i++)
  20.     {
  21.         for (int j = 0; j < 20; j++)
  22.         {
  23.             if (matriz[i][j] == 1)
  24.             {
  25.                 Console::ForegroundColor=ConsoleColor::Black;
  26.                 cout << (char)219;
  27.             }
  28.             else cout << ' ';
  29.         }cout << endl;
  30.     }
  31. }
  32. void MoverEnemigoyMovil(int matriz[20][20])
  33. {
  34.     Enemigos *enemigo = new Enemigos[3];
  35.     enemigo[0].x = 3, enemigo[1].x = 3, enemigo[2].x = 3, enemigo[0].y = 5, enemigo[1].y = 10, enemigo[2].y = 15, enemigo[0].dex = 1, enemigo[1].dex = -1, enemigo[2].dex = 1;
  36.     int px = 10, py = 18;
  37.     int key;
  38.     bool out = 1;
  39.     Console::SetCursorPosition(px, py);
  40.     cout << 'J';
  41.     Console::SetCursorPosition(enemigo[0].x, enemigo[0].y);
  42.     cout << '*';
  43.     Console::SetCursorPosition(enemigo[1].x, enemigo[1].y);
  44.     cout << '*';
  45.     Console::SetCursorPosition(enemigo[2].x, enemigo[2].y);
  46.     cout << '*';
  47.     while (out == 1)
  48.     {
  49.         if (kbhit())
  50.         {
  51.             key = _getch();
  52.             if (key == 224)
  53.             {
  54.                 Console::SetCursorPosition(px, py);
  55.                 cout << ' ';
  56.                 key = _getch();
  57.                 switch (key)
  58.                 {
  59.                 case 72:if (matriz[py - 1][px] == 0 || matriz[py - 1][px] == 2)py--;break;
  60.                 case 75:if (matriz[py][px - 1] == 0 || matriz[py][px - 1] == 2)px--;break;
  61.                 case 77:if (matriz[py][px + 1] == 0 || matriz[py][px + 1] == 2)px++;break;
  62.                 case 80:if (matriz[py + 1][px] == 0 || matriz[py + 1][px] == 2)py++;break;
  63.                 }
  64.             }Console::SetCursorPosition(px, py); cout << 'J';
  65.         }
  66.         _sleep(50);
  67.         Console::SetCursorPosition(enemigo[0].x, enemigo[0].y);
  68.         cout << ' ';
  69.         Console::SetCursorPosition(enemigo[1].x, enemigo[1].y);
  70.         cout << ' ';
  71.         Console::SetCursorPosition(enemigo[2].x, enemigo[2].y);
  72.         cout << ' ';
  73.         if (enemigo[0].x == 1 || enemigo[0].x == 18) enemigo[0].dex *= -1;
  74.         enemigo[0].x += enemigo[0].dex;
  75.         if (enemigo[1].x == 1 || enemigo[1].x == 18) enemigo[1].dex *= -1;
  76.         enemigo[1].x += enemigo[1].dex;
  77.         if (enemigo[2].x == 1 || enemigo[2].x == 18) enemigo[2].dex *= -1;
  78.         enemigo[2].x += enemigo[2].dex;
  79.         Console::SetCursorPosition(enemigo[0].x, enemigo[0].y);
  80.         cout << '*';
  81.         Console::SetCursorPosition(enemigo[1].x, enemigo[1].y);
  82.         cout << '*';
  83.         Console::SetCursorPosition(enemigo[2].x, enemigo[2].y);
  84.         cout << '*';
  85.         if (enemigo[0].x == px&&enemigo[0].y == py || enemigo[1].x == px&&enemigo[1].y == py || enemigo[2].x == px&&enemigo[2].y == py)
  86.         {
  87.             px = 10;
  88.             py = 18;
  89.             Console::SetCursorPosition(px, py);
  90.             cout << 'J';
  91.         }
  92.         if (matriz[py][px] == 2) out = 0;
  93.     }
  94.         Console::SetCursorPosition(2, 10);
  95.         cout << "A WINNER IS YOU!";
  96.  
  97. }
  98. int main()
  99. {
  100.     int matriz[20][20] = {
  101.         {1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1},
  102.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  103.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  104.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  105.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  106.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  107.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  108.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  109.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  110.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  111.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  112.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  113.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  114.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  115.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  116.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  117.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  118.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  119.         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  120.         {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  121.     };
  122.     DibujaMatriz(matriz);
  123.     MoverEnemigoyMovil(matriz);
  124.     _getch();
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment