HANGMAN小游戏

package HangmanGame0;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;
import javax.swing.*;
public class HangmanGame extends JFrame implements ActionListener {
	    JButton jb1=new JButton("play game");//三个选择项按钮
	    JButton jb2=new JButton("view instructions");
	    JButton jb3=new JButton("exit");
	    JLabel name=new JLabel("HANGMAN");  
		public HangmanGame(){
		      name.setFont(new Font("",0,30));
		      setTitle("HangmanGame");
		      setSize(300,200);
		      setLocationRelativeTo(null);
		      setVisible(true);
		      setLayout(new GridBagLayout());
		      GridBagConstraints c=new GridBagConstraints();//需要设置变量的大体位置
		      c.weighty=0.5;
		      add(name,c);
		      c.gridy=1;
		      c.fill=c.HORIZONTAL;
		      add(jb1,c);
		      c.gridy=2;
		      add(jb2,c);
		      c.gridy=3;
		      add(jb3,c);
		      jb1.addActionListener(this);
		      jb2.addActionListener(this);
		      jb3.addActionListener(this);
		}
		
		public static void main(String[] args){
			  HangmanGame a=new HangmanGame(); 
		}

		public void actionPerformed(ActionEvent e) {
		      if(e.getSource()==jb1){
		    	  gamewindow a=new gamewindow();
		      }
		      if(e.getSource()==jb2){
		    	  JOptionPane.showMessageDialog(this, "游戏说明:有这些国家可选择\nJAPAN\nQATAR\nSYRIA\nMONGOLIA\nBAHARAININDIA\nPAKISTAN\nAUSTRALIA\nCHINA");}  
		      if(e.getSource()==jb3){
		    	  System.exit(0);
		      }
		}
		class gamewindow extends JFrame implements ActionListener{
		     int time=0;//计次数使用判断是否输
		     int time1=0;//判断是否赢
		     boolean b=false;//依然判断用
		     JLabel word;//猜测的字母
		     String word1[]= {"JAPAN","QATAR","SYRIA","MONGOLIA","BAHARAIN","INDIA","PAKISTAN","AUSTRALIA","CHINA"};
		     String word2[]=new String[10];
		     Random r=new Random();
		     int n1=r.nextInt(10);//随机抽取国家名
		
		     int n2=word1[n1].length();//这个国家的字符长度
		     List<String> list=new ArrayList<>();
		     String n3="";//初始标签使用
		     JLabel message=new JLabel("GUESS A WORD");//用作大标题
		     JPanel jp1,jp2,key,key1,key2,key3;
		     String zimuda="QWERTYUIOPASDFGHJKLZXCVBNM";
		     String zimumax[]=zimuda.split("");//字符串数组
		     char[] zimumax1=zimuda.toCharArray();//字符数组
		     JButton a[]=new JButton[27];//按钮数组
		     public gamewindow(){
		     GridBagConstraints b=new GridBagConstraints();//需要设置变量的大体位置
		     setTitle("Game");
		     setVisible(true);
		     setSize(500, 450);
		     setLocationRelativeTo(null);
		
		     for(int i=0;i<26;i++){
		            a[i]= new JButton(zimumax[i]);
		     }
		     		a[26]=new JButton("play");
		            jp1=new JPanel();//上面的大标题
		            jp1.setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
		            jp1.add(message);
		            message.setFont(new Font("",0,20));//设置字体
		            add(jp1,BorderLayout.NORTH);
		
		            jp2=new JPanel();//主窗口
		            jp2.setBackground(Color.WHITE);
		            jp2.setLayout(new GridBagLayout() );
		            for(int i=0;i<n2;i++) {list.add("_ "); n3=n3+list.get(i);}
		            word=new JLabel(n3);
		            word.setFont(new Font("",0,35));//设置字体
		            jp2.add(word,b);
		            add(jp2,BorderLayout.CENTER); 
		
		              key=new JPanel();
		              key.setLayout(new GridBagLayout() );
		              key.setBackground(Color.BLACK);
		
		              b.gridx=0;//横方向
		              b.gridy=0;//纵方向
		              b.weighty=0.1;
		              b.anchor=b.CENTER;
		          key1=new JPanel();   key2=new JPanel();   key3=new JPanel();
		          key1.setBackground(Color.black);
		          key2.setBackground(Color.black);
		          key3.setBackground(Color.black);
		              for(int i=0;i<10;i++) {key1.add(a[i]);}
		              for(int i=10;i<19;i++) {key2.add(a[i]);}
		              for(int i=19;i<27;i++) {key3.add(a[i]);}
		              
		              key.add(key1,b);b.gridy++; key.add(key2,b);b.gridy++; key.add(key3,b);
		      add(key,BorderLayout.SOUTH);
		      for(int i=0;i<26;i++) {a[i].addActionListener(this);}
		      }
		     int t;
		    public void actionPerformed(ActionEvent e) {
		        for(int i=0;i<26;i++)
		            if(a[i]==e.getSource()){
			            a[i].setBackground(Color.red);
		            String n4="";
		            for(int j=0;j<n2;j++){
			             if(word1[n1].charAt(j)==zimumax1[i]){
			             b=true;
			             time1++;
			             a[i].setBackground(Color.GREEN);	
			             list.set(j,zimumax[i]+" ");
			             }
		            n4=n4+list.get(j);
		            }
			            if(!b) {
			            	time++;
			            	t=time+time1+1;
				            JLabel message=new JLabel("目前已经错了"+time+"次"+" "+"进行到了第"+t+"次");
				            jp1=new JPanel();//上面的大标题
					        jp1.setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
					        jp1.add(message);
					        message.setFont(new Font("",0,20));//设置字体
					        add(jp1,BorderLayout.NORTH);
			            }

		            b=false;
		            word.setText(n4);
		            if(time==5) {JOptionPane.showMessageDialog(this,"你输了!");dispose();}
		            if(time1==n2) {JOptionPane.showMessageDialog(this,"你赢了!");dispose();}
		            }
		    }
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值