Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  While loop  (Read 622 times)
0 Members and 1 Guest are viewing this topic.
Offline Kivihirvio

Junior Newbie





« Posted 2012-01-06 16:55:25 »

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:

  
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
 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 (:
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-01-06 17:04:36 »

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

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

1  
2  
3  
4  
5  
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.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2012-01-06 17:11:02 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kivihirvio

Junior Newbie





« Reply #3 - Posted 2012-01-06 17:25:51 »


1  
2  
3  
4  
5  
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.
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?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-01-06 17:30:01 »

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.
Offline ReBirth
« Reply #5 - Posted 2012-01-06 22:57:29 »

My restart code for Bubo4k (it restarts when you fail a lvl)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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
}

Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-01-06 23:00:36 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!