1
\$\begingroup\$

I'm not new to programming, but I am relatively new to game programming. I am trying to create a version of Breakout, as a good basic game to get myself introduced to 2d game programming.

I currently have a class structure as the following:

Class Structure

The BreakoutRunner class is the basic initialization stuff for a JFrame. It holds a BreakoutView, which is a JPanel that contains the update logic, and the renderer.

First off, is this a correct structure?

Secondly, I am having problems getting this to work correctly. In BreakoutView, I have the following constructor:

public BreakoutView()
{
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBackground(Color.black);

    state = GameState.PLAYING;

    gameBlocks = new Block[NUM_ROWS][BLOCK_ROWS];        
    gameBall = new Ball(WIDTH / 2, HEIGHT /2);
    player = new Player(WIDTH / 2);

    initializeGrid();

    //start();
}

I have the method start() which is commented because it is causing the problems. With the line commented, the game looks like so:

Game Image

But the problem arises when I take away the comment, and add the start() method.

The start method:

private void start()
{
    while(state == GameState.PLAYING)
    {
        update();
        repaint();

        try
        {
            Thread.sleep(50);
        }
        catch(InterruptedException e)
        {
            assert(e == null);
        }          
    }
}

Which includes calls to update:

private void update()
{    
    checkBlocks();
    moveBall();
}

and repaint, which I believe calls the paintComponent method:

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

    drawWalls(g);
    drawBlocks(g);
    drawPlayer(g);
    drawBall(g);
}

When the code is run with start() uncommented, it gets thrown into an infinite loop, while the frame and the panel get turned into something that looks like this:

enter image description here

What is going on here? Any idea why I'm just sent into an infinite loop?

Thanks Everyone!

DISCLAIMER: I know the update loop is not completely correct. I am more worried about getting it to work period, rather than at an even frame rate... (IE. sleep == bad).

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You're not supposed to call your start() method inside the class's constructor, is your problem.

You need to create a separate Thread object that handles your main logic/rendering loop.
That you can start in your constructor, but it would be safer to start in your main(String[] args) method, just in case.

By not allowing control to drop back into Swing, you're preventing it from actually pack()ing the JFrame and preparing it for rendering.

\$\endgroup\$
7
  • \$\begingroup\$ Well, where would the update logic go? Into the class where I have my main method? I'm a little bit confused about how to implement the loop, and where. \$\endgroup\$ Jul 29, 2012 at 1:05
  • \$\begingroup\$ In this case, you'd actually rename your start() method to run(), implement the Runnable interface, and create a new Thread(this), calling its start() method to start the process. \$\endgroup\$ Jul 29, 2012 at 1:09
  • \$\begingroup\$ Ah,I was gonna try and do this without threads, but it looks like I have to. \$\endgroup\$ Jul 29, 2012 at 1:16
  • \$\begingroup\$ I don't think it's possible to avoid using at least one thread. The main Swing thread is used to update all the UI elements, and it's not quite possible to permanently tack code onto the end of it. Having your own thread means you can control how it runs yourself. \$\endgroup\$ Jul 29, 2012 at 1:43
  • \$\begingroup\$ I'm still having a problem getting my updates to render. I created a new Thread(this); inside the constructor, which I imply calls the run() method. I posted the run method in the question. It complies, and this time the screen stays, and is able to be seen the whole time; but nothing is moving. Should I post my 2 update methods? \$\endgroup\$ Jul 30, 2012 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .