Isolate keypress waiting in a method

This commit is contained in:
manzerbredes 2015-05-01 12:56:38 +02:00
parent 03b57472fc
commit 672358a947
2 changed files with 65 additions and 54 deletions

View file

@ -14,7 +14,7 @@ ConsoleController::~ConsoleController()
void ConsoleController::play()
{
//Intruction msg
std::cout << "Use arrows to play !" << std::endl;
//Init keyPress
@ -26,67 +26,26 @@ void ConsoleController::play()
//Start game
while (!m_game->isOver())
{
//Wait for keypress
while(1){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
keyPress=LEFT;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
keyPress=RIGHT;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
keyPress=UP;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
// la touche "flèche gauche" est enfoncée : on bouge le personnage
keyPress=DOWN;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
//Wait for release
}
break;
}
}
//Get key press
keyPress=this->waitArrowKeyPress();
//New line for the console print arrow press
std::cout << std::endl;
//Check for keypress
switch (keyPress)
{
//Print keyPress
switch(keyPress){
case UP:
std::cout << "up" << std::endl;
std::cout << "Keypress : Up" << std::endl;
break;
case DOWN:
std::cout << "down" << std::endl;
std::cout << "Keypress : Down" << std::endl;
break;
case LEFT:
std::cout << "left" << std::endl;
std::cout << "Keypress : Left" << std::endl;
break;
case RIGHT:
std::cout << "right" << std::endl;
break;
default:
std::cout << "Keypress : Right" << std::endl;
break;
}
@ -94,8 +53,60 @@ void ConsoleController::play()
m_game->showGrid();
std::cout << std::endl;
//Pop new number
m_game->pop();
}
}
Direction ConsoleController::waitArrowKeyPress()
{
//Initialise keyPress
Direction keyPress;
//Wait for keypress
while(1){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
keyPress=LEFT;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
keyPress=RIGHT;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
keyPress=UP;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
//Wait for release
}
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
// la touche "flèche gauche" est enfoncée : on bouge le personnage
keyPress=DOWN;
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
//Wait for release
}
break;
}
}
return keyPress;
}

View file

@ -15,7 +15,7 @@ class ConsoleController
private:
Game * m_game;
Direction waitArrowKeyPress();
public:
ConsoleController();
~ConsoleController();