예제 #1
0
  @Override
  public void run() {
    double reset = getWidth() - letters[0].getWidth();
    while (true) {

      for (int i = 0; i < letters.length; i++) {

        if (letters[i].getBounds().intersects(player.getBounds())) {
          if (letters[i].getLabel().equals("A")) score--; // A for avoid lose a point
          else score++; // G for get earn 1 point
          scoreLabel.setText("Score: " + score);
          // move the letter immediately
          letters[i].setLocation(reset, letters[i].getY());
        } else {
          letters[i].move(-speed, 0);
          // wrap around if past the left edge
          if (letters[i].getX() < 0) letters[i].setLocation(reset, letters[i].getY());
        }
      }
      pause(gameSpeed);
    }
  }
예제 #2
0
  @Override
  public void init() {
    randgen = new RandomGenerator();
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setTitle("Get or Avoid!");
    setBackground(Color.gray);
    score = 0;
    scoreLabel = new JLabel("Score: " + score);
    scoreLabel.setFont(new Font("Cambria", Font.BOLD, 18));
    add(scoreLabel, NORTH);

    letters = new GLabel[10];

    for (int i = 0; i < 10; i++) {
      letters[i] = makeLetterLabel(screenDivisions); // 5 initial divisions
      add(letters[i]);
    }

    player = new GLabel("P", INDENT, 3 * getHeight() / screenDivisions);
    player.setFont(new Font("Cambria", Font.BOLD, 24));
    player.setColor(Color.blue);
    add(player);
    addKeyListeners();
  }