C
C
CodeInside2016-01-14 14:19:24
C++ / C#
CodeInside, 2016-01-14 14:19:24

How to implement a switch?

I make the user press the up/down arrows to select "1 player"/"2 players". The selected option should be highlighted with a white background. It seems to be implemented, but it does not work correctly. It's hard to figure out how to set colors in this C. Here's what I got:

#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

void Select1Player();
void Select2Players();

void main()
{
  bool player2 = false;
  Select1Player();
  
  char c = 0;
  while(c != 13)
  {
    c = _getch();
    if((c == 72 || c == 80) && player2 == false)//38 и 40 - стрелки вверх и вниз у таблице ASCII
    {
      player2 = true;
      Select2Players();
    }
    else if(c == 72 || c == 80)
    {
      player2 = false;
      Select1Player();
    }
  }
}

//выводит выбор параметров и выделяет "1 player"
void Select1Player()
{
  system("cls");
  COORD coord = { 50, 10 };
  static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hStdOut, coord);
  SetConsoleTextAttribute(hStdOut,
    BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY | BACKGROUND_RED);
  cout << "1 PLAYER";
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  coord = { 50, 11 };
  SetConsoleCursorPosition(hStdOut, coord);
  cout << "2 PLAYERS";
}

//выводит выбор параметров и выделяет "2 players"
void Select2Players()
{
  system("cls");
   COORD coord = { 50, 10 };
  static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hStdOut, coord);
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  cout << "1 PLAYER";
  SetConsoleTextAttribute(hStdOut,
    BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY | BACKGROUND_RED);
  coord = { 50, 11 };
  SetConsoleCursorPosition(hStdOut, coord);
  cout << "2 PLAYERS";
}

I understand where my error is, but I don't know how to solve it. Who can help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Medvedev, 2016-01-14
@CodeInside

#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

void Select1Player();
void Select2Players();

int main()
{
  bool player2 = false;
  Select1Player();

  char c = 0;
  while(c != 13)
  {
    c = _getch();
    if((c == 72 || c == 80) && player2 == false)//38 и 40 - стрелки вверх и вниз у таблице ASCII
    {
      player2 = true;
      Select2Players();
    }
    else if(c == 72 || c == 80)
    {
      player2 = false;
      Select1Player();
    }
  }
  return 0;
}

//выводит выбор параметров и выделяет "1 player"
void Select1Player()
{
  COORD coord = { 50, 10 };
  static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  system("cls");

  SetConsoleCursorPosition(hStdOut, coord);
  SetConsoleTextAttribute(hStdOut,
    BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY | BACKGROUND_RED);
  cout << "1 PLAYER";
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  coord = { 50, 11 };
  SetConsoleCursorPosition(hStdOut, coord);
  cout << "2 PLAYERS";
}

//выводит выбор параметров и выделяет "2 players"
void Select2Players()
{
   COORD coord = { 50, 10 };
  static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  system("cls");

  SetConsoleCursorPosition(hStdOut, coord);
  SetConsoleTextAttribute(hStdOut,
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
  cout << "1 PLAYER";
  SetConsoleTextAttribute(hStdOut,
    BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY | BACKGROUND_RED);
  coord = { 50, 11 };
  SetConsoleCursorPosition(hStdOut, coord);
  cout << "2 PLAYERS";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question