Ejemplo n.º 1
0
  public void play(int lv) {
    jl.setText("Level " + level);
    Game game = new Game(lv); // An object representing the game
    View view = new View(game); // An object representing the view of the game
    game.newGame();
    view.print();
    gameBoardPanel = view.mainPanel;
    ButtonPanel buttonPanel = new ButtonPanel(game);
    container.add(buttonPanel, BorderLayout.EAST);
    container.add(gameBoardPanel, BorderLayout.WEST);
    mainFrame.pack();

    // Main game loop
    while (true) {

      view.print();
      gameBoardPanel = view.mainPanel;

      // Win/lose conditions
      if (game.isWin()) {
        view.print();
        gameBoardPanel = view.mainPanel;
        int choice;
        choice = JOptionPane.showConfirmDialog(null, "You win!", "", JOptionPane.OK_OPTION);
        if (choice == JOptionPane.OK_OPTION) {
          level++;
          mainFrame.remove(buttonPanel);
          mainFrame.remove(gameBoardPanel);
          play(level);
        }
      }
      if (game.isLose()) {
        view.print();
        gameBoardPanel = view.mainPanel;
        int choice;
        choice =
            JOptionPane.showConfirmDialog(
                null, "You lose!", "Would you like to play again?", JOptionPane.YES_NO_OPTION);
        if (choice == JOptionPane.YES_OPTION) {
          level = 1;
          mainFrame.remove(buttonPanel);
          mainFrame.remove(gameBoardPanel);
          play(level);
        } else {
          System.exit(0);
        }
      }
    }
  }
Ejemplo n.º 2
0
 public void actionPerformed(ActionEvent ae) {
   JButton button = (JButton) ae.getSource();
   if (button == high) {
     frame.remove(main);
     frame.setContentPane(scores);
   }
   if (button == cred) {
     frame.remove(main);
     frame.setContentPane(credits);
   }
   if (button == start) {
     frame.remove(main);
     frame.setContentPane(story);
   }
   if (button == stor) {
     frame.remove(story);
     frame.setContentPane(tutorial);
   }
   if (button == tutor) {
     frame.remove(main);
     DesktopLauncher a = new DesktopLauncher();
     a.run();
   }
   if (button == returns1) {
     frame.remove(scores);
     frame.setContentPane(main);
   }
   if (button == returns2) {
     frame.remove(credits);
     frame.setContentPane(main);
   }
   if (button == returns3) {
     frame.remove(story);
     frame.setContentPane(main);
   }
   if (button == returns4) {
     frame.remove(tutorial);
     frame.setContentPane(story);
   }
   frame.validate();
   frame.repaint();
 }
Ejemplo n.º 3
0
 public void actionPerformed(ActionEvent e) {
   level = 1;
   mainFrame.remove(gameBoardPanel);
   play(level);
 }