JAVA:华容道游戏

public class game1 {
	public static void main(String args[]) {
		new HuaRongRoad();
	}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HuaRongRoad extends JFrame implements MouseListener,KeyListener,ActionListener{
	Person person[]=new Person[10];
	JButton left,right,above,below;
	JButton restart=new JButton("重新开始");
	public HuaRongRoad() {
		init();
		setTitle("华容道");
		setBounds(550,150,320,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		validate();
	}
	public void init() {
		setLayout(null);
		add(restart);
		restart.setBounds(100,360,120,35);
		restart.addActionListener(this);
		String name[]= {"曹操","关羽","张","刘","周","黄","兵","兵","兵","兵"};
		for(int k=0;kh/2)
			go(man,below);
		if(yw/2)
			go(man,right);
	}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void go(Person man,JButton direction) {
		boolean move=true;
		Rectangle manRect=man.getBounds();
		int x=man.getBounds().x;
		int y=man.getBounds().y;
		if(direction==below)
			y=y+50;
		else if(direction==above)
			y=y-50;
		else if(direction==left)
			x=x-50;
		else if(direction==right)
			x=x+50;
		manRect.setLocation(x,y);
		Rectangle directionRect=direction.getBounds();
		for(int k=0;k<10;k++) {
			Rectangle personRect=person[k].getBounds();
			if((manRect.intersects(personRect))&&(man.number!=k))
				move=false;
		}//判断可不可以移动
		if(manRect.intersects(directionRect))
			move=false;
		if(move==true)
			man.setLocation(x,y);
		if(man==person[0]&&man.getBounds().x==104&&man.getBounds().y==204) {
			JOptionPane.showMessageDialog(this,"You Win!","胜利",JOptionPane.PLAIN_MESSAGE);
			dispose();
			new HuaRongRoad();//判断胜利条件
		}
	}
	public void actionPerformed(ActionEvent e){
		dispose();
		new HuaRongRoad();//释放内存,重置
	}
}

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Person extends JButton implements FocusListener {
	int number;
	Color c=new Color(250,245,30);
	Font font=new Font("宋体",Font.BOLD,12);
	Person(int number,String s){
		super(s);
		setBackground(c);
		setFont(font);
		this.number=number;
		c=getBackground();
		addFocusListener(this);//添加焦点事件
	}
	public void focusGained(FocusEvent e) {
		setBackground(Color.blue);
	}
	public void focusLost(FocusEvent e) {
		setBackground(c);
	}
}

JAVA:华容道游戏_第1张图片

你可能感兴趣的:(JAVA:华容道游戏)