defining keys in pong game

0 jo vembeer · October 30, 2015
Hi everyone!
 I made a console pong game in c++. but I want to add a new stick in my project and make it possible to play multiplayer. My question  is " How can I define keys like 'w', 'a', 's', 'd' to control the second stick?"
Here is my codes.

Post a Reply

Replies

Oldest  Newest  Rating
0 jo vembeer · October 30, 2015
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>

using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

void gotoxy(int x, int y)//start gotoxy
{
static HANDLE h = NULL;
if (!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h, c);
} //end function gotoxy






int main() {//start main



srand(time(0));

bool controlx = 0, controly = 0, controlTop, controlRaket;

int a = 0; // for getch ( raket)

int g = 0; // game over control

int score = 0; //score control

int kalan = 3;

int  topx, topy, raket_1x, raket_2x, raket_1y, raket_2y, raket_3x, raket_3y, start;

raket_1x = 45;

raket_2x = 45;

raket_3x = 45;

raket_1y = 11;

raket_2y = 12;

raket_3y = 13;

topx = 1;

topy = rand()%30;



cout << "Yukari ve asagi ok tuslarini kullan \n\n Baslamak icin bir tusa bas ";

start = _getch(); // ba?latma tu?unu al?r 


while (1) {//start while

system("cls");

 for(int x =1 ; x<30; x++)
 {
  gotoxy(0,x);
  cout<< "|";
  gotoxy(55,x);
  cout<< "|";
 
 }
 for(int x =1 ; x<55; x++)
 {
  gotoxy(x,0);
  cout<< "*";
  gotoxy(x,30);
  cout<< "*";
 
 }



 
if (_kbhit()) {//start kbhit if

switch ((a = _getch())) {//start switch

case KEY_UP:
if (raket_1y == 0)
break;
raket_1y--;
raket_2y--;
raket_3y--;
raket_1y--;
raket_2y--;
raket_3y--;
break;

case KEY_DOWN:
if (raket_3y == 30)
break;
raket_1y++;
raket_2y++;
raket_3y++;
raket_1y++;
raket_2y++;
raket_3y++;
break;
/*  */     

case KEY_RIGHT:
if (raket_1x == 54)
break;
raket_1x++;
raket_2x++;
raket_3x++;
raket_1x++;
raket_2x++;
raket_3x++;
break;

case KEY_LEFT:
if (raket_1x == 0)
break;
raket_1x--;
raket_2x--;
raket_3x--;
raket_1x--;
raket_2x--;
raket_3x--;
break;        


default:

break;
}//end switch

}//end kbhit if

gotoxy(raket_1x, raket_1y);
cout << "I";
gotoxy(raket_2x, raket_2y);
cout << "I";
gotoxy(raket_3x, raket_3y);
cout << "I";
gotoxy(topx, topy);
cout << "O";

if (topx == 55 && g == 3) {//start game over control

system("cls");
cout << "Game Over \n";

system("Pause");
exit(0);

}//end game over control
if ( (((raket_1x == topx == 1) && (raket_1y == topy == 1)) || ((raket_2x == topx == 1) && (raket_2y == topy == 1)) || ((raket_3x == topx == 1) && (raket_3y == topy == 1))) && controlRaket == 0) {//start raket sekme control
controlx = 1;
controly = 1;
score++;

}

if (( (((raket_1x == topx == 1) && (raket_1y == topy == 1)) || ((raket_2x == topx == 1) && (raket_2y == topy == 1)) || ((raket_3x == topx == 1) && (raket_3y == topy == 1))) && controlTop == 0) && controlRaket == 0) {
controlx = 1;
controly = 0;
score++;

}

if((((raket_1x == topx == 1) && (raket_1y == topy == 1)) || ((raket_2x == topx == 1) && (raket_2y == topy == 1)) || ((raket_3x == topx == 1) && (raket_3y == topy == 1))) && controlRaket == 1) {//start raket sekme control
controlx = 0;
controly = 1;
score++;

}



if (((((raket_1x == topx == 1) && (raket_1y == topy == 1)) || ((raket_2x == topx == 1) && (raket_2y == topy == 1)) || ((raket_3x == topx == 1) && (raket_3y == topy == 1))) && controlTop == 0) && controlRaket == 1) {
controlx = 0;
controly = 0;
score++;

}


//start topx control
if (topx == 1){
controlx = 0;
controlRaket = 0;//raket arkas? sekme

}

if (topx == 55) {
controlRaket = 1;//raket arkas? sekme
controlx = 1;
g++;
kalan--;

}


if (controlx == 0 )
topx++;
if (controlx == 1 )
topx--;

//end topx control

gotoxy(55,33 );
cout << "Skorun  :  " << score ;
gotoxy(55, 36);
cout << "Kalan Hakkin : " << kalan;

//start topy control 
if (topy == 1)


controly = 0;
   controlTop = 0;

        }

if (topy == 30)
{

controly = 1;
   controlTop = 1; // topun raketten a?a?? ve ya yukar? sekmesini kontrol için

        }

if (controly == 0 && controlTop == 0)


topy++;

}

if (controly == 1)
{
topy--;

}
//end topy control 

Sleep(55);//game speed

}//end while


system("Pause");

return 0;


}//end main
  • 1

C++

107,272 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator