i'm trying to make a two player console snake game. One of the snakes is controled by 'wasd' and other 'uhjk'. I have two function to get the keys getKEYS1()
and getKEYS2()
. Only difference is the expected chars.
void Snake::getKEYS1()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
if (dir1 != down) // 1
dir1 = up;
break;
case 'a':
if (dir1 != right) // 3
dir1 = left;
break;
case 's':
if (dir1 != up) // 2
dir1 = down;
break;
case 'd':
if (dir1 != left) // 4
dir1 = right;
break;
case 'r': //to kill
dir1 = kill;
break;
}
}
return;
}
And to move the head i use the Move()
function for both snakes.
void Snake::Move(Directions dir)
{
//getKEYS1();
//getKEYS2();
switch (dir)
{
case Snake::stop:
break;
case Snake::up:
headY--;
break;
case Snake::down:
headY++;
break;
case Snake::left:
headX--;
break;
case Snake::right:
headX++;
break;
case Snake::kill:
exit(0);
break;
default:
break;
}
//adjusting & balancing speed
if (dir == down || dir == up) Sleep(30);
else Sleep(7.5);
//if sneake goes out of borders
if (headX > width - 2) headX = 1;
else if (headX < 1) headX = width - 1;
if (headY > height - 2) headY = 1;
else if (headY < 1) headY = height - 1;
//return;
}
I can print both snakes to screen but when i try to move, they move with a huge delay.
Snake s;
std::vector<Snake> Snakes;
Snakes.push_back(s);
Snakes.push_back(s);
bo.Render(screenBuffer); //render borders to inner buffer
Snakes[0].Render(screenBuffer); //render snake1 to inner buffer
Snakes[1].Render(screenBuffer); //render snake2 to inner buffer
screenBuffer->RenderToScreen(); //render all to screen
screenBuffer->Clear(); //updating the screen
Snakes[0].getKEYS1();
Snakes[0].Move(Snakes[0].dir1);
Snakes[1].getKEYS2();
Snakes[1].Move(Snakes[1].dir2);
I think i call getKEYS2() but program tries to run getKEYS1() too and that's why reaction is slow. Am i wrong? What causes this problem? And what to do to avoid it? If you need more info to understand the problem, please let me know.
I'm new to C++ so i stuck a little here. Thanks in advance.