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