Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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).

share|improve this question

1 Answer 1

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.

share|improve this answer
    
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. –  Brendan Jul 29 '12 at 1:05
    
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. –  Raceimaztion Jul 29 '12 at 1:09
    
Ah,I was gonna try and do this without threads, but it looks like I have to. –  Brendan Jul 29 '12 at 1:16
    
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. –  Raceimaztion Jul 29 '12 at 1:43
    
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? –  Brendan Jul 30 '12 at 22:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.