重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

华容道java代码讲解 数字华容道java代码

求用C语言代吗编写一个华容道的游戏出来。最好带每步的解释

package 华容道;

网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站制作、做网站收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10多年的创新互联建站网站建设公司。

import java.awt.*;

import java.awt.event.*;

//主函数

public class Main {

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("Start");//重新开始按钮

public Hua_Rong_Road()

{

init();

setBounds(100,100,320,360);

setVisible(true);//设置Frame为可见,默认为不可见

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[0].requestFocus();

left=new Button();

right=new Button();

above=new Button();

below=new Button();

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

}

}

java代码解析

一楼的说的够全面了,不过稍有误解.

再来表示抱歉,我对编程语言中的中文名词非常不了解,所以如果以下的回复对你的阅读或者理解造成困难,请见谅.

1.首先,要明白这个问题的答案,需要了解call (pass) by value 和 call (pass) by reference 的区别.简单来说:

call by value通常是复制这个parameter的值去另外一块内存里,然后传给function, 所以在method/function里边对这个变量的所有变更,实际上都是对复制过来的镜像进行操作,不会对原本的variable有任何影响.

call by reference是将parameter的reference传给function,简单点理解就是直接把variable传给function.所以说这个variable的值是可以被function改变的.这个用法在c/c++中非常常见,用法是variable_name.

2.再来,在Java里边,你可以很简单的理解为: Java中只有call by value, 也就是说,所以所有传给function的parameter本身都不会被改变. (这是最简单直白的理解,当然也有另一种常从sun的人那边听到的说法:Java是call by value + call by reference by value)

3.那么现在的问题就是为什么第二个结果是2了. 首先说一下sun官方的解释: 对于reference type在作为parameter/argument的时候,也是call by value, 但是在你拥有足够权限时(比方说那个变量是public的, 不是final的等等各种符合的情况),可以修改这个object中fields的值(也就是属于这个object(严谨点讲是an instance of the object) 内部的变量, 在你的例子中, ko 里边的 a 就是一个field, 所以update(ko)会使ko.a变成2).

4.如果你是一个有过c/c++学习经验的人或者你以上的解释很难理解,以下这种说法或许更适合你 (当然了,这只是大多包括我在内有c经验的人的一种理解方式)

这里可以引入一个新的概念,pointer. 这是一种比较特殊的变量,它内部所储存的东西,其实只是另外一个变量的内存地址. 如果对内存没有概念,你可以把它简单理解为是风筝的线轴,虽然看它本身看不出什么端倪,但是顺着摸过去总会找到风筝,看到它是什么样子. 以pointer方式理解Java的人,通常会说: Type variable = new Type(); 这个过程中,最后生成的这个variable其实就是一个pointer,而不是instance本身.

在Java中, 有c/c++经验的人通常认为Java是call by value.同时,当一个变量用在储存reference type的时候,实际上储存的是它的pointer,这也一样可以解释为什么ko.a会有2这个结果,因为虽然pointer被传到function里边时,本身是call by value,无法被改变.但这并不影响function本身对这个pointer指向的object的内容做任何改变. 当然,再次声明,这只是一种帮助有c/c++经验的人理解的方法. Sun本身严正声明Java里边没有pointer这个东西的存在.

5. 再来解释一下为什么说楼上所说的(或者说楼上引用的)理解略有偏差.

引用"我们上面刚学习了JAVA的数据类型,则有:值类型就是按值传递的,而引用类型是按引用传递的" 这句话很明显的有两点错误. 第一点,如果我上面所说的,Java是没有call by reference的.

第二点,暂且假设Java里边是有call by reference的, 这句话依然不成立.

Java中的变量有两种类型: primitive types 和 reference type.

primitive type包括byte, short, int, long, char, boolean, float和double.

而这8种之外的所有的,都是reference type.

下面是一段对你的贴上来的code的一点延伸,希望可以帮助你更好的理解Java中的argument / parameter到底是如何运作的.

public class Test {

public static void main(String[] args) {

int a = 1;

Koo koo = new Koo();

Object o = new Integer(1);

Koo newKoo = new Koo();

update(a);

update(koo);

update(o);

update(newKoo);

newUpdate(newKoo);

System.out.println(a);

System.out.println(koo.a);

System.out.println(o);

System.out.println(newKoo.a);

}

static void update(int a) {

a++;

}

static void update(Koo koo) {

koo.a++;

}

static void update(Object o) {

o = (int) (Integer.parseInt(o.toString()) + 1);

}

static void newUpdate(Koo koo) {

koo = new Koo();

}

}

class Koo {

int a = 1;

}

/*

o = (int) (Integer.parseInt(o.toString()) + 1); 这一行中的(int)纯粹是多余的,是否有这个casting对code本身没有任何影响. 如果你高兴也可以用

o = new Integer(Integer.parseInt(o.toString()) + 1);

或者干脆

o = Integer.parseInt(o.toString()) + 1;

*/

以上这些code运行之后会得到1 2 1 2的结果. 后面两个结果可以很好的说明, 即使对objects (reference type variables) 来看, Java所应用的也并不是call by reference. 否则的话,以上code运行结果应该是1 2 2 1

希望你可以真正理解这个新的例子中,产生1212这个结果的原因,从而对Java中的arguments有一个系统全面的认识.

图片是相关资料的链接,知道里貌似不能加网址

用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();

}

}

1200分跪求JAVA数字拼图游戏源代码!

1:

import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

// 华容道原理的拼图游戏。 利用轻组建的套用。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Choice;

import java.awt.Color;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyMainFrame extends JFrame implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

MyCanvas myCanvas;

JPanel panelNorth,panelPreview;

Button start,preview,set;

Container container;

public MyMainFrame() {//初使化

container=this.getContentPane();

start=new Button("开始");

start.addActionListener(this);

preview=new Button("预览");

preview.addActionListener(this);

set = new Button("设置");

set.addActionListener(this);

panelPreview=new JPanel();

panelPreview.setLayout(null);

Icon icon=new ImageIcon ("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.add(label);

panelNorth=new JPanel();

panelNorth.setBackground(Color.yellow);

panelNorth.add(start);

panelNorth.add(preview);

panelNorth.add(set);

myCanvas=new MyCanvas();

container.add(myCanvas,BorderLayout.CENTER);

container.add(panelNorth,BorderLayout.NORTH);

this.setTitle("成型拼图小游戏-1212");

this.setLocation(300,200);

this.setSize(408,465);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(3);

} //end of 初始化 构造函数

public void actionPerformed(ActionEvent e) {

Button button=(Button)e.getSource();

if(button==start){

myCanvas.Start();

}else if(button==preview){

if(button.getLabel()=="预览"){

container.remove(myCanvas);

container.add(panelPreview);

panelPreview.updateUI();

container.repaint();

button.setLabel("返回");

}else{

container.remove(panelPreview);

container.add(myCanvas);

container.repaint();

button.setLabel("预览");

}

}else if(button==set){

Choice pic = new Choice();

//pic.add("QQ");

pic.add("美女");

int i=JOptionPane.showConfirmDialog(this,pic,"选择图片", JOptionPane.OK_CANCEL_OPTION);

//使用选择对话框来进行选择图片。

if(i==JOptionPane.YES_OPTION){

MyCanvas.pictureID=pic.getSelectedIndex()+5;

myCanvas.reLoadPictrue();

Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.removeAll();

panelPreview.add(label);

panelPreview.repaint();

}

}

}

public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException

{

new MyMainFrame();

} //end of main

}

2:

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyCanvas extends JPanel implements MouseListener

{

/**

*

*/

private static final long serialVersionUID = 1L;

boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件

Cell cell[];//定义方格

Rectangle cellNull;//定义空方格区域 是一个矩形类

public static int pictureID=4;// 当前选择的图片代号

public MyCanvas() {

this.setLayout(null);

this.setSize(400,400);

cellNull=new Rectangle(300,300,100,100);//空方格区域在第三行每三列

cell=new Cell[16];

Icon icon;

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

for(int j=0;j4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j]=new Cell(icon);

cell[i*4+j].setLocation(j*100,i*100);

this.add(cell[i*4+j]);

}

}

this.remove(cell[15]);//移除最后一个多余的方格

} //放置9张小图片并且移调最后一张

public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片

Icon icon;

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

for(int j=0;j4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j].setIcon(icon);

}

}

}

public boolean isFinish(){//判断是否拼合成功

for(int i=0;i15;i++)

{ int x=cell[i].getBounds().x;

int y=cell[i].getBounds().y;

if(y/100*4+x/100!=i)

return false;

} //end of for

return true;

}

public void Start(){//对方格进行重新排列,打乱顺序

while(cell[0].getBounds().x=100cell[0].getBounds().y=100){//当第一个方格距左上角较近时

int x=cellNull.getBounds().x;

int y=cellNull.getBounds().y;

int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动

if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动

x-=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){//依次寻找左侧的按钮

cell[j].move("RIGHT",100);

cellNull.setLocation(x,y);

break;//找到后跳出for循环

}

}

}

}else if(direction==1){//RIGHT

x+=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("LEFT",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else if(direction==2){//UP

y-=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("DOWN",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else{//DOWN

y+=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("UP",100);

cellNull.setLocation(x,y);

break;

}

}

}

}

}

if(!hasAddActionListener)//如果尚未添加动作事件,则添加

for(int i=0;i15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了

cell[i].addMouseListener(this);

hasAddActionListener=true;

}

private boolean test(int x,int y){

if((x=0x=200)||(y=0y=200))

return true;

else

return false;

}

public void mouseClicked(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mousePressed(MouseEvent e) {

//方格的鼠标事件,因为用到了MyCanvas中的一些方法,因此没有在Cell类中处理鼠标事件

Cell button=(Cell)e.getSource();

int x1=button.getBounds().x;//得到所单击方格的坐标

int y1=button.getBounds().y;

int x2=cellNull.getBounds().x;//得到空方格的坐标

int y2=cellNull.getBounds().y;

if(x1==x2y1-y2==100)//进行比较,如果满足条件则进行交换

button.move("UP",100);

else if(x1==x2y1-y2==-100)

button.move("DOWN",100);

else if(x1-x2==100y1==y2)

button.move("LEFT",100);

else if(x1-x2==-100y1==y2)

button.move("RIGHT",100);

else

return;//不满足就不进行任何处理

cellNull.setLocation(x1,y1);

this.repaint();

if(this.isFinish()){//进行是否完成的判断

JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");

for(int i=0;i15;i++)

cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用

hasAddActionListener=false;

}

}

}

3:

import javax.swing.Icon;

import javax.swing.JButton;

public class Cell extends JButton {

/**

*

*/

private static final long serialVersionUID = 1L;

Cell(Icon icon){//实际为ICON

super(icon);

this.setSize(100,100);

}

public void move(String direction,int sleep){//方格的移动

if(direction=="UP"){

this.setLocation(this.getBounds().x,this.getBounds().y-100);

}else if(direction=="DOWN"){

this.setLocation(this.getBounds().x,this.getBounds().y+100);

}else if(direction=="LEFT"){

this.setLocation(this.getBounds().x-100,this.getBounds().y);

}else{

this.setLocation(this.getBounds().x+100,this.getBounds().y);

}

}

}


当前文章:华容道java代码讲解 数字华容道java代码
URL分享:http://cqcxhl.com/article/hgsphi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP