网站首页 > 文章中心 > 其它

JAVA华容道完整代码

作者:小编 更新时间:2023-08-25 07:47:57 浏览量:44人看过

用java设计一个华容道游戏

import java.awt.*;

import java.awt.event.*;

public class MoveExample //主类

{

JAVA华容道完整代码-图1

public static void main(String args[]) //定义主方法

new Hua_Rong_Road(); //创建对象

}

class Person extends Button implements FocusListener

int number;

Person(int number,String s)//构造方法

super(s);//调用父类s的构造方法

setBackground(c);//设置组件的背景色

this.number = number;//调用当前的number

JAVA华容道完整代码-图2

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(); //初始化

setVisible(true);//设置窗口可见

setResizable(true);//设置窗口可调节

validate();//刷新

addWindowListener( new WindowAdapter()//获得窗口事件监视器

public void windowClosing(WindowEvent e)//窗口正在被关闭时,窗口监视器调用该方法

System.exit(0);

);

public void init()

setLayout(null);//设置默认布局

add(restart);//添加重新开始

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]);//添加人物

left = new Button();//画出游戏界面边框,并用定义的left,right,above,below控制大小

right = new Button();

above = new Button();

below = new Button();

add(left);

add(right);

add(above);

add(below);

} //完成界面布局

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;

go(man,below);

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)//向各个方向移动

else if(direction==above)

else if(direction==left)

else if(direction==right)

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))

if(move==true)

man.setLocation(x,y);

public void actionPerformed(ActionEvent e)

dispose();

new Hua_Rong_Road();

用java编程 华容道游戏

public class MoveExample

public static void main(String args[])

Person(int number,String s)

super(s);

this.number = number;

addFocusListener(this);

public void focusGained(FocusEvent e)

setBackground(Color.red);

public void focusLost(FocusEvent e)

Person person[] = new Person[10];

public Hua_Rong_Road()

init();

setVisible(true);

validate();

addWindowListener( new WindowAdapter()

public void windowClosing(WindowEvent e)

setLayout(null);

add(restart);

restart.addActionListener(this);

person[k] = new Person(k,name[k]);

person[k].addMouseListener(this);

person[k].addKeyListener(this);

add(person[k]);

left = new Button();

public void keyPressed(KeyEvent e)

if(e.getKeyCode()==KeyEvent.VK_DOWN)

public void mouseReleased(MouseEvent e){}

JAVA华容道完整代码-图3

Rectangle manRect = man.getBounds(); //什么意思?

int x = man.getBounds().x; //又不懂了

if(direction==below)

if((manRect.intersects(personRect))(man.number!=k))

这是我们课本上的,颜色不一样,其他的都差不多,不过是用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;

People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 构造函数

name = s;

left_x = x;

left_y = y;

width = w;

height = h;

setBackground(Color.orange);

road.add(this);

addKeyListener(road);

setBounds(x, y, w, h);

rect = new Rectangle(x, y, w, h);

public void focusGained(FocusEvent e) {

public void focusLost(FocusEvent e) {

public class Hua_Rong_Road extends Applet implements KeyListener,

ActionListener {

People people[] = new People[10];

Rectangle left, right, above, below;// 华容道的边界 .

public void init() {

people[0].setForeground(Color.white);

public void paint(Graphics g) {// 画出华容道的边界:

g.setColor(Color.cyan);

// 提示曹操逃出位置和按键规则:

g.setColor(Color.red);

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.setLocation(man.left_x, man.left_y);

man.rect.setLocation(man.left_x, man.left_y);

for (int i = 0; i 10; i++) {

if ((man.rect.intersects(people[i].rect)) (man.number != i)) {

if (man.rect.intersects(below)) {

if (e.getKeyCode() == KeyEvent.VK_UP) {

if (man.rect.intersects(above)) {

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

if (man.rect.intersects(left)) {

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

if (man.rect.intersects(right)) {

public void keyTyped(KeyEvent e) {

public void keyReleased(KeyEvent e) {

public void actionPerformed(ActionEvent e) {

this.removeAll();

this.init();

以上就是土嘎嘎小编为大家整理的JAVA华容道完整代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章