华容道:
华容道是一个大家比较熟悉的益智类游戏。编写GUI程序,通过键盘和鼠标实现了人物的移动。
运行效果:
下面附上代码:
Main类
:
/**
* @author jitwxs
* @date 2021年04月01日 21:16
*/
public class Main {
public static void main(String[] args) {
new HuaRongRoad();
}
}
HuaRongRoad类
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author jitwxs
* @date 2021年04月01日 21:17
*/
public class HuaRongRoad extends JFrame implements MouseListener, KeyListener, ActionListener {
Person[] person=new Person[10];
JButton left,right,up,down;
JButton restart=new JButton("重新开始");
public HuaRongRoad(){
init();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100,100,320,500);
setVisible(true);
validate();
}
public void init(){
setLayout(null);
add(restart);
restart.setBounds(100,320,120,35);
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,100,50);
person[3].setBounds(204,154,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,204,50,50);
person[8].setBounds(104,54,50,50);
person[9].setBounds(154,204,50,50);
requestFocus();
left=new JButton();
right=new JButton();
up=new JButton();
down=new JButton();
add(left);
add(right);
add(up);
add(down);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
up.setBounds(49,49,210,5);
down.setBounds(49,304,210,5);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
Person man=(Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN){
go(man,down);
}
if(e.getKeyCode()==KeyEvent.VK_UP){
go(man,up);
}if(e.getKeyCode()==KeyEvent.VK_LEFT){
go(man,left);
}if(e.getKeyCode()==KeyEvent.VK_RIGHT){
go(man,right);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
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,down);
}if(y<h/2){
go(man,up);
}if(x<w/2){
go(man,left);
}if(x>w/2){
go(man,right);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(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==down){
y=y+50;
}else if(direction==up){
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 i=0;i<10;i++){
Rectangle personRect=person[i].getBounds();
if((manRect.intersects(personRect))&&(man.number!=i)){
move=false;
}
if(manRect.intersects(directionRect)){
move=false;
}
if(move==true){
man.setLocation(x,y);
}
}
}
public void action(ActionEvent e){
dispose();
new HuaRongRoad();
}
}
Person类
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
/**
* @author jitwxs
* @date 2021年04月01日 21:19
*/
public class Person extends JButton implements FocusListener {
int number;
Color c=new Color(235,123,111);
Font font=new Font("宋体",Font.BOLD,16);
Person(int number,String string){
super(string);
setBackground(c);
setFont(font);
this.number=number;
c=getBackground();
addFocusListener(this);
}
@Override
public void focusGained(FocusEvent e) {
setBackground(Color.lightGray);
}
@Override
public void focusLost(FocusEvent e) {
setBackground(c);
}
}