Пример #1
0
  private void start() {
    frame = new JFrame("Pong");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    panel =
        new JPanel() {
          public Dimension getPreferredSize() {

            return new Dimension(W, H);
          }

          public void paint(Graphics g) {
            for (Brick b : bricks) {
              b.paint(g);
            }

            ball.paint(g);
            paddle.paint(g);
          }
        };
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);

    while (true) {
      try {
        tick();
        frame.repaint();
        Thread.sleep(2);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
Пример #2
0
 /**
  * Called once per frame from {@link FrameRate}'s loop. Calls tick on the soundManager, and the
  * current game. (This will in turn call the tick of the Director, the SceneDirector and all
  * Actor's roles.
  *
  * @priority 3
  */
 public static void tick() {
   try {
     soundManager.tick();
     currentGame.tick();
   } catch (Exception e) {
     handleException(e);
   }
 }
Пример #3
0
  @Override
  public void nextStepForAllGames() {
    lock.writeLock().lock();
    try {
      for (Game game : games) {
        if (game.isGameOver()) {
          game.newGame();
        }
        game.tick();
      }

      HashMap<Player, PlayerData> map = new HashMap<Player, PlayerData>();
      for (int i = 0; i < games.size(); i++) {
        Game game = games.get(i);

        Player player = players.get(i);

        map.put(
            player,
            new PlayerData(
                gameType.getBoardSize(),
                decoder.encode(game.getBoardAsString()),
                player.getScore(),
                game.getMaxScore(),
                game.getCurrentScore(),
                player.getCurrentLevel() + 1,
                player.getMessage()));
      }

      screenSender.sendUpdates(map);

      for (int index = 0; index < players.size(); index++) {
        Player player = players.get(index);
        Game game = games.get(index);
        try {
          String board = game.getBoardAsString().replace("\n", "");

          if (logger.isDebugEnabled()) {
            logger.debug(String.format("Sent for player '%s' board \n%s", player, board));
          }

          controllers.get(index).requestControl(player, board);
        } catch (IOException e) {
          logger.error(
              "Unable to send control request to player "
                  + player.getName()
                  + " URL: "
                  + player.getCallbackUrl(),
              e);
        }
      }
    } catch (Error e) {
      e.printStackTrace();
      logger.error("nextStepForAllGames throws", e);
    } finally {
      lock.writeLock().unlock();
    }
  }
Пример #4
0
  public void playGame() {
    Joystick joystick = game.getJoystick();

    do {
      printBoard();

      String line = console.read();
      boolean bomb = false;
      boolean move = false;
      for (Character ch : line.toCharArray()) {
        if (ch == 's' || ch == 'ы') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.down();
          move = true;
        } else if (ch == 'a' || ch == 'ф') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.left();
          move = true;
        } else if (ch == 'd' || ch == 'в') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.right();
          move = true;
        } else if (ch == 'w' || ch == 'ц') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.up();
          move = true;
        } else if (ch == ' ') {
          if (bomb) {
            game.tick();
            move = false;
          }
          joystick.act();
          bomb = true;
        }
      }
      game.tick();
    } while (!game.isGameOver());

    printBoard();
    console.print("Game over!");
  }
Пример #5
0
  public void run() {
    init();
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60D;
    double ns = 1_000_000_000 / amountOfTicks;
    double delta = 0;

    while (running) {
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
      // only update 60 times per second
      if (delta >= 1) {
        tick();
        delta--;
      }
      // draw all objects onto screen
      render();
    }
  }
Пример #6
0
 @Override
 public void display(GLAutoDrawable drawable) {
   game.tick();
 }