1

Im new to java programming. Im learning to develop a game similar to the one called breakout. Here's how it works

you have a set of bricks to hit using a ball and the paddle

Im caught in a situation here,

Whenever i miss the ball, itl take me to the else loop where im sending "Game over" message. Instead i want the ball back again, and the other left over bricks to hit.

Here's the snippet Java Code:

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

       if (ingame) {
           g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
                       ball.getWidth(), ball.getHeight(), this);
           g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
                       paddle.getWidth(), paddle.getHeight(), this);

           for (int i = 0; i < 30; i++) {
               if (!bricks[i].isDestroyed())
                   g.drawImage(bricks[i].getImage(), bricks[i].getX(),
                               bricks[i].getY(), bricks[i].getWidth(),
                               bricks[i].getHeight(), this);
           }
       } else {

           Font font = new Font("Verdana", Font.BOLD, 18);
           FontMetrics metr = this.getFontMetrics(font);

           g.setColor(Color.BLACK);
           g.setFont(font);
           g.drawString(message,
                        (Commons.WIDTH - metr.stringWidth(message)) / 2,
                        Commons.WIDTH / 2);
       }


       Toolkit.getDefaultToolkit().sync();
       g.dispose();
   }

Thanks in advance :)

user644749
  • 21
  • 5

1 Answers1

1

1) Use paintComponent()

2) Don't destry the Graphics passed as argument

3) The code should be equal to the start code; move the ball inside the playfield and start again.

Tobias
  • 9,170
  • 3
  • 24
  • 30
  • But how do i get those bricks from the previous state? Something to do with the for loop... please help – user644749 Oct 01 '11 at 11:40
  • You haven't deleted them? Why should they vanish? – Tobias Oct 01 '11 at 11:55
  • Here is the requirement! Suppose i have 5 bricks to hit, I take my first shot and hit a brick, it vanishes, there remains other 4 bricks, i miss the ball for my second shot. now i want the ball and the paddle and the remaining 4 bricks, so that i can resume playing – user644749 Oct 01 '11 at 12:57
  • isDestroyed() consists of a boolean value false. Doen't it delete the brick?? – user644749 Oct 01 '11 at 13:00
  • 1
    Some where in the code; ingame is set to false. You have to find this location and reset the balls position. – Tobias Oct 01 '11 at 14:07