Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string>
- using namespace std;
- using namespace System;
- #define Filas 20
- #define Columnas 50
- #define Arriba 'w'
- #define Abajo 's'
- #define Izquierda 'a'
- #define Derecha 'd'
- struct enemigo {
- short x, y;
- short cont = 0, retraso = 100;
- short dy = 1;
- void asignarPosicion() {
- Random r;
- do {
- x = r.Next(2, Columnas);
- y = r.Next(2, Filas);
- } while (!(x > 1 && x < Columnas &&
- y > 1 && y < Filas));
- }
- void mover() {
- if (cont == retraso) {
- borrar();
- if (y + dy == 1 || y + dy == Filas) dy *= -1;
- y += dy;
- dibujar();
- cont = 0;
- }
- cont++;
- }
- void dibujar() {
- Console::SetCursorPosition(x, y);
- cout << "*";
- }
- void borrar() {
- Console::SetCursorPosition(x, y);
- cout << " ";
- }
- };
- struct protagonista {
- short x=2, y=2;
- void mover(char mov) {
- borrar();
- if (mov == Arriba && y - 1 > 1 ) y--;
- if (mov == Abajo && y + 1 < Filas ) y++;
- if (mov == Izquierda && x - 1 > 1 ) x--;
- if (mov == Derecha && x + 1 < Columnas) x++;
- dibujar();
- }
- void dibujar() {
- Console::SetCursorPosition(x, y);
- cout << char(1);
- }
- void borrar() {
- Console::SetCursorPosition(x, y);
- cout << " ";
- }
- void matar(enemigo* &e) {
- if (this->x == e->x &&
- this->y == e->y) {
- delete e;
- e = NULL;
- }
- }
- };
- void pintarMapa() {
- for (short columna = 1; columna <= Columnas; columna++) {
- for (short fila = 1; fila <= Filas; fila++) {
- if (fila == 1 || fila == Filas ||
- columna == 1 || columna == Columnas) {
- Console::SetCursorPosition(columna, fila);
- cout << char(219);
- }
- }
- }
- }
- void jugar() {
- Console::CursorVisible = false;
- pintarMapa();
- protagonista prot;
- enemigo *e1, *e2;
- e1 = new enemigo;
- e2 = new enemigo;
- e1->asignarPosicion(); _sleep(500);
- e2->asignarPosicion();
- prot.dibujar();
- while (true) {
- if (kbhit())
- prot.mover(getch());
- if (e1) {
- e1->mover();
- prot.matar(e1);
- }
- if (e2) {
- e2->mover();
- prot.matar(e2);
- }
- }
- }
- int main() {
- jugar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement