/** Initialize the game */ public void run() { // Get ready getReady(); vx = 0; vy = 0; num_brick_left = NUM_BRICK_ROW * NUM_BRICK_COL; // Add label at the bottom mylabel = new GLabel("Developed by Teng Zhong"); add(mylabel, (getWidth() - mylabel.getWidth()) / 2, (getHeight() - 300)); // Start game playing while (!gameover()) { moveBall(); checkRebounce(); pause(DELAY); } // When game is over remove(ball); GLabel lab = new GLabel("Game Over!"); lab.setFont("SansSerif-28"); lab.setColor(Color.RED); add(lab, (getWidth() - lab.getWidth()) / 2, (getHeight() - lab.getHeight()) / 2); }
public void showMessage(String message) { pausePlay(); gameMessage.setLabel(message); gameMessage.setLocation( WIDTH / 2 - gameMessage.getWidth() / 2, HEIGHT / 2 - gameMessage.getHeight() / 2); add(gameMessage); }
private void paint_label() { /* Write an inscription in the lower right corner of the window * From the window width and subtract the width of the label we get the start * point of the X inscription. (similar to height) */ GLabel label = new GLabel("Flag of " + COUNTRY, FLAG_WIDTH, getHeight()); label.setFont(new Font("Tamoha", Font.BOLD, 18)); label.setLocation((getWidth() - label.getWidth()), (getHeight() - label.getHeight() / 2)); add(label); }
public void restartGame() { getGCanvas().removeAll(); lives = NTURNS; bricksRemaining = NBRICK_ROWS * NBRICKS_PER_ROW; Color[] colors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN}; int xOffset = BRICK_SEP / 2; int yOffset = BRICK_Y_OFFSET; for (int row = 0; row < NBRICK_ROWS; row++) { Color color = colors[(int) ((double) colors.length / (double) NBRICK_ROWS * row)]; for (int col = 0; col < NBRICKS_PER_ROW; col++) { BreakoutBrick rect = new BreakoutBrick( col * BRICK_WIDTH + col * BRICK_SEP + xOffset, row * BRICK_HEIGHT + row * BRICK_SEP + yOffset, BRICK_WIDTH, BRICK_HEIGHT); rect.setColor(color); rect.setFilled(true); add(rect); } } paddle = new BreakoutPaddle( WIDTH / 2 - PADDLE_WIDTH / 2, HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET, PADDLE_WIDTH, PADDLE_HEIGHT); add(paddle); ball = new BreakoutBall(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS, BALL_RADIUS); add(ball); lifeDisplay.setLabel(String.format("Lives remaining: %d", lives)); lifeDisplay.setLocation(WIDTH - lifeDisplay.getWidth() - 10, lifeDisplay.getHeight() + 10); add(lifeDisplay); resumePlay(); }
public void mouseMoved(MouseEvent e) { if (label != null) remove(label); label = new GLabel("X: " + e.getX() + " Y: " + e.getY()); add(label, (getWidth() - label.getWidth()) / 2, (getHeight() - label.getHeight()) / 2); }