Exemple #1
0
  private void loadImages() throws Exception {
    // background
    backgroundControl = new FramedControl();
    backgroundControl.setColor(Color.BLACK);
    backgroundControl.setOpacity(0.5f);
    backgroundControl.setSize(new Size(displayMode.getWidth(), displayMode.getHeight()));
    backgroundControl.setTopLeftPosition(new Point(0, 0));
    WindowManager.getInstance().setBackgroundControl(backgroundControl);

    // middle dot
    middleDotControl = new VisualControl();
    middleDotControl.setTexture(getClass().getResource(URL_IMAGE_MIDDLE_DOT));
    Size middleDotControlSize = middleDotControl.getSize();
    middleDotControl.setSize(new Size(middleDotControlSize.getWidth(), displayMode.getHeight()));
    middleDotControl.setTopLeftPosition(
        new Point(((displayMode.getWidth() / 2.0f) - (middleDotControlSize.getWidth() / 2.0f)), 0));

    // top bar
    BounceableControl topBar = new BounceableControl();
    topBar.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    topBar.setSize(new Size(displayMode.getWidth(), BAR_SIZE));
    topBar.setTopLeftPosition(new Point(0, 0));
    bounceableControls.add(topBar);

    // bottom bar
    BounceableControl bottomBar = new BounceableControl();
    bottomBar.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    bottomBar.setSize(new Size(displayMode.getWidth(), BAR_SIZE));
    bottomBar.setTopLeftPosition(new Point(0, (displayMode.getHeight() - BAR_SIZE)));
    bounceableControls.add(bottomBar);

    // win/lose
    wonControl = new VisualControl();
    // wonControl.setColor(Color.BLACK);
    wonControl.setTexture(getClass().getResource(URL_IMAGE_WON));
    wonControl.setVisible(false);
    lostControl = new VisualControl();
    // lostControl.setColor(Color.BLACK);
    lostControl.setTexture(getClass().getResource(URL_IMAGE_LOST));
    lostControl.setVisible(false);

    // paddles
    for (int i = 0; i < NUMBER_OF_PADDLES; i++) {
      BounceableControl paddle = new BounceableControl();
      paddle.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
      paddle.setVisible(false);
      paddles.put(i, paddle);
    }

    // ball shadows
    for (int i = 0; i < NUMBER_OF_BALL_SHADOWS; i++) {
      float opacity = (1.0f - ((1.0f / (float) (NUMBER_OF_BALL_SHADOWS + 1)) * (i + 1)));
      float size =
          (BALL_SIZE * (NUMBER_OF_BALL_SHADOWS / (float) (NUMBER_OF_BALL_SHADOWS + (i + 1))));
      log.debug("opacity: " + opacity + ", size: " + size);

      VisualControl visualControl = new VisualControl();
      visualControl.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
      visualControl.setSize(new Size(size, size));
      visualControl.setOpacity(opacity);
      visualControl.setVisible(false);
      ballShadows.addLast(visualControl);
    }

    // ball
    ball = new VisualControl();
    // ball.setColor(Color.BLACK);
    ball.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    ball.setSize(new Size(BALL_SIZE, BALL_SIZE));
    // ball.setMargin(1.0f);
    ball.setVisible(false);

    // listeners
    backgroundControl.addTouchListener(this);
  }
Exemple #2
0
  public void run(Map<String, String> parameters) {
    DisplayManager.create();
    DisplayManager displayManager = DisplayManager.getInstance();
    displayManager.setWindowTitle(TITLE);
    ObjectObserver objectObserver = new ObjectObserverMoteJ();
    WindowManager windowManager = null;

    try {
      for (DisplayMode mode : displayManager.getAvailableDisplayModes()) {
        log.info(mode.getWidth() + "x" + mode.getHeight() + ":" + mode.getBitsPerPixel());
      }
      displayMode = displayManager.getDisplayMode(800, 600, 32);
      displayManager.createDisplay(displayMode);
      Keyboard.create();

      objectObserver.initialize(parameters);

      WindowManager.create(displayManager, objectObserver);
      windowManager = WindowManager.getInstance();
      windowManager.getWindowManagerCalibrator().addWindowManagerCalibratorListener(this);

      FrameMeter frameMeter = new FrameMeter();
      calibrationRequested = true;
      isCalibrated = false;
      isRunning = true;
      while (isRunning) {
        if (Display.isCloseRequested()) {
          break;
        }

        while (Keyboard.next()) {
          if (Keyboard.getEventKey() == Keyboard.KEY_F) {
            if (Keyboard.getEventKeyState()) {
              displayManager.setFullScreen(!displayManager.isFullScreen());
            }
          }
          if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
            isRunning = false;
          }
          if (Keyboard.getEventKey() == Keyboard.KEY_C) {
            calibrationRequested = true;
          }
          if (Keyboard.getEventKey() == Keyboard.KEY_UP) {
            ballSpeed += 0.1f;
          }
          if (Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
            ballSpeed -= 0.1f;
          }
        }

        if (calibrationRequested) {
          windowManager.calibrate();
          calibrationRequested = false;
          isCalibrated = false;
        } else if (isCalibrated) {
          windowManager.setCursorCollection(new CursorCollectionDefault());

          log.info("Loading images...");
          loadImages();
          isCalibrated = false;

          gameState = GameState.Ready;
        }

        if (frameMeter.update()) {
          displayManager.setWindowTitle(TITLE + " - " + frameMeter.getFps() + " fps");
        }

        // game state
        if (gameState != null) {
          if (gameState.equals(GameState.Ready)) {
            onGameStateReady();
          } else if (gameState.equals(GameState.Start)) {
            onGameStateStart();
          } else if (gameState.equals(GameState.Playing)) {
            onGameStatePlaying();
          } else if (gameState.equals(GameState.End)) {
            onGameStateEnd();
          }
        }

        windowManager.update();
        Thread.sleep(30);
      }
    } catch (Exception exception) {
      exception.printStackTrace();
    } finally {
      try {
        if (windowManager != null) {
          windowManager.destroy();
        }
        Keyboard.destroy();
        displayManager.destroy();
      } catch (Exception exception) {
        exception.printStackTrace();
      }
    }
  }