如何在游戏Hangman

时间:2018-06-02 21:48:33

标签: java user-interface listener keylistener

我的想法是你有刽子手游戏,你在String []中有几个单词,你可以找到猜到的单词的长度,然后你就会发出声音" "字符的长度与单词的长度一样多。如果我按下'在键盘上,字母在,填写" "按顺序排列。我不知道我是如何抓住用户的键盘信。 这是我到目前为止的代码

public class hangman extends JFrame{
    private JLabel lab1,skore,chyby;
    private JButton newGame,exit,start;
    private int pocetChyb = 0;
    private int pocetSkore = 0;
    private static Random rn = new Random();
    private String[] objektHry = {"apple","fruit","pear","football","hockey","game","vocabulary","sanctuary","chicken","dream","cat","person","boat","family","legs","cheese","cup","fish","socks","lips","oval","triangle","eyes","bee","rocket","hat","sun","corn","spider"};
private int delkaPole = objektHry.length;
private String zvoleneSlovo;
public static void main(String[] args)
{
    hangman gui = new hangman();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension d = new Dimension(1000,600);
    gui.setPreferredSize(d);
    gui.setVisible(true);
    gui.pack();
    gui.setLocationRelativeTo(null);
    gui.setResizable(false);
}
public hangman() {
   super("Hangman");    

   setLayout(null);

   SibeniceLepsi s = new SibeniceLepsi();
   this.add(s);
   s.setBounds(-50,-20,500,500);

   lab1 = new JLabel("HANGMAN");
   skore = new JLabel("Skore: 0");
   chyby = new JLabel("Chyby: 0");
   newGame = new JButton("New Game");
   exit = new JButton("EXIT");
   start = new JButton("Start Game");

   lab1.setFont(new Font("serifsains",1,80));
   exit.setFont(new Font("serifsains",1,60));
   newGame.setFont(new Font("serifsains",1,28));
   start.setFont(new Font("serifsains",1,28));
   skore.setFont(new Font("serifsains",1,20));
   chyby.setFont(new Font("serifsains",1,20));

   this.add(lab1);
   this.add(skore);
   this.add(chyby);
   this.add(exit);
   this.add(newGame);
   this.add(start);

   newGame.setVisible(false);

   lab1.setBounds(290,0,500,90);
   exit.setBounds(780,450,200,100);
   newGame.setBounds(580,450,200,100);
   start.setBounds(380,450,200,100);
   chyby.setBounds(400,300,100,40);
   skore.setBounds(400,350,100,40);
     start.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent evt){
              newGame.setVisible(true);
              int poradi = rn.nextInt(delkaPole) + 0;
              zvoleneSlovo = objektHry[poradi];
              System.out.println(zvoleneSlovo);



         }


     }); 
     exit.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent evt){
              System.exit(0);
         }


     }); 
     newGame.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent evt){
             pocetChyb = 0;
             pocetSkore = 0;
             chyby.setText("Chyby: "+pocetChyb);
             skore.setText("Skore: "+pocetSkore);
              skore.setText("Skore: "+pocetChyb);
              chyby.setText("Chyby: "+pocetSkore);
             dispose();
             String[] args = {};
             hangman.main(args);
         }


     }); 
     //here should be a listener which catches the letters
}

}

0 个答案:

没有答案