快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

华容道java源代码 华容道编程

200求JAVA课程设计报告 关于手机华容道的

这个我试了的没有任务问题,稀望对你有点帮助,记得类名要改为Hua_Rong_Road ,因为只有Hua_Rong_Road 这个类是公开的.另外包名也改下package xxxx(你自己建的包名),玩游戏时移动人物,用键盘(上下左右 ,--,--,上,下)操作,鼠标是不能移动 人物的,照着我说的做,应该是没什么问题的:

创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为姚安企业提供专业的成都网站制作、成都做网站,姚安网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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开发小游戏

我给你个华容道的游戏参照下吧。可以自己照着做下。多看例子想想就行了。

找个游戏做的不好,不要笑话啊。

import java.awt.*;

import javax.swing.JApplet.*;

import java.awt.event.*;

import javax.swing.*;

class People extends JButton implements FocusListener

{

Rectangle rect=null;

int left_x,left_y;//按钮左上角坐标.

int width,height; //按钮的宽和高.

String name;

int number;

public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)

{

super(s);

name=s;

this.number=number;

left_x=x;

left_y=y;

width=w;

height=h;

setBackground(Color.GREEN);

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.GREEN);

}

}

public class HuaRongRoad extends JApplet implements KeyListener,ActionListener

{

People people[]=new People[10];

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

JButton restart=new JButton("restart");

public void init()

{

getContentPane().setLayout(null);

getContentPane().add(restart);

restart.setBounds(5,5,80,25);

restart.addActionListener(this);

getContentPane().setBackground(Color.white);

people[0]=new People(0,"曹操",154,54,200,200,this);

people[1]=new People(1,"关羽",154,254,200,100,this);

people[2]=new People(2,"张飞",54,254,100,200,this);

people[3]=new People(3,"刘备",354,254,100,200,this);

people[4]=new People(4,"张辽",54,54,100,200,this);

people[5]=new People(5,"曹仁",354,54,100,200,this);

people[6]=new People(6,"兵 ",54,454,100,100,this);

people[7]=new People(7,"兵 ",354,454,100,100,this);

people[8]=new People(8,"兵 ",154,354,100,100,this);

people[9]=new People(9,"兵 ",254,354,100,100,this);

people[9].requestFocus();

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

left=new Rectangle(49,49,5,510);

right=new Rectangle(454,49,5,510);

above=new Rectangle(49,49,410,5);

below=new Rectangle(49,554,410,5);

}

public void paint(Graphics g)

{ //华容道的边界

super.paint(g);

g.setColor(Color.cyan);

g.fillRect(49,49,5,510);

g.fillRect(454,49,5,510);

g.fillRect(49,49,410,5);

g.fillRect(49,554,410,5);

//

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+100; //向下前进50个单位

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

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

//判断是否和其他人或边界重叠,出现就退回50个单位

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

{

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

{

man.left_y=man.left_y-100;

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-100;

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-100; //向上前进50个单位

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

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

//判断是否和其他人或边界重叠,出现就退回50个单位

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

{

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

{

man.left_y=man.left_y+100;

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+100;

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-100; //向左前进50个单位

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

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

//判断是否和其他人或边界重叠,出现就退回50个单位

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

{

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

{

man.left_x=man.left_x+100;

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+100;

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+100; //向右进50个单位

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

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

//判断是否和其他人或边界重叠,出现就退回50个单位

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

{

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

{

man.left_x=man.left_x-100;

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-100;

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)

{

getContentPane().removeAll();

this.init();

}

}

用Eclipse怎么写华容道问题

package huaroad;

import java.util.*;

import com.siemens.mp.io.*;

import com.siemens.mp.game.*;

import java.io.IOException;

import javax.microedition.lcdui.*;

// 游戏类

public class HRGame extends Canvas implements CommandListener, Backable

{

private static int BLOCKSIZE = 0; // 每块大小

private static int IMGOFFSET = 0; // 名字图片对于边框的偏移量

// 布局定义 (4*5)块

// 0-空, 1~4-兵, 5-张飞, 6-赵云, 7-马超, 8-黄忠, 9-关羽, 10-曹操

private static final byte gamelayout[][][] = {

{ // 横刀立马

{ 5,10,10, 6 },

{ 5,10,10, 6 },

{ 7, 9, 9, 8 },

{ 7, 2, 3, 8 },

{ 1, 0, 0, 4 },

},

{ // 过五关

{ 1,10,10, 3 },

{ 2,10,10, 4 },

{ 5, 5, 6, 6 },

{ 7, 7, 8, 8 },

{ 0, 9, 9, 0 },

},

{ // 水泄不通

{ 1,10,10, 5 },

{ 2,10,10, 5 },

{ 6, 6, 7, 7 },

{ 8, 8, 9, 9 },

{ 3, 0, 0, 4 },

},

{ // 小燕出巢

{ 5,10,10, 6 },

{ 5,10,10, 6 },

{ 7, 7, 8, 8 },

{ 1, 9, 9, 3 },

{ 2, 0, 0, 4 },

},

{ // 进在咫尺

{ 1, 5, 6, 7 },

{ 2, 5, 6, 7 },

{ 8, 8, 3, 4 },

{ 9, 9,10,10 },

{ 0, 0,10,10 },

},

{ // 左右夹击

{ 1,10,10, 2 },

{ 5,10,10, 6 },

{ 5, 9, 9, 6 },

{ 7, 3, 4, 8 },

{ 7, 0, 0, 8 },

},

{ // 重兵挡道

{ 5,10,10, 6 },

{ 5,10,10, 6 },

{ 1, 2, 3, 4 },

{ 7, 9, 9, 8 },

{ 7, 0, 0, 8 },

}

};

private static final byte posSxy[][] = { // 各布局光标起始位置(x,y)

{ 1, 4 }, { 0, 4 }, { 1, 4 }, { 1, 4 }, { 0, 4 },

{ 1, 4 }, { 1, 4 }

};

private static final Command COMMANDS[] = { // 定义菜单

new Command("继续游戏", 4, 0),

new Command("新游戏", 1, 1),

new Command("退出", 6, 2),

new Command("按键设置", 1, 3),

new Command("帮助信息", 1, 4)

};

public byte ctrlkey[] = { // 按键定义(初始按键)

'4', // left - 4

'6', // right - 6

'2', // up - 2

'8', // down - 8

'5', // select - 5

};

private Command SndCmd, VibCmd;

final private static String openSnd="开启声音", closeSnd="关闭声音", openVib="开启振动", closeVib="关闭振动";

public boolean Snd, Vib;

private byte grid[][] = new byte[4][5]; // 游戏布局矩阵

private int posx, posy; // 当前光标位置

private int oldx, oldy; // 上次光标位置

private int offx, offy; // 棋子的大小偏移量(相对于当前块)。

// 比如当前块是曹操的右上角,那么offx=-1,offy=1

// 表示x负方向还有一块,y的正方向有一块

private int steps, tmpstep; // 已走步数

private Image nameImg[]; // 名字图片(由于可写文字太大,所以用图片)

private SelNew gamelist;

private Display display;

private Quitable winQuit;

private boolean selected, gameOver; // 是否选中/是否游戏结束

private ExtendedImage ExScreenImg; // 扩展图片作为绘图缓存(Siemens扩展)

private Graphics Exg; // 缓存的Graphics对象

private MelodyComposer melody; // midi播放(Siemens扩展)

public HRGame()

{

if (getHeight() 64) // 根据屏幕高度得到图形尺寸,以便针对不同机型

{

// 6688i使用

BLOCKSIZE = 15; // 每块大小

IMGOFFSET = 3; // 名字图片对于边框的偏移量

}

else

{

// 3118使用

BLOCKSIZE = 12;

IMGOFFSET = 1;

}

nameImg = new Image[26]; // 载入名字图片

try

{

nameImg[0] = Image.createImage("images/zhang.png"); // 张

nameImg[1] = Image.createImage("images/zhang2.png"); // 张(反色)

nameImg[2] = Image.createImage("images/fei.png"); // 飞

nameImg[3] = Image.createImage("images/fei2.png"); // 飞(反色)

nameImg[4] = Image.createImage("images/zhao.png"); // 赵

nameImg[5] = Image.createImage("images/zhao2.png"); // 赵(反色)

nameImg[6] = Image.createImage("images/yun.png"); // 云

nameImg[7] = Image.createImage("images/yun2.png"); // 云(反色)

nameImg[8] = Image.createImage("images/ma.png"); // 马

nameImg[9] = Image.createImage("images/ma2.png"); // 马(反色)

nameImg[10] = Image.createImage("images/chao.png"); // 超

nameImg[11] = Image.createImage("images/chao2.png"); // 超(反色)

nameImg[12] = Image.createImage("images/huang.png"); // 黄

nameImg[13] = Image.createImage("images/huang2.png"); // 黄(反色)

nameImg[14] = Image.createImage("images/zhong.png"); // 忠

nameImg[15] = Image.createImage("images/zhong2.png"); // 忠(反色)

nameImg[16] = Image.createImage("images/guan.png"); // 关

nameImg[17] = Image.createImage("images/guan2.png"); // 关(反色)

nameImg[18] = Image.createImage("images/yu.png"); // 羽

nameImg[19] = Image.createImage("images/yu2.png"); // 羽(反色)

nameImg[20] = Image.createImage("images/cao.png"); // 曹

nameImg[21] = Image.createImage("images/cao2.png"); // 曹(反色)

nameImg[22] = Image.createImage("images/c.png"); // 操

nameImg[23] = Image.createImage("images/c2.png"); // 操(反色)

nameImg[24] = Image.createImage("images/bing.png"); // 兵

nameImg[25] = Image.createImage("images/bing2.png"); // 兵(反色)

}

catch(IOException ioexception) { }

ExScreenImg = new ExtendedImage(Image.createImage(104, getHeight())); // 新建绘图缓存

Exg = ExScreenImg.getImage().getGraphics(); // 缓存绘图的Graphics对象

melody = new MelodyComposer(); // 新建midi

Snd = true; // 音效开启

Vib = true; // 震动开启

/////////////////////////////////////////////

// 读取按键设置 共7字节,依次表示:左按键、右按键、上按键、下按键、选中按键、音效、震动

try

{

byte bflag[] = new byte[2];

bflag[0] = bflag[1] = 1;

File keyfile = new File();

int fid = keyfile.open("CtrlKey"); // 打开文件(Siemens扩展)

if(keyfile.length(fid) 0) keyfile.read(fid, ctrlkey, 0, 5); // 读取按键参数到ctrlkey

if(keyfile.length(fid) 5) keyfile.read(fid, bflag, 0, 2); // 读取文件

Snd = (bflag[0] == 1); // 音效参数 (1-开启,其他-关闭)

Vib = (bflag[1] == 1); // 震动参数 (1-开启,其他-关闭)

keyfile.close(fid);

}

catch(IOException ioexception) { }

catch(NullPointerException npe){ }

//////////////////////////////////////////////

if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5); // 根据参数设置菜单

else SndCmd = new Command(openSnd, Command.OK, 5);

if (Vib) VibCmd = new Command(closeVib, Command.OK, 6);

else VibCmd = new Command(openVib, Command.OK, 6);

for(int i = 0; i COMMANDS.length; i++) // 添加菜单

addCommand(COMMANDS[i]);

addCommand(SndCmd);

addCommand(VibCmd);

}

public void activate(Display disp, Quitable quitable) // 激活

{

winQuit = quitable;

display = disp;

show();

}

public void getkeys(byte ckeys[]) // 从自定义按键结果设置按键,并显示游戏界面

{

for (byte i=0; i5; i++) ctrlkey[i] = ckeys[i];

show();

}

public void show() // 显示游戏画面

{

display.setCurrent(this);

setCommandListener(this);

}

public void newGame(SelNew gmlist) // 新游戏

{

gamelist = gmlist;

int gnum = gamelist.getSelectedIndex(); // 获取游戏布局编号

int i, j;

for (i=0; i4; i++) // 根据编号初始化游戏矩阵

for (j=0; j5; j++)

grid[i][j] = gamelayout[gnum][j][i];

posx = posSxy[gnum][0]; // 初始化当前光标位置

posy = posSxy[gnum][1];

offx = offy = 0;

steps = 0; // 已走步数为0

gameOver = false;

selected = false; // 没有选中某一块

ExScreenImg.clear((byte)0); // 清空缓存

Exg.drawRect(4, 0, 2+BLOCKSIZE*4, 2+BLOCKSIZE*5); // 画边框

Exg.drawRect(4+BLOCKSIZE, 2+BLOCKSIZE*5, 2+BLOCKSIZE*2, 2);

// 画布局名称

String gname,gname2;

switch(gamelist.getSelectedIndex())

{

case 0:

gname = "横刀"; gname2 = "立马"; break;

case 1:

gname = "过"; gname2 = "五关"; break;

case 2:

gname = "水泄"; gname2 = "不通"; break;

case 3:

gname = "小燕"; gname2 = "出巢"; break;

case 4:

gname = "进在"; gname2 = "咫尺"; break;

case 5:

gname = "左右"; gname2 = "夹击"; break;

default:

gname = "重兵"; gname2 = "挡道"; break;

}

Exg.drawString(gname, 70, 0, Graphics.TOP|Graphics.LEFT);

Exg.drawString(gname2, 70, 13, Graphics.TOP|Graphics.LEFT);

// 画步数

Exg.drawString("步数", 70, 34, Graphics.TOP|Graphics.LEFT);

Exg.setColor(0xffffff);

Exg.fillRect(68, 55, 33, 13);

Exg.setColor(0);

Exg.drawString(Integer.toString(steps), 100, 47, Graphics.TOP|Graphics.RIGHT);

// 画棋子

byte layout[] = {0,0,0,0,0, 0,0,0,0,0}; // 分别表示 10个棋子是否已经处理过,依次为:

// 1~4-兵, 5-张飞, 6-赵云, 7-马超, 8-黄忠, 9-关羽, 10-曹操

for(i=0; i4; i++)

for(j=0; j5; j++)

{

if (grid[i][j] != 0)

if (layout[grid[i][j]-1] == 0) // 如果这个棋子没有画过

{

moveSelbox(i, j, false); // 画一个棋子

layout[grid[i][j]-1] = 1; // 已经画过了,标志置1

}

}

drawSelbox(); // 画选择框

SoundPlay(0); // 播放游戏开始音效

}

protected void paint(Graphics parag)

{

redraw();

}

// 画选择框

private void drawSelbox()

{

if (selected) // 选中状态

{

drawBox(posx, posy, offx, offy, 0xffffff, false, 1, 0);

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

for (int j=0; j5; j++)

{

if (grid[i][j] == 0) drawBox(i, j, 0, 0, 0xffffff, true, 1, 0);

}

drawBox(posx, posy, offx, offy, 0, true, 0, 2);

}

else // 候选状态

{

if (grid[posx][posy] != 0) // 当前块不是空的

{

drawBox(posx, posy, offx, offy, 0xffffff, true, 0, 1);

drawBox(posx, posy, offx, offy, 0, false, 0, 0);

}

drawBox(posx, posy, offx, offy, 0, false, 2, 0); //画外框

}

}

// 移动选择框

// i,j当前块的(x,y)坐标, moving:true-移动到该棋子,false-重画该棋子

private void moveSelbox(int i, int j, boolean moving)

{

int ox = 0;

int oy = 0;

if (grid[i][j] != 0) // 该块不是空的

{

if (i 0) // 左侧

if (grid[i][j] == grid[i-1][j]) ox = -1;// 左侧属于同一个棋子

if (i 3) // 右侧

if (grid[i][j] == grid[i+1][j]) ox = 1; // 右侧属于同一个棋子

if (j 0) // 上面

if (grid[i][j] == grid[i][j-1]) oy = -1;// 上面属于同一个棋子

if (j 4) // 下面

if (grid[i][j] == grid[i][j+1]) oy = 1;// 下面属于同一个棋子

}

if (moving)

{

offx = ox;

offy = oy;

drawSelbox(); // 移动选择框

}

else

drawBox(i, j, ox, oy, 0, false, 0, 1); // 画棋子

}

// 向缓存画棋子或外框:(px,py)焦点格坐标,(ox,oy)整块相对焦点的偏移量,

// color颜色, fill是否填充黑色,bigbox是否大块(分3级),img是否画人名:0-不画、1-正常、2-反色

private void drawBox(int px, int py, int ox, int oy, int color, boolean fill, int bigbox, int img)

{

int gx[] = new int[2];

int gy[] = new int[2];

boolean dir = (ox==0);

if (bigbox != 0)

{

gx[0] = gx[1] = 5;

gy[0] = gy[1] = 1;

}

else

{

gx[0] = 6;

gx[1] = 4;

gy[0] = 2;

gy[1] = 0;

}

if (ox 0)

{

gx[0] += (px+ox)*BLOCKSIZE;

gx[1] += (px+1)*BLOCKSIZE;

}

else

{

gx[0] += px*BLOCKSIZE;

gx[1] += (px+ox+1)*BLOCKSIZE;

}

if (oy 0)

{

gy[0] += (py+oy)*BLOCKSIZE;

gy[1] += (py+1)*BLOCKSIZE;

}

else

{

gy[0] += py*BLOCKSIZE;

gy[1] += (py+oy+1)*BLOCKSIZE;

}

Exg.setColor(color);

if (fill)

Exg.fillRect(gx[0], gy[0], gx[1]-gx[0]+1, gy[1]-gy[0]+1);

else

Exg.drawRect(gx[0], gy[0], gx[1]-gx[0], gy[1]-gy[0]);

if (bigbox == 2)

{

Exg.drawLine(gx[0]+1, gy[0]-1, gx[1]-1, gy[0]-1);

Exg.drawLine(gx[0]+1, gy[1]+1, gx[1]-1, gy[1]+1);

Exg.drawLine(gx[0]-1, gy[0]+1, gx[0]-1, gy[1]-1);

Exg.drawLine(gx[1]+1, gy[0]+1, gx[1]+1, gy[1]-1);

}

else if (bigbox == 3)

{

if (ox != 0)

{

if (py 0 grid[px][py-1]!=grid[px+ox][py-1])

Exg.drawLine((gx[0]+gx[1])/2, gy[0]-1, (gx[0]+gx[1])/2, gy[0]-1);

if (py 4 grid[px][py+1]!=grid[px+ox][py+1])

Exg.drawLine((gx[0]+gx[1])/2, gy[1]+1, (gx[0]+gx[1])/2, gy[1]+1);

}

if (oy != 0)

{

if (px 0 grid[px-1][py]!=grid[px-1][py+oy])

Exg.drawLine(gx[0]-1, (gy[0]+gy[1])/2, gx[0]-1, (gy[0]+gy[1])/2);

if (px 3 grid[px+1][py]!=grid[px+1][py+oy])

Exg.drawLine(gx[1]+1, (gy[0]+gy[1])/2, gx[1]+1, (gy[0]+gy[1])/2);

}

}

Exg.setColor(0);

if (img0)

{

if (grid[px][py] == 10)

{

Exg.drawImage(nameImg[20+img-1], gx[0]+BLOCKSIZE/2+IMGOFFSET, gy[0]+1+IMGOFFSET, 20);

Exg.drawImage(nameImg[22+img-1], gx[0]+BLOCKSIZE/2+IMGOFFSET, gy[0]+BLOCKSIZE, 20);

}

else if (grid[px][py] 5)

Exg.drawImage(nameImg[24+img-1], gx[0]+IMGOFFSET, gy[0]+IMGOFFSET, 20);

else if(dir)

{

Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1], gx[0]+IMGOFFSET, gy[0]+1+IMGOFFSET, 20);

Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1+2], gx[0]+IMGOFFSET, gy[0]+BLOCKSIZE, 20);

}

else

{

Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1], gx[0]+1+IMGOFFSET, gy[0]+IMGOFFSET, 20);

Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1+2], gx[0]+BLOCKSIZE, gy[0]+IMGOFFSET, 20);

}

}

}

protected void keyPressed(int kcode) // 按键响应

{

if (gameOver) return; // 游戏结束了,不响应

if (selected) // 处于选中状态

{

if (kcode == ctrlkey[4]) // 选择

{

selected = false; // 不选中

drawSelbox();

}

else

{

int tmpx, tmpy;

tmpx = posx;

tmpy = posy;

if (kcode == ctrlkey[0]) // 左移

{

if (offx 0) tmpx += offx;

tmpx --;

if (tmpx 0) tmpx = posx; // 靠边,移不动

else if (grid[tmpx][posy] == 0 grid[tmpx][posy+offy] == 0)

tmpx = posx-1;

else tmpx = posx;

}

else if (kcode == ctrlkey[1]) // 右移

{

if (offx 0) tmpx += offx;

tmpx ++;

if (tmpx 3) tmpx = posx; // 靠边,移不动

else if (grid[tmpx][posy] == 0 grid[tmpx][posy+offy] == 0)

tmpx = posx+1;

else tmpx = posx;

}

else if (kcode == ctrlkey[2]) // move up

{

if (offy 0) tmpy += offy;

tmpy --;

if (tmpy 0) tmpy = posy; // 靠顶,移不动

else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)

tmpy = posy-1;

else tmpy = posy;

}

else if (kcode == ctrlkey[3]) // move down

{

if (offy 0) tmpy += offy;

tmpy ++;

if (tmpy 4) tmpy = posy; // 靠底,移不动

else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)

tmpy = posy+1;

else tmpy = posy;

}

// 重画焦点块

if ( tmpx != posx || tmpy != posy)

{

byte oldbnum = grid[posx][posy];

grid[posx][posy] = grid[posx+offx][posy] = 0;

grid[posx][posy+offy] = grid[posx+offx][posy+offy] = 0;

drawBox(posx, posy, offx, offy, 0xffffff, true, 1, 0);

posx = tmpx;

posy = tmpy;

grid[posx][posy] = grid[posx+offx][posy] = oldbnum;

grid[posx][posy+offy] = grid[posx+offx][posy+offy] = oldbnum;

drawSelbox();

}

// 计算、画 步数

if (posx == oldx posy == oldy) // 如果等于上一步的位置,表示回退了一步

steps = tmpstep; // 步数返回上一次的步数

else if (steps == tmpstep)

steps ++; // 步数增加

Exg.setColor(0xffffff);

Exg.fillRect(68, 55, 33, 13);

Exg.setColor(0);

Exg.drawString(Integer.toString(steps), 100, 45, Graphics.TOP| Graphics.RIGHT);

// 判断曹操(10)的位置

if (grid[1][4] == 10 grid[2][4] == 10) // 胜利

{

gameOver = true;

SoundPlay(1);

if (Vib) Vibrator.triggerVibrator(100); // 震动100ms(Siemens扩展)

Exg.setColor(0xffffff);

Exg.fillRect(11, 28, 47, 18);

Exg.setColor(0);

Exg.drawRect(11, 28, 47, 18);

Exg.drawString("胜利了!", 14, 32, 20);

}

}

}

else

{

if (kcode == ctrlkey[4]) // select

{

if (grid[posx][posy] != 0)

{

selected = true; // 选中

tmpstep = steps;

oldx = posx;

oldy = posy;

drawSelbox();

}

}

else

{

int tmpx, tmpy;

tmpx = posx;

tmpy = posy;

if (kcode == ctrlkey[0]) // move left

{

tmpx --;

if (tmpx 0) tmpx = 0;

else if (grid[tmpx][posy]==grid[posx][posy] grid[posx][posy]!=0)

{

tmpx --;

if (tmpx 0) tmpx = posx;

}

}

else if (kcode == ctrlkey[1]) // move right

{

tmpx ++;

if (tmpx 3) tmpx = 3;

else if (grid[tmpx][posy]==grid[posx][posy] grid[posx][posy]!=0)

{

tmpx ++;

if (tmpx 3) tmpx = posx;

}

}

else if (kcode == ctrlkey[2]) // move up

{

tmpy --;

if (tmpy 0) tmpy = 0;

else if (grid[posx][tmpy]==grid[posx][posy] grid[posx][posy]!=0)

{

tmpy --;

if (tmpy 0) tmpy = posy;

}

}

else if (kcode == ctrlkey[3]) // move down

{

tmpy ++;

if (tmpy 4) tmpy = 4;

else if (grid[posx][tmpy]==grid[posx][posy] grid[posx][posy]!=0)

{

tmpy ++;

if (tmpy 4) tmpy = posy;

}

}

if ( tmpx != posx || tmpy != posy)

{

drawBox(posx, posy, offx, offy, 0xffffff, false, 3, 0);

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

for (int j=0; j5; j++)

{

if (grid[i][j] == 0) drawBox(i, j, 0, 0, 0xffffff, true, 1, 0);

}

posx = tmpx;

posy = tmpy;

moveSelbox(posx, posy, true);

}

}

}

redraw();

}

public void commandAction(Command command, Displayable displayable) // 菜单响应

{

boolean savepara = false;

if(command == COMMANDS[1]) // new game

{

gamelist.activate(display, winQuit);

}

else if(command == COMMANDS[2]) // exit

winQuit.quit();

else if(command == COMMANDS[3]) // 按键设置

{

SetKeys fskey = new SetKeys(ctrlkey);

fskey.activate(display, this);

}

else if(command == COMMANDS[4]) // about

{

HRAbout hrabout = new HRAbout();

hrabout.activate(display, this);

}

else if (command == SndCmd)

{

Snd = !Snd;

removeCommand(SndCmd);

if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5);

else SndCmd = new Command(openSnd, Command.OK, 5);

addCommand(SndCmd);

savepara = true;

}

else if (command == VibCmd)

{

Vib = !Vib;

removeCommand(VibCmd);

if (Vib) VibCmd = new Command(closeVib,Command.OK, 6);

else VibCmd = new Command(openVib,Command.OK, 6);

addCommand(VibCmd);

savepara = true;

}

if (savepara)

{

/////////////////////////////////////////////

// 写入参数

try

{

byte bflag[] = new byte[2];

File keyfile = new File();

int fid = keyfile.open("CtrlKey");

keyfile.write(fid, ctrlkey, 0, 5); // 写入按键数据(Siemens扩展)

bflag[0] = (byte)(Snd ? 1 : 0);

bflag[1] = (byte)(Vib ? 1 : 0);

keyfile.write(fid, bflag, 0, 2);

keyfile.close(fid);

}

catch(IOException ioexception) { }

catch(NullPointerException npe){}

//////////////////////////////////////////////*/

}

}

private void redraw()

{

ExScreenImg.blitToScreen(0, 0); // 缓存内数据直接输出到屏幕上

}

//音乐

private void SoundPlay(int n) // 播放音效

{

if (!Snd) return; // 音效关闭,则返回

// Siemens扩展

melody.resetMelody();

try

{

if (n == 0) // new game

{

melody.setBPM(100);

melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_H3, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_8);

}

else if (n == 1) // win

{

melody.setBPM(110);

melody.appendNote(MelodyComposer.TONE_C2, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);

melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);

以上为核心代码 由于代码太多无法一次传上来

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源代码 华容道编程
网站网址:http://6mz.cn/article/ddepesc.html

其他资讯