[ Foro de C++ ]

Problema con HILOS

12-Sep-2019 21:45
Invitado (Mauricio :v)
0 Respuestas

#include "pch.h"
#include "iostream"
#include "thread"
#include "conio.h"
#include "windows.h"

using namespace std;
using namespace System;

void gotoxy(int x, int y) {
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}

class NAVE
{
private:
int x,  y, dirH;
bool personaje;
int m_sec;

short Jug[4][3] =
{
{0,1,0},
{1,0,1},
{0,1,0},
{1,0,1}
};

short Enem[4][3] =
{
{1,0,1},
{0,1,0},
{1,0,1},
{0,1,0}
};
public:
NAVE(int pX, int pY, int pDir, bool _personaje, int _veloc)
{
x = pX;
y = pY;
dirH = pDir;
personaje = _personaje; //Si personaje es true entonces seleccionas jugador sino enemigo
m_sec = _veloc;
}
~NAVE(){}

void Pintar()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (personaje)
{
gotoxy(j + x, i + y);
Console::ForegroundColor = ConsoleColor::DarkBlue;
cout << (char)(Jug[i][j] == 1 ? 219 : 32);
}
else
{
gotoxy(j + x, i + y);
Console::ForegroundColor = ConsoleColor::DarkRed;
cout << (char)(Enem[i][j] == 1 ? 219 : 32);
}
}
}
}

void Borrar()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
gotoxy(j + x, i + y);
Console::ForegroundColor = ConsoleColor::Black;
cout << (char)(219);
}
}
}

void Mover()
{
Borrar();
if (dirH == 1)
{
x++;
if (x > 75)
{
x = 74;
dirH = -1;
}
}
if (dirH == -1)
{
x--;
if (x < 1)
{
x = 2;
dirH = 1;
}
}
Pintar();
}

void MoverNave()
{
while (true)
{
Mover();
Sleep(m_sec);
}
}
};

void Tarea1()
{
NAVE jug(40, 30, 1, true, 20);
jug.MoverNave();
}

void Tarea2()
{
NAVE enem(40, 1, 1, false, 80);
enem.MoverNave();
}

int main()
{
Console::CursorVisible = false;
//ShowCursor(0);
Console::SetWindowSize(80, 30);

thread t1(Tarea1);
//thread t2(Tarea2);

t1.join();
//t2.join();

return 0;
}

Al ejecutar las dos tareas se hacen por separado, sin embargo una se interpone con la otra




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)