0

I am developing a Snake game. On all of my devices I have the same speed. Except my Nexus 7. The game just runs slower.

I use System.currenttimemillis() to count the time for the loop (f.E. 100ms) and get the time how long everything takes to have always the same time every Gameloop. The last thing is Thread.sleep() till i have my 100ms and then comes the next loop. If there is not enough time for everything to calculate I would have get a Log.d Message.

On my Nexus 7 i didn't get a Message that it is slow, so it must have the 100ms, but it is much slower than the other devices.

Does anyone have an idea why this could be? Gamefield size is always the same checked that already.

EDIT:// Here is the Code with the GameLoop inside. I think the Thread.sleep is the problem, but i am not sure.

            while (running) {

            if (gameEnd) {running = false; Log.d("doInBackground","set running false");}
            //Spiel Schleife im OnClickListener, OnClick für Pause
            int i = 0;

            if (!gameEnd | !gameCancel) {
                //Spiel läuft
                while (!gameEnd & !gameCancel) {
                    long timeStart = System.currentTimeMillis();
                    /*
                     * LOOP for GamePause
                     */
                    if (gamePause) {
                        PauseTimeStart = System.currentTimeMillis();
                        while (gamePause) {
                            //Spielpause, Dialog einblenden, OnClickListener einrichten mit break
                            if (!gamePauseDialog) {
                                gamePauseDialog = true;
                                gameContinues = true;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (gamePause)
                                        {
                                        mHandler.sendEmptyMessage(0);}
                                    }
                                });
                            }
                        }
                    /*
                     * LOOP for GamePause END
                     *
                     * LOOP GameContinue Start
                     */
                    } else {
                        //Wird das Spiel fortgestzt gibt es eine 2 Sekündige Unterbrechung bevor es weitergeht
                        if (gameContinues) {
                            while (gameContinues) {
                                try {
                                    Thread.sleep(1500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                gameContinues = false;
                                PauseTime = Math.round(PauseTime + System.currentTimeMillis() - PauseTimeStart);
                            }

                       /*
                        * Start of the normal Gameloop without Cancel, Pause and so on
                        */

                        } else { // Start GameLoop

                            //Richtungwunsch auf andere Variable übertragen danach auf False setzen
                            // Dadurch sind die Variablen wieder frei für den nächsten Bewegungswunsch auch wenn Update Game noch nicht
                            // oder gerade durchgeführt wird
                            Boolean nDown = NextMoveDown;
                            Boolean nUp = NextMoveUp;
                            Boolean nLeft = NextMoveLeft;
                            Boolean nRight = NextMoveRight;
                            NextMoveDown = false;
                            NextMoveLeft = false;
                            NextMoveRight = false;
                            NextMoveUp = false;

                            /*
                             * Drawing Gamefield, updating GameStatus,Move Snake and so on
                             */
                            gameEnd = GamefieldView.updateGame(
                                        nLeft,
                                        nRight,
                                        nUp,
                                        nDown,
                                        gameContinues);


                            Points = GamefieldView.getPoints();

                            //Zeitberechnung
                            //Time = Math.round(i / FPS); BUGGY

                            //Durchläufe und Punkte an onProgressUpdate() geben
                            publishProgress(Points);
                            i++;
                            gamePauseDialog = false; //Vorsichtshalber gamePauseDialog false setzen, wenn noch true
                           /*
                            *  Schwierigkeits grad und Thread Sleep
                            */

                            long tickePS = 1000 / FPS;
                            long timeSleep = tickePS -(System.currentTimeMillis() - timeStart);
                            //Log.d("Sleeptime", " " + timeSleep + " : " + timeStart + " : " + System.currentTimeMillis() + " : " + tickePS + " : " + (System.currentTimeMillis()-timeStart));
                            try {
                                if (timeSleep > 0) {
                                    Thread.sleep(timeSleep);
                                }
                                else {
                                    Thread.sleep(10);
                                    Log.e("PERFORMANCE WARNING", "Loop took too long! No.->" + performanceCounter++);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } //Standard Game Loop END
                    }
                } //If !GameEnd | !GameCancel
            } else {
                //Wenn Spiel beendet oder abgebrochen wurde
                Log.d("GameAsyncTask - ", " Gameover");
            }

        }
Marcus
  • 6,697
  • 11
  • 46
  • 89
Nick
  • 135
  • 1
  • 4
  • 13
  • 2
    Or your code could be wrong... – user253751 Mar 04 '15 at 18:22
  • But there is only one thing the loop does. Move the Snake to the next xy point. And this in 100ms. If the time is not enough i get that via log.d and if the time will be too short i get the difference, so every round the thread sleeps 100ms. And on both phones it works like charm and the tablet is much slower (about the half) but in log.d i get no message that it took longer. – Nick Mar 04 '15 at 20:59
  • Well, nobody can tell if your code is wrong without seeing it. – user253751 Mar 04 '15 at 21:01
  • Added the Gameloop as code. I think the Thread.Sleep is the problem.. – Nick Mar 04 '15 at 21:17
  • Maybe your slow devices are just that _slow_ and your Nexus is just that _fast_. How much slower are the other devices? Does it have to be 100ms? Can be 150 or 200? –  Mar 04 '15 at 23:09

0 Answers0