Java初学之华容道游戏

时间:2021-09-01 14:33:56
 package hhuarongdao;

 public class example {
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
 package hhuarongdao;
import java.awt.*;
import javax.swing.*; import java.awt.event.*;
public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener
{
Person [] person = new Person[10]; //person 为自定义的一个变量类型
JButton left,right,above,below ;
JButton restart = new JButton("重新开始");
public Hua_Rong_Road()
{
init();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100,100,320,500); //设置窗口初始位置以及大小的一个函数
setVisible(true); //窗口是否可见
validate(); //使用 validate 方法会使容器再次布置其子组件。确保布局有效。
}
public void init()
{
setLayout(null);
add(restart);
restart.addActionListener(this);
String name [] = {"曹操","关","张","刘","周","黄","兵","兵","兵","兵"};
for(int i=0;i<name.length;i++)
{
person[i]= new Person(i,name[i]);
person[i].addMouseListener(this);
person[i].addKeyListener(this);
add(person[i]);
}
person[0].setBounds(104, 54, 100, 100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,54,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204, 54, 50, 100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocus(); //将焦点放在控件上面
left=new JButton();
right=new JButton();
above=new JButton();
below = new JButton();
add(left);
add(right);
add(above);
add(below);
left.setBounds(49, 49, 5, 260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate(); //确保布局有效...这条语句不要也不会对程序有太大的影响
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man= (Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)
go(man,below);
if(e.getKeyCode()==KeyEvent.VK_UP)
go(man,above);
if(e.getKeyCode()==KeyEvent.VK_LEFT)
go(man,left);
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
go(man,right);
}
public void mousePressed(MouseEvent e)
{
Person man=(Person)e.getSource();
int x=-1,y=-1;
x=e.getX();
y=e.getY();
int w=man.getBounds().width;
int h=man.getBounds().height;
if(y>h/2) go(man,below);
if(y<h/2) go(man,above);
if(x<w/2) go(man,left);
if(x>w/2) go(man,right);
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void go(Person man, JButton direction) {
// TODO Auto-generated method stub
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below) y+=50;
else if(direction==above)y-=50;
else if(direction==left)x-=50;
else if(direction==right)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);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub }
public void actionPerformed(ActionEvent e)
{
dispose(); //注销
new Hua_Rong_Road();
} }
 package hhuarongdao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Person extends JButton implements FocusListener
{ int number ;
Color c= new Color(255,245,170);
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.red);
}
public void focusLost(FocusEvent e)
{
setBackground(c);
}
}

效果图:

Java初学之华容道游戏