🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

how to : 2 player - Pong game

Started by
4 comments, last by whereiswez 22 years, 11 months ago
how do i make it so that 2 players can simultaneously hold down keys, and all the actions will be registered .. im using borland command line and no asm stuff .. and trying to keep the includes to a minimum ..
Advertisement
You probably want to have an array of booleans that symbolize the characters you want to check for or you could just have an array for all characters but for pong that would be a waste, and then in your keydown method you just mark those as true. On keyup you can mark them as false.

Good luck,
-lao
2 simultaneous keydown, right ?
just use

if (KEY_DOWN(VK_DOWN)) { do player 1 movement}
if (KEY_DOWN(VK_......) { do player 2 movement}

it''s automatically detect 2 keydown
quote: Original post by Cobramania
2 simultaneous keydown, right ?
just use

if (KEY_DOWN(VK_DOWN)) { do player 1 movement}
if (KEY_DOWN(VK_......) { do player 2 movement}

it''s automatically detect 2 keydown



Howdee .. I''ve seen both of these reply posts mentioned in previous topics and posts, but I''m not sure if they apply to me .. yet.

I''m busy relearning to program, and hopefully go into complexed game programming.

The PONG game I''m working on is in ASCII ehehe
and the max #include''s i''m using so far are time, stdlib, conio, iostream..

I''ll try looking up these things anyway though .. I have been trying .. also the other thing is that I''m using ''switch'' which I suppose I should be changing to ''ifs'' then .. hmm ..

Thanx .. though



Yo dude!

Don''t know if it will help, but:

while(!kbhit())
{
}

makes a constant loop until the user press a key. This key can be retrieved with getch() after kbhit() returns nonzero.

later!

oro...?
SKeTch
K .. I do actually need help with this .. I am going to put the code here, so that you can see what I''m trying to do ..

Please help me ?

I don''t expect somebody to rewrite the whole program for me .. I just need the soltuion for multiple keys in and the appropraite response.

>

/*
to do :

two players .. need simultaneous keypress function
computer ai
if ball goes to extent, then set score, regame !
prod menu system
scoring system ? ..

ball speed / game speed?

add formulas for curve balls !

random flying things to collect, exp pad, speed ball, slow ball, bigger ball ?
*/

#include
#include
#include
#include

struct myballpos
{
int x;
int y;
} ballpos;

struct mypaddle
{
int x;
int y;
int ex;
} lpad;

struct mypaddle rpad;

void wait (int msec)
{
clock_t endwait;
endwait = clock () + (msec * CLK_TCK/1000);
while (clock() < endwait) {}
};

void drawball(int x, int y, int maskoff)
{
gotoxy(x,y);
switch (maskoff)
{
case 0 :
cout << " ";
break;
default :
cout << "*";
};
};

void drawpad(int x, int y, int ex, int maskoff)
{
for (int i=y-ex;i<=y+ex;i++)
{
gotoxy(x,i);
switch (maskoff)
{
case 0 :
cout << " ";
break;
default :
cout << "H";
};
};
};

int main()
{
srand(time(NULL));

char ch;

int gamespeed = 50;
int ai = 1;

int strchmax = 5;
int strch = rand() % strchmax;
int strchc = strch;
int endgame = 0;
int l2r = rand() % 1; // from left to right
int b2t = rand() % 1; // from bottom to top

/*
init ball , l pad, r pad positions
*/
ballpos.x = 40;
ballpos.y = 12;

lpad.x = 15;
lpad.y = 12;
lpad.ex = 3;

rpad.x = 65;
rpad.y = 12;
rpad.ex = 3;

clrscr();

/*
begin the game loop ! :D
*/

while (endgame==0)
{
/*
if AI enabled then GAME ON WOOHOO ! :D
*/
if (ai == 1)
{
drawpad(rpad.x, rpad.y, rpad.ex, 0);
if ((ballpos.y > rpad.ex) && (ballpos.y <= 24-rpad.ex))
rpad.y = ballpos.y;
};

drawpad(lpad.x, lpad.y, lpad.ex, 1);
drawpad(rpad.x, rpad.y, rpad.ex, 1);

/*
check which keys are pressed, affect the assigned objects movement
check extents of pad movement
*/
if (kbhit())
{
ch = getch();
switch (ch)
{
case ''`'' :
{
endgame = 1;
};
break;
case ''+'' :
{
if (gamespeed > 0)
gamespeed = gamespeed -1;
};
break;
case ''-'' :
{
if (gamespeed < 50)
gamespeed = gamespeed +1;
};
break;
case ''s'' :
{
if (lpad.y-lpad.ex > 1)
{
drawpad(lpad.x, lpad.y, lpad.ex, 0);
lpad.y = lpad.y-1;
drawpad(lpad.x, lpad.y, lpad.ex, 1);
};
};
break;
case ''x'' :
{
if (lpad.y+lpad.ex < 24)
{
drawpad(lpad.x, lpad.y, lpad.ex, 0);
lpad.y = lpad.y+1;
drawpad(lpad.x, lpad.y, lpad.ex, 1);
};
};
break;
case ''5'' :
{
if (rpad.y-rpad.ex > 1)
{
drawpad(rpad.x, rpad.y, rpad.ex, 0);
rpad.y = rpad.y-1;
drawpad(rpad.x, rpad.y, rpad.ex, 1);
};
};
break;
case ''2'' :
{
if (rpad.y+rpad.ex < 24)
{
drawpad(rpad.x, rpad.y, rpad.ex, 0);
rpad.y = rpad.y+1;
drawpad(rpad.x, rpad.y, rpad.ex, 1);
};
};
break;
};
};

drawball(ballpos.x,ballpos.y,0);

/*
check the stretch of the ball distance to follow
check the distance the ball has followed to the stretch
check the direction of the ball from top to bottom or reversed
reverse the direction
*/
if (strchc > 0)
{
strchc = strchc -1;
}
else
if ((strchc == 0) && (strch != 0))
{
strchc = strch;
if (ballpos.y < 2)
{
b2t = 0;
}
else
if (ballpos.y > 23)
{
b2t = 1;
};

switch (b2t)
{
case 0 :
ballpos.y = ballpos.y +1;
break;
case 1 :
ballpos.y = ballpos.y -1;
break;
};
};

/*
check if the ball reaches boundary limits
change direction from left to right
reverse the direction
*/
if (ballpos.x > 79)
{
l2r = 0;
}
else
if (ballpos.x < 2)
{
l2r = 1;
}

/*
check if ball hits LEFT paddle ! :D
*/
if ((ballpos.x == lpad.x) && (ballpos.y > lpad.y-lpad.ex-1) && (ballpos.y < lpad.y+lpad.ex+1))
{
l2r = 1;
// if hit TOP
if ( ballpos.y < (lpad.y-lpad.ex) + (((lpad.ex*2)+1)/3) )
{
// if going DOWN
if (b2t == 0)
{
if ((strch <= strchmax) && (strch > 1))
strch = strch -1;
else
if (strch < 1)
strch = strchmax;
}
else
// if going UP
if (b2t == 1)
{
if ((strch < strchmax) && (strch >= 1))
{
strch = strch +1;
b2t = 0;
}
else
if ((strch == strchmax) || (strch == 0))
{
if (strch == 0)
strch = strchmax;
b2t = 0;
};
};
}
else
// if hit BOTTOM
if ( ballpos.y > (lpad.y+lpad.ex) - (((lpad.ex*2)+1)/3) )
{
// if going DOWN
if (b2t == 0)
{
if ((strch < strchmax) && (strch >= 1))
{
strch = strch +1;
b2t = 1;
}
else
if ((strch == strchmax) || (strch == 0))
{
if (strch == 0)
strch = strchmax;
b2t = 1;
};
}
else
// if going UP
if (b2t == 1)
{
if ((strch < strchmax) && (strch > 1))
strch = strch -1;
else
if (strch < 1)
strch = strchmax;
}
};
};

/*
check if ball hits RIGHT paddle ! :D
*/
if ((ballpos.x == rpad.x) && (ballpos.y > rpad.y-rpad.ex-1) && (ballpos.y < rpad.y+rpad.ex+1))
{
l2r = 0;
// if hit TOP
if ( ballpos.y < (rpad.y-rpad.ex) + (((rpad.ex*2)+1)/3) )
{
// if going DOWN
if (b2t == 0)
{
if ((strch <= strchmax) && (strch > 1))
strch = strch -1;
else
if (strch < 1)
strch = strchmax;
}
else
// if going UP
if (b2t == 1)
{
if ((strch < strchmax) && (strch >= 1))
{
strch = strch +1;
b2t = 0;
}
else
if ((strch == strchmax) || (strch == 0))
{
if (strch == 0)
strch = strchmax;
b2t = 0;
};
};
}
else
// if hit BOTTOM
if ( ballpos.y > (rpad.y+rpad.ex) - (((rpad.ex*2)+1)/3) )
{
// if going DOWN
if (b2t == 0)
{
if ((strch < strchmax) && (strch >= 1))
{
strch = strch +1;
b2t = 1;
}
else
if ((strch == strchmax) || (strch == 0))
{
if (strch == 0)
strch = strchmax;
b2t = 1;
};
}
else
// if going UP
if (b2t == 1)
{
if ((strch < strchmax) && (strch > 1))
strch = strch -1;
else
if (strch < 1)
strch = strchmax;
}
};
};

/*
increase the ball position from left to right or reverse
*/
switch (l2r)
{
case 0 :
ballpos.x = ballpos.x -1;
break;
case 1 :
ballpos.x = ballpos.x +1;
break;
};

/*
statistics check
*/
gotoxy(1,25);
cout << "b.x:" << ballpos.x << " ";
cout << "b.y:" << ballpos.y << " ";
cout << "sc:" << strchc << " ";
cout << "s:" << strch << " ";
cout << "l.x:" << lpad.x << " ";
cout << "l.y:" << lpad.y << " ";
cout << "l.ex:" << lpad.ex << " ";
cout << "r.x:" << rpad.x << " ";
cout << "r.y:" << rpad.y << " ";
cout << "r.ex:" << rpad.ex << " ";
cout << "gs:" << gamespeed << " ";

wait(0);

drawball(ballpos.x,ballpos.y,1);
wait(gamespeed);
};

return 0;
}

This topic is closed to new replies.

Advertisement