Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/com/zetcode/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void paintComponent(Graphics g) {
}

private void doDrawing(Graphics g) {

if (inGame) {

g.drawImage(apple, apple_x, apple_y, this);
Expand All @@ -111,18 +111,22 @@ private void doDrawing(Graphics g) {
} else {

gameOver(g);
}

}

}

private void gameOver(Graphics g) {

String msg = "Game Over";
String restartmsg = "Press R to Restart";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);

g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
g.drawString(restartmsg, (B_WIDTH - metr.stringWidth(restartmsg)) / 2, B_HEIGHT / 2 + 20);
}

private void checkApple() {
Expand Down Expand Up @@ -210,6 +214,22 @@ public void actionPerformed(ActionEvent e) {
repaint();
}


private void restartGame() {
inGame = true;
leftDirection = false;
rightDirection = true;
upDirection = false;
downDirection = false;
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * 10; y[i] = 50;
}
locateApple();
timer.start();
}


private class TAdapter extends KeyAdapter {

@Override
Expand Down Expand Up @@ -240,6 +260,10 @@ public void keyPressed(KeyEvent e) {
rightDirection = false;
leftDirection = false;
}

if( (key == KeyEvent.VK_R) && (!inGame)){
restartGame();
}
}
}
}