Пример #1
0
  @Override
  public void run() {
    if (state == GameState.Running) {
      while (true) {
        robot.update();
        if (robot.isJumped()) {
          currentSprite = characterJumped;
        } else if (robot.isDucked() == false) {
          // currentSprite = character;
          currentSprite = anim.getImage();
        }

        ArrayList<Projectile> projectiles = robot.getProjectiles();
        for (int i = 0; i < projectiles.size(); i++) {
          Projectile p = (Projectile) projectiles.get(i);
          if (p.isVisible() == true) {
            p.update();
          } else {
            projectiles.remove(i);
          }
        }

        // Update objects
        updateTiles();
        hb.update();
        hb2.update();
        bg1.update();
        bg2.update();

        animate();
        repaint();
        try {
          Thread.sleep(17);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        if (robot.getCenterY() > 500) {
          state = GameState.Dead;
        }
      }
    }
  }
Пример #2
0
  @Override
  public void keyPressed(KeyEvent e) {

    switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
        System.out.println("Move up");
        break;

      case KeyEvent.VK_DOWN:
        currentSprite = characterDown;
        if (robot.isJumped() == false) {
          robot.setDucked(true);
          robot.setSpeedX(0);
        }
        break;

      case KeyEvent.VK_LEFT:
        robot.moveLeft();
        robot.setMovingLeft(true);
        break;

      case KeyEvent.VK_RIGHT:
        robot.moveRight();
        robot.setMovingRight(true);
        break;

      case KeyEvent.VK_SPACE:
        robot.jump();
        break;

      case KeyEvent.VK_CONTROL:
        if (robot.isDucked() == false && robot.isJumped() == false) {
          robot.shoot();
          robot.setReadyToFire(false);
        }
        break;
    }
  }