Hi all,
I’m making a 2 player network pong game but the response to key polling is slow. I have tried a few options but have found the most promising to be the following code. Could someone plese geive me pointers as to how to speed up the responsiveness?
while (Pong.isRunning()) {
lm.append(lSide);
lm.append(rSide);
if(hasBall)
lm.append(ball);
lm.append(bar);
if(!Pong.isSinglePlayer())
new Thread() {
public void run() {
message = Pong.readMessage();
}
}.start();
new Thread() {
public void run() {
direction = getDirection();
}
}.start();
if(direction!=NO_MOVE)
bar.move(direction, 0);
if (ball.collidesWith(lSide, true)
|| ball.collidesWith(rSide, true)) {
moveX = moveX * -1;
boinc.play();
} else if (Pong.isSinglePlayer()
&& ball.collidesWith(tSide, true)) {
moveY = 1;
boinc.play();
} else if (ball.collidesWith(bar, true)) {
Point [] refPts = getRefPts(ball);
if(inSprite(bar,refPts[0]))
moveX = -1;
else
if(inSprite(bar,refPts[2]))
{
moveY=-1;
}
else
if(inSprite(bar,refPts[4]))
{
moveX=1;
} else
if(inSprite(bar,refPts[1]) && !(inSprite(bar,refPts[2]) || inSprite(bar,refPts[3])))
{
moveX= 1;
moveY=-1;
}
else
if(inSprite(bar,refPts[3]) && !(inSprite(bar,refPts[1]) || inSprite(bar,refPts[2])))
{
moveY=-1;
moveX=-1;
}
boinc.play();
} else if (!Pong.isSinglePlayer()
&& ball.collidesWith(tSide, true)) {
hasBall = false;
message = ball.getX() + "," + moveX + ","
+ moveY;
Pong.sendMessage(message);
} else if (ball.collidesWith(bSide, true)) {
die.play();
return;
}
if (hasBall) {
ball.move(moveX, moveY);
ball.paint(g);
}
/*new Thread() {
public void run() {
lm.paint(g, 0, 0);
}
}.start();
*/
if (!hasBall && message != null)
{
int[] intAr = parseMessage(message);
if(intAr!=null){
ball.setPosition(intAr[0], 0);
moveX = intAr[1];
moveY = intAr[2] * -1;
hasBall = true;
}
}
g.setColor(234, 224, 190);
g.fillRect(0, 0, width, height);
lm.paint(g, 0, 0);
flushGraphics();
}
}
private int getDirection()
{
int keys;
keys = getKeyStates();
if ((keys & LEFT_PRESSED) != 0)
{
if (!bar.collidesWith(lSide, false))
return LEFT;
} else if ((keys & RIGHT_PRESSED) != 0) {
if (!bar.collidesWith(rSide, false))
return RIGHT;
}
return NO_MOVE;
}