JAVA华容道完整代码,java实现华容道
用java设计一个华容道游戏
import java.awt.*;
成都创新互联公司专注于城固网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供城固营销型网站建设,城固网站制作、城固网页设计、城固网站官网定制、成都小程序开发服务,打造城固网络公司原创品牌,更为您提供城固网站排名全网营销落地服务。
import java.awt.event.*;
public class MoveExample //主类
{
public static void main(String args[]) //定义主方法
{
new Hua_Rong_Road(); //创建对象
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(128,128,128);
Person(int number,String s)//构造方法
{
super(s);//调用父类s的构造方法
setBackground(c);//设置组件的背景色
this.number = number;//调用当前的number
c = getBackground();
addFocusListener(this);//添加焦点事件监听器
}
public void focusGained(FocusEvent e)//焦点事件触发
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦点事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person类的数组
Button left,right,above,below;
Button restart = new Button("重新开始");
public Hua_Rong_Road() //华容道的构造方法
{
init(); //初始化
setBounds(100,100,320,360);//设置窗口在屏幕上出现位置,和窗口大小
setVisible(true);//设置窗口可见
setResizable(true);//设置窗口可调节
validate();//刷新
addWindowListener( new WindowAdapter()//获得窗口事件监视器
{
public void windowClosing(WindowEvent e)//窗口正在被关闭时,窗口监视器调用该方法
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);//设置默认布局
add(restart);//添加重新开始
restart.setBounds(100,320,120,25);//重新开始按钮大小
restart.addActionListener(this);//事件源获得监视器
String name[] = {"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};
for(int k = 0;kname.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,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
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,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);//为每个人物按钮设置位置和大小
person[9].requestFocus();//把焦点先设置在这个按钮上
left = new Button();//画出游戏界面边框,并用定义的left,right,above,below控制大小
right = new Button();
above = new Button();
below = new Button();
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)//响应用户按下方向光标的操作;用KeyEvent类中的getkeycode()判断哪个键被按下
{
go(man,below); //go方法控制移动
}
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(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/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,Button 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;k10;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);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
用java编程 华容道游戏
import java.awt.*;
import java.awt.event.*;
public class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);
this.number = number;
c = getBackground();
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("重新开始");
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
restart.addActionListener(this);
String name[] = {"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};
for(int k = 0;kname.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,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
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,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocus();
left = new Button();
right = new Button();
above = new Button();
below = new Button();
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(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/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,Button 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;k10;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);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
这是我们课本上的,颜色不一样,其他的都差不多,不过是用awt组件写的,你应该是要用swing写的吧,照这个改改吧...
200求JAVA课程设计报告 关于手机华容道的
这个我试了的没有任务问题,稀望对你有点帮助,记得类名要改为Hua_Rong_Road ,因为只有Hua_Rong_Road 这个类是公开的.另外包名也改下package xxxx(你自己建的包名),玩游戏时移动人物,用键盘(上下左右 ,--,--,上,下)操作,鼠标是不能移动 人物的,照着我说的做,应该是没什么问题的:
package baidu.testfive;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
class People extends Button implements FocusListener // 代表华容道人物的类。
{
Rectangle rect = null;
int left_x, left_y;// 按扭的左上角坐标.
int width, height; // 按扭的宽和高.
String name;
int number;
People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 构造函数
{
super(s);
name = s;
this.number = number;
left_x = x;
left_y = y;
width = w;
height = h;
setBackground(Color.orange);
road.add(this);
addKeyListener(road);
setBounds(x, y, w, h);
addFocusListener(this);
rect = new Rectangle(x, y, w, h);
}
public void focusGained(FocusEvent e) {
setBackground(Color.red);
}
public void focusLost(FocusEvent e) {
setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,
ActionListener {
People people[] = new People[10];
Rectangle left, right, above, below;// 华容道的边界 .
Button restart = new Button("重新开始");
public void init() {
setLayout(null);
add(restart);
restart.setBounds(5, 5, 80, 25);
restart.addActionListener(this);
people[0] = new People(0, "曹操", 104, 54, 100, 100, this);// 构造曹操
people[1] = new People(1, "关羽", 104, 154, 100, 50, this);// 构造关羽
people[2] = new People(2, "张飞", 54, 154, 50, 100, this);
people[3] = new People(3, "刘备", 204, 154, 50, 100, this);
people[4] = new People(4, "张辽", 54, 54, 50, 100, this);
people[5] = new People(5, "曹仁", 204, 54, 50, 100, this);
people[6] = new People(6, "兵 ", 54, 254, 50, 50, this);
people[7] = new People(7, "兵 ", 204, 254, 50, 50, this);
people[8] = new People(8, "兵 ", 104, 204, 50, 50, this);
people[9] = new People(9, "兵 ", 154, 204, 50, 50, this);
people[9].requestFocus();
left = new Rectangle(49, 49, 5, 260);
people[0].setForeground(Color.white);
right = new Rectangle(254, 49, 5, 260);
above = new Rectangle(49, 49, 210, 5);
below = new Rectangle(49, 304, 210, 5);
}
public void paint(Graphics g) {// 画出华容道的边界:
g.setColor(Color.cyan);
g.fillRect(49, 49, 5, 260);// left.
g.fillRect(254, 49, 5, 260);// right.
g.fillRect(49, 49, 210, 5); // above.
g.fillRect(49, 304, 210, 5);// below.
// 提示曹操逃出位置和按键规则:
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动", 100, 20);
g.setColor(Color.red);
g.drawString("曹操到达该位置", 110, 300);
}
public void keyPressed(KeyEvent e) {
People man = (People) e.getSource();// 获取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
man.left_y = man.left_y + 50; // 向下前进50个单位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(below)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
man.left_y = man.left_y - 50; // 向上前进50个单位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判断是否和其它人物或上边界出现重叠,如果出现重叠就退回50个单位距离。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(above)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
man.left_x = man.left_x - 50; // 向左前进50个单位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判断是否和其它人物或左边界出现重叠,如果出现重叠就退回50个单位距离。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(left)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
man.left_x = man.left_x + 50; // 向右前进50个单位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判断是否和其它人物或右边界出现重叠,如果出现重叠就退回50个单位距离。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(right)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void actionPerformed(ActionEvent e) {
this.removeAll();
this.init();
}
}
标题名称:JAVA华容道完整代码,java实现华容道
网站地址:http://hbruida.cn/article/hscdie.html