一个华容道的程序 其中一个方法

Surrin1999 2018-04-12 09:05:30

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

[java] view plain copy
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;k<name.length;k++) {
person[k]=new Person(k,name[k]);
person[k].addMouseListener(this);//添加鼠标事件
person[k].addKeyListener(this);//添加键盘事件
add(person[k]);
}
person[0].setBounds(104,54,99,99);
person[1].setBounds(104,154,99,49);
person[2].setBounds(54,154,49,99);
person[3].setBounds(204,154,49,99);
person[4].setBounds(54,54,49,99);
person[5].setBounds(204,54,49,99);
person[6].setBounds(54,254,49,49);
person[7].setBounds(204,254,49,49);
person[8].setBounds(104,204,49,49);
person[9].setBounds(154,204,49,49);
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 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;
}//如何判断可不可以移动的 personRect 、directionRect 到底是什么 需要详细解释。。没玩过华容道规则不太懂
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();//释放内存,重置
}
}

[java] view plain copy
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);
}
}



请求红字部分每一行都有详细注释 真心看不懂。。没玩过华容道
...全文
926 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
给你个建议,先去玩玩华容道。可能就明白了。得现熟悉业务,才能懂代码。
Surrin1999 2018-04-16
  • 打赏
  • 举报
回复
引用 3 楼 lye2000000_super 的回复:
给你个建议,先去玩玩华容道。可能就明白了。得现熟悉业务,才能懂代码。
发完贴第二天研究了以下已经基本懂了。。看看有没大神详细解答验证下就差不多了。。
Surrin1999 2018-04-13
  • 打赏
  • 举报
回复
哪位大神理我一下啊
Surrin1999 2018-04-12
  • 打赏
  • 举报
回复
请求红字部分每一行都有详细注释 真心看不懂。。没玩过华容道

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧