Ejemplo n.º 1
0
 @Override
 public void keyPressed(KeyEvent event) {
   char key = event.getKeyChar();
   if (key == 'k') {
     player.move(0, playerspeed);
   } else if (key == 'i') {
     player.move(0, -playerspeed);
   }
 }
Ejemplo n.º 2
0
 public GLabel makeLetterLabel(int numDivs) {
   boolean get = randgen.nextBoolean();
   String ga;
   if (get) ga = "G";
   else ga = "A";
   GLabel getOrAvoid = new GLabel(ga);
   getOrAvoid.setFont(new Font("Cambria", Font.BOLD, 24));
   double locx = randgen.nextDouble(INDENT, getWidth() - getOrAvoid.getWidth());
   int whichdiv = randgen.nextInt(1, numDivs);
   double locy = whichdiv * getHeight() / numDivs;
   getOrAvoid.setLocation(locx, locy);
   return getOrAvoid;
 }
Ejemplo n.º 3
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();
  }
Ejemplo n.º 4
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);
    }
  }