Sluggish response to key polling

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;
                
        }

Why are you starting new threads in every iteration of the game loop?
And why do you have a separate thread just to call setDirection() instead of just doing that in the game loop?
And you could probably use a sleep() at the end of the game loop to give the system thread some time to handle events.

shmoove

Well, now that I’ve asked all those questions I’ll take a shot at guessing what lead you to write that code. You started out with a good ol’ traditional game loop like this one:


while (running) {
  get input
  do logic
  redraw screen
}

You noticed the sluggish response and started trying out all sorts of stuff and ended up with what you have now, right?

All that was actually missing is a small delay at the end of every iteration (needed so the phone has time to handle user input) and your problem would disappear:


while (running) {
  get input
  do logic
  redraw screen
  try {
    sleep(some amount of time);
  }
  catch (Exception e) {}
}

If I’m way off in my guessing then pay no attention, but I thought it might be useful.

shmoove

Hehe, you hit the nail on the head ;D Its just freaky how similar that sounds to the last couple of hours… Thanks :slight_smile: