-5

I have a problem with my java game. I’m beginner, but i have to write it as a school project.
Game is called „Birthday Cake” there is 7 candles on the cake and randomly one of it is showing for let say 30s and during this time u have to click on it to get point, if u don’t click on it during this time next candle will show. Game end when 10 candle shows.
I made for loop and i tried to make it work for sooo long that I’m dying from frustration
my for loop works but it is so fast that i use Thread.sleep(1000), i tried lots of solutions it looks ok. BUT when i start my game nothing is happening and after few seconds all 7 candles shows and quickly disappear. I think I’m doing something wrong, but i have no idea what.

   if(Dane.start){

        int liczbaLosowa = 0;

        for(int i=0; i<10 ;i++){
            liczbaLosowa = (int)(Math.random()*7);

            this.wspX= wspX_p[liczbaLosowa];
            this.wspY= wspY_p[liczbaLosowa];
            g2d.drawImage(plomienImg, wspX, wspY,null);
            Toolkit.getDefaultToolkit().sync();
            try {
                Thread.sleep(1000);     
            } catch (Exception ex) { }
            //repaint();
        }
        Dane.start=false;

        }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Miszel
  • 1
  • 2
  • Is this Swing? Is it being called on the Swing event thread? The code looks to 1) have the potiential to put your entire GUI to sleep, and 2) throw NullPointerExceptions due to misuse of a Graphics object. If Swing, there are ways to fix this including use of a Swing Timer, using the Graphics object inside of a JPanel's paintComponent method, or better still, swapping a JLabel's ImageIcons. Please give us more information and help us understand your problem and code better. – Hovercraft Full Of Eels Dec 25 '15 at 21:52
  • yes it is on Swing, this loop is inside JPanel paintComponent, i load images using BufferedImage – Miszel Dec 25 '15 at 22:07
  • There's so much on Google about the game loop from simple to complex ones. – Steven Dec 25 '15 at 22:12

2 Answers2

6

this loop is inside JPanel paintComponent...

Never,
Never,

NEVER

call Thread.sleep(...) inside of paintComponent ever. Please understand that this method largely determines the perceived responsiveness of your program and anything that slows it down or freezes it will severely slow down and freeze your GUI. In fact you should never call Thread.sleep inside the code of most Swing programs (all that runs on the Swing event thread) but doing so in paintComponent is an even worse sin. The solution is to use a Swing Timer, and put code that you want to be called repeatedly at regular intervals inside of the Timer's ActionListener's actionPerformed code. Within this method, change the value held by fields within your class, for instance wspX and wspY, call repaint(), and then use those fields inside of paintComponent to determine what gets painted where.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Thread.sleep() is a bad call which can lead into many problems. i was told to never use it. instead i will show you the way i do my game loops. it might not be the perfect game loop but it is good.

i recommand implemets runnable and putting your loop in your run method.

 public void run(){

    init(); //initialisation of images, sound..etc. will be executed once only

    int fps = 60 //number of update per second.
    double tickPerSecond = 1000000000/fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();

    while(running){

        now = System.nanoTime();
        delta += (now - lastTime)/tickPerSecond;
        lastTime = now;

        if(delta >= 1){

            tick();
            render();
            delta--;
        }
    }
}

private void init(){

  //initialisation image, sound, loading world, generate maps....etc
}

private void tick(){

  //tick player, world, entities..etc
}

private void render(){

  //render graphics.  
}

also dont forget to create start and stop method for the thread. you can change the fps to what number you would like, no need to go higher than 60.

HeRoXLeGenD1
  • 27
  • 1
  • 4