为我的简单游戏设置难度级别

时间:2013-12-16 19:18:51

标签: java user-interface methods

我一直在编写一个简单的网格盗版游戏,我希望玩家能够看到一个屏幕,上面有两个标签,简单易用。根据按下它的按钮,它将关闭该屏幕并打开一个具有不同尺寸网格的新窗口。目前,代码是针对三个三格的编写的,我想将其设置为难,并且想要编写一个二乘二以便于编码。我遇到麻烦的是宣布新的方法来放入另一个gui,与我已经拥有的gui分开。任何建议/解决方案将不胜感激?

  import java.awt.*;
  import javax.swing.*;
  import javax.swing.Timer;
 import java.util.*; 
 import java.awt.event.*;





  public class PirateGame extends JFrame implements ActionListener  {

  JLabel label1, label2, label3;

  ImageIcon image1, image2, image3, image4, image5; 

  JTextField textResult; 

  JButton [] buttons;


  int treasureLocation; 

  int clicks = 0;

 public int count = 0;       


 public static void main(String[] args){


  new PirateGame();

  }


   public PirateGame (){




  this.setSize(700,700);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("Treasure Hunt Game");

  JPanel thePanel = new JPanel();


  thePanel.setLayout(new GridLayout(0,3,0,0));

  image1 = new ImageIcon(getClass().getResource("Treasure.jpg"));
  image2 = new ImageIcon(getClass().getResource("Pirate.jpg"));
  image3 = new ImageIcon(getClass().getResource("sand2.jpg"));
  image4 = new ImageIcon(getClass().getResource("emptyhole.jpg"));   
  image5 = new ImageIcon(getClass().getResource("map.jpg"));

  label1 = new JLabel("Click the buttons!");
  label1.setFont(new Font("Serif", Font.PLAIN, 20));
  label1.setForeground(Color.red);


  label2 = new JLabel(image5); 
  label3 = new JLabel(image2);


  buttons = new JButton[9]; 
  buttons[0] = new JButton(image3);
  buttons[1] = new JButton(image3);
  buttons[2] = new JButton(image3);
  buttons[3] = new JButton(image3);
  buttons[4] = new JButton(image3);
  buttons[5] = new JButton(image3);
  buttons[6] = new JButton(image3);
  buttons[7] = new JButton(image3);
  buttons[8] = new JButton(image3);





  thePanel.add(buttons[0]);
  thePanel.add(buttons[1]);
  thePanel.add(buttons[2]);
  thePanel.add(buttons[3]);
  thePanel.add(buttons[4]);
  thePanel.add(buttons[5]);
  thePanel.add(buttons[6]);
  thePanel.add(buttons[7]);
  thePanel.add(buttons[8]);
  thePanel.add(label1); 
  thePanel.add(label2);
  thePanel.add(label3);

  buttons[0].addActionListener(this);
  buttons[1].addActionListener(this);
  buttons[2].addActionListener(this);
  buttons[3].addActionListener(this);
  buttons[4].addActionListener(this);
  buttons[5].addActionListener(this);
  buttons[6].addActionListener(this);
  buttons[7].addActionListener(this);
  buttons[8].addActionListener(this);



  this.add(thePanel);

  this.setVisible(true);



  treasureLocation = new Random().nextInt(buttons.length);


  System.out.println(treasureLocation);




   }




   public void actionPerformed(ActionEvent evt){
   Object source = evt.getSource();

  if (source == buttons[treasureLocation]) {


     buttons[treasureLocation].setIcon(image1);
     label1.setText("You've found me Treasure!");

     Timer timer = new Timer(3000,  
           new ActionListener() {


              public void actionPerformed(ActionEvent evt) { 

                 PirateGame.this.setVisible(false);
                 PirateGame.this.dispose();
                 new PirateGame();

              }
           }); 
     timer.setRepeats(false); 
     timer.start();
   }
   else
   {((JButton)source).setIcon(image4); 
     label1.setText("Keep tryin' ARGG!");

   }


   clicks++;
   System.out.println(clicks);


   if ((clicks == 5) && (source !=  buttons[treasureLocation])){

     label1.setText("One more try Matey!");

   }

   if ((clicks == 6) && (source !=  buttons[treasureLocation])) {

     label1.setText("Walk the Plank!");


     Timer timer2 = new Timer(3000,  
           new ActionListener() {


              public void actionPerformed(ActionEvent evt) { 

                 PirateGame.this.setVisible(false);
                 PirateGame.this.dispose();
                 new PirateGame();

              }
           }); 
     timer2.setRepeats(false); 
     timer2.start();
   }

   if (clicks == 7){

     PirateGame.this.setVisible(false);
     PirateGame.this.dispose();
     new PirateGame();
   }

  }  





 }

1 个答案:

答案 0 :(得分:0)

对于初学者 - 将所有ui创建代码移动到单独的方法

CreateUI(int GridSize)

使用for循环创建按钮等。

int numButtons = GridSize * GridSize;
buttons = new JButton[Grid]; 
for ( int i = 0; i < numButtons; i++ )
{
 buttons[i] = new JButton(image3);
}

对于初学者来说,让你的UI工作,这样你就可以传递它2或3,只需手动更改代码并测试它们是否都有效。

然后,您必须了解如何在运行时切换UI。我猜 是你的代码:

  this.add(thePanel);

  this.setVisible(true);

您需要弄清楚如何删除面板,制作新面板并添加它。这些是一些开始的想法...

相关问题