While loop

I have an applet. I’m trying to add restart possibility to my game. I have currently two classes. Window and Snake.
Windows class’s draw:


 public void paint(Graphics g) {
        super.paint(g);

        if (inGame)
        {
            g.drawImage(apple, apple_x, apple_y, this);

            for (int z = 0; z < dots; z++) {
                if (z == 0)
                    g.drawImage(head, x[z], y[z], this);
                else g.drawImage(cube, x[z], y[z], this);
                
                
            }
            
	        g.setColor(Color.YELLOW);
	        g.drawString("Points " + points, 5, 15);
            
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();		
    		
    	}else{
        gameOver(g);
    	}
    	
    }

In the beginning I make int variable counter that equals to 1. Where I put the code that is while (counter < 2){}? When you die, it sets counter to 3 and in KeyEvent when i press R it makes counter 1 again.

Currently, when i start it only grey box shows up.

Also, how can i play this applet on mobile phone?

EDIT: Sry for the spoiler, didnt know what it looks like since my mozilla has been buggin lately, and didnt know about the code thing. Thanks (:

Is your code really a spoiler? Use the code tag, not the spoiler tag please :slight_smile:

Anyway, counter isn’t a very good name for what sounds like a game state variable. I recommend doing something like this:


public static final int GAME_STARTING = 1;
public static final int GAME_PLAYING = 2;
public static final int GAME_OVER = 3;

private int gameState = GAME_STARTING;

Then set that variable whenever you transition from one game state to another.

As for how to get your app working on a mobile, that would involve porting it to Android (for a smartphone) or J2ME (for a feature phone). Either way would involve pretty much a rewrite from the Java2D code. I’d get the game working as a regular applet first, then worry about porting.

I don’t get it…? Example if i put gameState = GAME_PLAYING in the graphics part i showed and put when pressing R gameState = GAME_PLAYING, it doesnt work?

Don’t put all your game logic in paint(). The only thing you should be doing there is, well, painting. I’m not really sure how to explain it without backing up and going over the whole idea of state machines, and I’m not a particularly good teacher when it comes to abstract concepts.

My restart code for Bubo4k (it restarts when you fail a lvl)


boolean inGame;
//main loop
while (true){
   //sign variables to their default value of new level
   inGame = true;
   while(inGame){
      //update logic
      if (loseCondition) inGame = false;
      //render
      //check isAlive()
   }
   //do what you like after game is in lose state. 
   //if you want draw "game over" and wait for keypress you can start new loop waitting for it
}

This post explains the basic gist of a State based game.

Instead of using boolean flags for different states, use the power of OO to your advantage with many classes that extend/implement State.