0

Is there a way to make my 2048 game run faster?

I made a setting called high speed mode so that if moves were made really fast I would just skip the spawning, combining and moving animations, however even with that I am still usually one or two moves behind when I spam a bunch of keys at once.

Right now I have a tilePanel class to handle displaying my tiles, and use a timer(very basic code) with paintComponent(drawing the tiles constantly) to make sure it continually updates the board.

public void doAnimation() {
        System.nanoTime();
        Timer timer = new Timer(2, this);
        timer.start();

    }

The way I have structured my game is that I have an ArrayList of commands(ie pressing the right key adds "right" to the arraylist and eventually the arraylist gets to it and executes) I receive to make sure that it always does a move, spawns before doing the next move in the sequence.

To make sure the board continually updates itself, i made a very basic run thread:

public void run(){

                while(true) {
                    if(canDoNewMove&&commands.size()>0&&parent.spawnsLeft==0) {
                        move(commands.get(0));
                        commands.remove(0);
                        canDoNewMove=false;
                    }
                    if(commands.size()>=2) {
                        board.isBehind=true;
                    }
                    else {
                        board.isBehind=false;
                    }
                    if(totalMovements==0&&moveDone) {
                        moveDone=false;
                        if(parent.changed) {
                            parent.spawn(parent);

                        }
                        canDoNewMove=true;

                        
                    }
                    //moveDone=false;
                    try {
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }};

        Thread sampleThread = new Thread(backGroundRunnable);
        sampleThread.start();
        
    }

Would reducing method calls produce a noticeable enough difference that the actions would finish right after I press the keys or is the problem within the tilePanel class that draws the board?

thanks guys

  • "Would reducing method calls produce a noticeable enough difference" - If they are long/heavy methods then yes. You can test this by finding the time difference between the start and end of a method: https://stackoverflow.com/a/34541535/1270000 – sorifiend Jul 05 '21 at 01:43
  • 1
    I can't see outside the code you posted but it seems your background thread is spinning uselessly a lot; using `wait` instead of `sleep`, and `notify` when there is an update should reduce a lot of unnecessary work. Better yet, instead of doing low-level synchronisation work yourself, use a [`BlockingQueue`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/BlockingQueue.html), this is exactly the scenario it is designed for. Your background thread should be idle if there are no updates to process. – Amadan Jul 05 '21 at 01:43
  • 2
    Also, unless a single call to animation takes 18ms, painting every 2ms is mad. No-one needs more than 50 FPS (20ms per frame). – Amadan Jul 05 '21 at 01:54
  • @Amadan would I still have to use wait and notify to make the BlockingQueue work? from what I can tell with BlockingQueue I would still have to use a runnable with while true in order to make sure it does not only call once, thanks. – babayaga007 Jul 06 '21 at 06:21
  • 1
    `BlockingQueue` does the waiting and notifying for you. Yes, you still need `while`, but `BlockingQueue` will block, and not let your code run while waiting for new data. It's the difference between "Any news yet? Any news yet? Any news yet? Any news yet? Thanks. Any news yet? Any news yet?..." and "Let me know when we have news. Oh? Thanks. Let me know when we have news again. ..." Both of them are in a loop, but only one of them is annoying. – Amadan Jul 06 '21 at 08:04

0 Answers0