public void buttonPressed(Button button) {
    if (button.getId() == TitleMenu.RESTART_GAME_ID) {
      clearMenus();
      TitleMenu menu = new TitleMenu(GAME_WIDTH, GAME_HEIGHT);
      addMenu(menu);

    } else if (button.getId() == TitleMenu.START_GAME_ID) {
      clearMenus();
      isMultiplayer = false;

      localId = 0;
      synchronizer = new TurnSynchronizer(this, null, 0, 1);
      synchronizer.setStarted(true);

      createLevel(TitleMenu.level);
    } else if (button.getId() == TitleMenu.SELECT_LEVEL_ID) {
      addMenu(new LevelSelect(false));
    } else if (button.getId() == TitleMenu.SELECT_HOST_LEVEL_ID) {
      addMenu(new LevelSelect(true));
    } else if (button.getId() == TitleMenu.HOST_GAME_ID) {
      addMenu(new HostingWaitMenu());
      isMultiplayer = true;
      isServer = true;
      try {
        if (isServer) {
          localId = 0;
          serverSocket = new ServerSocket(3000);
          serverSocket.setSoTimeout(1000);

          hostThread =
              new Thread() {

                public void run() {
                  boolean fail = true;
                  try {
                    while (!isInterrupted()) {
                      Socket socket = null;
                      try {
                        socket = serverSocket.accept();
                      } catch (SocketTimeoutException e) {

                      }
                      if (socket == null) {
                        System.out.println("asdf");
                        continue;
                      }
                      fail = false;

                      packetLink = new NetworkPacketLink(socket);

                      createServerState = 1;
                      break;
                    }
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                  if (fail) {
                    try {
                      serverSocket.close();
                    } catch (IOException e) {
                    }
                  }
                };
              };
          hostThread.start();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (button.getId() == TitleMenu.JOIN_GAME_ID) {
      addMenu(new JoinGameMenu());
    } else if (button.getId() == TitleMenu.CANCEL_JOIN_ID) {
      popMenu();
      if (hostThread != null) {
        hostThread.interrupt();
        hostThread = null;
      }
    } else if (button.getId() == TitleMenu.PERFORM_JOIN_ID) {
      menuStack.clear();
      isMultiplayer = true;
      isServer = false;

      try {
        localId = 1;
        packetLink = new ClientSidePacketLink(TitleMenu.ip, 3000);
        synchronizer = new TurnSynchronizer(this, packetLink, localId, 2);
        packetLink.setPacketListener(this);
      } catch (Exception e) {
        e.printStackTrace();
        // System.exit(1);
        addMenu(new TitleMenu(GAME_WIDTH, GAME_HEIGHT));
      }
    } else if (button.getId() == TitleMenu.SELECT_DIFFICULTY_ID) {
      addMenu(new DifficultySelect());
    } else if (button.getId() == TitleMenu.EXIT_GAME_ID) {
      System.exit(0);
    }
  }
  public void run() {
    long lastTime = System.nanoTime();
    double unprocessed = 0;
    int frames = 0;
    long lastTimer1 = System.currentTimeMillis();

    try {
      init();
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    // if (!isMultiplayer) {
    // createLevel();
    // }

    int toTick = 0;

    long lastRenderTime = System.nanoTime();
    int min = 999999999;
    int max = 0;

    while (running) {
      if (!this.hasFocus()) {
        keys.release();
      }

      double nsPerTick = 1000000000.0 / framerate;
      boolean shouldRender = false;
      while (unprocessed >= 1) {
        toTick++;
        unprocessed -= 1;
      }

      int tickCount = toTick;
      if (toTick > 0 && toTick < 3) {
        tickCount = 1;
      }
      if (toTick > 20) {
        toTick = 20;
      }

      for (int i = 0; i < tickCount; i++) {
        toTick--;
        // long before = System.nanoTime();
        tick();
        // long after = System.nanoTime();
        // System.out.println("Tick time took " + (after - before) *
        // 100.0 / nsPerTick + "% of the max time");
        shouldRender = true;
      }
      // shouldRender = true;

      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
        createBufferStrategy(3);
        continue;
      }
      if (shouldRender) {
        frames++;
        Graphics g = bs.getDrawGraphics();

        Random lastRandom = TurnSynchronizer.synchedRandom;
        TurnSynchronizer.synchedRandom = null;

        render(g);

        TurnSynchronizer.synchedRandom = lastRandom;

        long renderTime = System.nanoTime();
        int timePassed = (int) (renderTime - lastRenderTime);
        if (timePassed < min) {
          min = timePassed;
        }
        if (timePassed > max) {
          max = timePassed;
        }
        lastRenderTime = renderTime;
      }

      long now = System.nanoTime();
      unprocessed += (now - lastTime) / nsPerTick;
      lastTime = now;

      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      if (shouldRender) {
        if (bs != null) {
          bs.show();
        }
      }

      if (System.currentTimeMillis() - lastTimer1 > 1000) {
        lastTimer1 += 1000;
        fps = frames;
        frames = 0;
      }
    }
  }