這個(gè)我試了的沒(méi)有任務(wù)問(wèn)題,稀望對(duì)你有點(diǎn)幫助,記得類(lèi)名要改為Hua_Rong_Road ,因?yàn)橹挥蠬ua_Rong_Road 這個(gè)類(lèi)是公開(kāi)的.另外包名也改下package xxxx(你自己建的包名),玩游戲時(shí)移動(dòng)人物,用鍵盤(pán)(上下左右 ,--,--,上,下)操作,鼠標(biāo)是不能移動(dòng) 人物的,照著我說(shuō)的做,應(yīng)該是沒(méi)什么問(wèn)題的:
創(chuàng)新互聯(lián)長(zhǎng)期為上1000家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為姚安企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站,姚安網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
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 // 代表華容道人物的類(lèi)。
{
Rectangle rect = null;
int left_x, left_y;// 按扭的左上角坐標(biāo).
int width, height; // 按扭的寬和高.
String name;
int number;
People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 構(gòu)造函數(shù)
{
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("重新開(kāi)始");
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);// 構(gòu)造曹操
people[1] = new People(1, "關(guān)羽", 104, 154, 100, 50, this);// 構(gòu)造關(guān)羽
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) {// 畫(huà)出華容道的邊界:
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.
// 提示曹操逃出位置和按鍵規(guī)則:
g.drawString("點(diǎn)擊相應(yīng)的人物,然后按鍵盤(pán)上的上下左右箭頭移動(dòng)", 100, 20);
g.setColor(Color.red);
g.drawString("曹操到達(dá)該位置", 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; // 向下前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或下邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
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; // 向上前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或上邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
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; // 向左前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或左邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
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; // 向右前進(jìn)50個(gè)單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或右邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個(gè)單位距離。
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();
}
}
我給你個(gè)華容道的游戲參照下吧??梢宰约赫罩鱿隆6嗫蠢酉胂刖托辛?。
找個(gè)游戲做的不好,不要笑話(huà)啊。
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;//按鈕左上角坐標(biāo).
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,"關(guān)羽",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("單擊,按方向箭頭移動(dòng)",100,20);
g.setColor(Color.red);
g.drawString("曹操到達(dá)該位置",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; //向下前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
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; //向上前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
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; //向左前進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
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; //向右進(jìn)50個(gè)單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現(xiàn)就退回50個(gè)單位
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();
}
}
package huaroad;
import java.util.*;
import com.siemens.mp.io.*;
import com.siemens.mp.game.*;
import java.io.IOException;
import javax.microedition.lcdui.*;
// 游戲類(lèi)
public class HRGame extends Canvas implements CommandListener, Backable
{
private static int BLOCKSIZE = 0; // 每塊大小
private static int IMGOFFSET = 0; // 名字圖片對(duì)于邊框的偏移量
// 布局定義 (4*5)塊
// 0-空, 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關(guān)羽, 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 },
},
{ // 過(guò)五關(guān)
{ 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 },
},
{ // 進(jìn)在咫尺
{ 1, 5, 6, 7 },
{ 2, 5, 6, 7 },
{ 8, 8, 3, 4 },
{ 9, 9,10,10 },
{ 0, 0,10,10 },
},
{ // 左右?jiàn)A擊
{ 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[][] = { // 各布局光標(biāo)起始位置(x,y)
{ 1, 4 }, { 0, 4 }, { 1, 4 }, { 1, 4 }, { 0, 4 },
{ 1, 4 }, { 1, 4 }
};
private static final Command COMMANDS[] = { // 定義菜單
new Command("繼續(xù)游戲", 4, 0),
new Command("新游戲", 1, 1),
new Command("退出", 6, 2),
new Command("按鍵設(shè)置", 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="開(kāi)啟聲音", closeSnd="關(guān)閉聲音", openVib="開(kāi)啟振動(dòng)", closeVib="關(guān)閉振動(dòng)";
public boolean Snd, Vib;
private byte grid[][] = new byte[4][5]; // 游戲布局矩陣
private int posx, posy; // 當(dāng)前光標(biāo)位置
private int oldx, oldy; // 上次光標(biāo)位置
private int offx, offy; // 棋子的大小偏移量(相對(duì)于當(dāng)前塊)。
// 比如當(dāng)前塊是曹操的右上角,那么offx=-1,offy=1
// 表示x負(fù)方向還有一塊,y的正方向有一塊
private int steps, tmpstep; // 已走步數(shù)
private Image nameImg[]; // 名字圖片(由于可寫(xiě)文字太大,所以用圖片)
private SelNew gamelist;
private Display display;
private Quitable winQuit;
private boolean selected, gameOver; // 是否選中/是否游戲結(jié)束
private ExtendedImage ExScreenImg; // 擴(kuò)展圖片作為繪圖緩存(Siemens擴(kuò)展)
private Graphics Exg; // 緩存的Graphics對(duì)象
private MelodyComposer melody; // midi播放(Siemens擴(kuò)展)
public HRGame()
{
if (getHeight() 64) // 根據(jù)屏幕高度得到圖形尺寸,以便針對(duì)不同機(jī)型
{
// 6688i使用
BLOCKSIZE = 15; // 每塊大小
IMGOFFSET = 3; // 名字圖片對(duì)于邊框的偏移量
}
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"); // 關(guān)
nameImg[17] = Image.createImage("images/guan2.png"); // 關(guān)(反色)
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對(duì)象
melody = new MelodyComposer(); // 新建midi
Snd = true; // 音效開(kāi)啟
Vib = true; // 震動(dòng)開(kāi)啟
/////////////////////////////////////////////
// 讀取按鍵設(shè)置 共7字節(jié),依次表示:左按鍵、右按鍵、上按鍵、下按鍵、選中按鍵、音效、震動(dòng)
try
{
byte bflag[] = new byte[2];
bflag[0] = bflag[1] = 1;
File keyfile = new File();
int fid = keyfile.open("CtrlKey"); // 打開(kāi)文件(Siemens擴(kuò)展)
if(keyfile.length(fid) 0) keyfile.read(fid, ctrlkey, 0, 5); // 讀取按鍵參數(shù)到ctrlkey
if(keyfile.length(fid) 5) keyfile.read(fid, bflag, 0, 2); // 讀取文件
Snd = (bflag[0] == 1); // 音效參數(shù) (1-開(kāi)啟,其他-關(guān)閉)
Vib = (bflag[1] == 1); // 震動(dòng)參數(shù) (1-開(kāi)啟,其他-關(guān)閉)
keyfile.close(fid);
}
catch(IOException ioexception) { }
catch(NullPointerException npe){ }
//////////////////////////////////////////////
if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5); // 根據(jù)參數(shù)設(shè)置菜單
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[]) // 從自定義按鍵結(jié)果設(shè)置按鍵,并顯示游戲界面
{
for (byte i=0; i5; i++) ctrlkey[i] = ckeys[i];
show();
}
public void show() // 顯示游戲畫(huà)面
{
display.setCurrent(this);
setCommandListener(this);
}
public void newGame(SelNew gmlist) // 新游戲
{
gamelist = gmlist;
int gnum = gamelist.getSelectedIndex(); // 獲取游戲布局編號(hào)
int i, j;
for (i=0; i4; i++) // 根據(jù)編號(hào)初始化游戲矩陣
for (j=0; j5; j++)
grid[i][j] = gamelayout[gnum][j][i];
posx = posSxy[gnum][0]; // 初始化當(dāng)前光標(biāo)位置
posy = posSxy[gnum][1];
offx = offy = 0;
steps = 0; // 已走步數(shù)為0
gameOver = false;
selected = false; // 沒(méi)有選中某一塊
ExScreenImg.clear((byte)0); // 清空緩存
Exg.drawRect(4, 0, 2+BLOCKSIZE*4, 2+BLOCKSIZE*5); // 畫(huà)邊框
Exg.drawRect(4+BLOCKSIZE, 2+BLOCKSIZE*5, 2+BLOCKSIZE*2, 2);
// 畫(huà)布局名稱(chēng)
String gname,gname2;
switch(gamelist.getSelectedIndex())
{
case 0:
gname = "橫刀"; gname2 = "立馬"; break;
case 1:
gname = "過(guò)"; gname2 = "五關(guān)"; break;
case 2:
gname = "水泄"; gname2 = "不通"; break;
case 3:
gname = "小燕"; gname2 = "出巢"; break;
case 4:
gname = "進(jìn)在"; 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);
// 畫(huà)步數(shù)
Exg.drawString("步數(shù)", 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);
// 畫(huà)棋子
byte layout[] = {0,0,0,0,0, 0,0,0,0,0}; // 分別表示 10個(gè)棋子是否已經(jīng)處理過(guò),依次為:
// 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關(guān)羽, 10-曹操
for(i=0; i4; i++)
for(j=0; j5; j++)
{
if (grid[i][j] != 0)
if (layout[grid[i][j]-1] == 0) // 如果這個(gè)棋子沒(méi)有畫(huà)過(guò)
{
moveSelbox(i, j, false); // 畫(huà)一個(gè)棋子
layout[grid[i][j]-1] = 1; // 已經(jīng)畫(huà)過(guò)了,標(biāo)志置1
}
}
drawSelbox(); // 畫(huà)選擇框
SoundPlay(0); // 播放游戲開(kāi)始音效
}
protected void paint(Graphics parag)
{
redraw();
}
// 畫(huà)選擇框
private void drawSelbox()
{
if (selected) // 選中狀態(tài)
{
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 // 候選狀態(tài)
{
if (grid[posx][posy] != 0) // 當(dāng)前塊不是空的
{
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); //畫(huà)外框
}
}
// 移動(dòng)選擇框
// i,j當(dāng)前塊的(x,y)坐標(biāo), moving:true-移動(dòng)到該棋子,false-重畫(huà)該棋子
private void moveSelbox(int i, int j, boolean moving)
{
int ox = 0;
int oy = 0;
if (grid[i][j] != 0) // 該塊不是空的
{
if (i 0) // 左側(cè)
if (grid[i][j] == grid[i-1][j]) ox = -1;// 左側(cè)屬于同一個(gè)棋子
if (i 3) // 右側(cè)
if (grid[i][j] == grid[i+1][j]) ox = 1; // 右側(cè)屬于同一個(gè)棋子
if (j 0) // 上面
if (grid[i][j] == grid[i][j-1]) oy = -1;// 上面屬于同一個(gè)棋子
if (j 4) // 下面
if (grid[i][j] == grid[i][j+1]) oy = 1;// 下面屬于同一個(gè)棋子
}
if (moving)
{
offx = ox;
offy = oy;
drawSelbox(); // 移動(dòng)選擇框
}
else
drawBox(i, j, ox, oy, 0, false, 0, 1); // 畫(huà)棋子
}
// 向緩存畫(huà)棋子或外框:(px,py)焦點(diǎn)格坐標(biāo),(ox,oy)整塊相對(duì)焦點(diǎn)的偏移量,
// color顏色, fill是否填充黑色,bigbox是否大塊(分3級(jí)),img是否畫(huà)人名:0-不畫(huà)、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) // 按鍵響應(yīng)
{
if (gameOver) return; // 游戲結(jié)束了,不響應(yīng)
if (selected) // 處于選中狀態(tài)
{
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; // 靠邊,移不動(dòng)
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; // 靠邊,移不動(dòng)
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; // 靠頂,移不動(dòng)
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; // 靠底,移不動(dòng)
else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)
tmpy = posy+1;
else tmpy = posy;
}
// 重畫(huà)焦點(diǎn)塊
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();
}
// 計(jì)算、畫(huà) 步數(shù)
if (posx == oldx posy == oldy) // 如果等于上一步的位置,表示回退了一步
steps = tmpstep; // 步數(shù)返回上一次的步數(shù)
else if (steps == tmpstep)
steps ++; // 步數(shù)增加
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); // 震動(dòng)100ms(Siemens擴(kuò)展)
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) // 菜單響應(yīng)
{
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]) // 按鍵設(shè)置
{
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)
{
/////////////////////////////////////////////
// 寫(xiě)入?yún)?shù)
try
{
byte bflag[] = new byte[2];
File keyfile = new File();
int fid = keyfile.open("CtrlKey");
keyfile.write(fid, ctrlkey, 0, 5); // 寫(xiě)入按鍵數(shù)據(jù)(Siemens擴(kuò)展)
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); // 緩存內(nèi)數(shù)據(jù)直接輸出到屏幕上
}
//音樂(lè)
private void SoundPlay(int n) // 播放音效
{
if (!Snd) return; // 音效關(guān)閉,則返回
// Siemens擴(kuò)展
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);
以上為核心代碼 由于代碼太多無(wú)法一次傳上來(lái)
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("開(kāi)始");
start.addActionListener(this);
preview=new Button("預(yù)覽");
preview.addActionListener(this);
set = new Button("設(shè)置");
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 初始化 構(gòu)造函數(shù)
public void actionPerformed(ActionEvent e) {
Button button=(Button)e.getSource();
if(button==start){
myCanvas.Start();
}else if(button==preview){
if(button.getLabel()=="預(yù)覽"){
container.remove(myCanvas);
container.add(panelPreview);
panelPreview.updateUI();
container.repaint();
button.setLabel("返回");
}else{
container.remove(panelPreview);
container.add(myCanvas);
container.repaint();
button.setLabel("預(yù)覽");
}
}else if(button==set){
Choice pic = new Choice();
//pic.add("QQ");
pic.add("美女");
int i=JOptionPane.showConfirmDialog(this,pic,"選擇圖片", JOptionPane.OK_CANCEL_OPTION);
//使用選擇對(duì)話(huà)框來(lái)進(jìn)行選擇圖片。
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;//設(shè)置方格的動(dòng)作監(jiān)聽(tīng)器的標(biāo)志位,TRUE為已經(jīng)添加上動(dòng)作事件
Cell cell[];//定義方格
Rectangle cellNull;//定義空方格區(qū)域 是一個(gè)矩形類(lèi)
public static int pictureID=4;// 當(dāng)前選擇的圖片代號(hào)
public MyCanvas() {
this.setLayout(null);
this.setSize(400,400);
cellNull=new Rectangle(300,300,100,100);//空方格區(qū)域在第三行每三列
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]);//移除最后一個(gè)多余的方格
} //放置9張小圖片并且移調(diào)最后一張
public void reLoadPictrue(){//當(dāng)選擇其它圖形進(jìn)行拼圖時(shí),需重新加載新圖片
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(){//對(duì)方格進(jìn)行重新排列,打亂順序
while(cell[0].getBounds().x=100cell[0].getBounds().y=100){//當(dāng)?shù)谝粋€(gè)方格距左上角較近時(shí)
int x=cellNull.getBounds().x;
int y=cellNull.getBounds().y;
int direction=(int)(Math.random()*4);//產(chǎn)生0-4,對(duì)應(yīng)空方格的上下左右移動(dòng)
if(direction==0){//空方格左移動(dòng),與左側(cè)方格互換位置,左側(cè)方格右移動(dòng)
x-=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){//依次尋找左側(cè)的按鈕
cell[j].move("RIGHT",100);
cellNull.setLocation(x,y);
break;//找到后跳出for循環(huán)
}
}
}
}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)//如果尚未添加動(dòng)作事件,則添加
for(int i=0;i15;i++)//為第個(gè)方格添加動(dòng)作事件,這樣單擊按鈕就能移動(dòng)了
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) {
//方格的鼠標(biāo)事件,因?yàn)橛玫搅薓yCanvas中的一些方法,因此沒(méi)有在Cell類(lèi)中處理鼠標(biāo)事件
Cell button=(Cell)e.getSource();
int x1=button.getBounds().x;//得到所單擊方格的坐標(biāo)
int y1=button.getBounds().y;
int x2=cellNull.getBounds().x;//得到空方格的坐標(biāo)
int y2=cellNull.getBounds().y;
if(x1==x2y1-y2==100)//進(jìn)行比較,如果滿(mǎn)足條件則進(jìn)行交換
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;//不滿(mǎn)足就不進(jìn)行任何處理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//進(jìn)行是否完成的判斷
JOptionPane.showMessageDialog(this,"景鋒恭喜你完成拼圖,加油!想繼續(xù)下一關(guān)么?");
for(int i=0;i15;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠標(biāo)事件,鼠標(biāo)單擊方格不在起作用
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){//實(shí)際為ICON
super(icon);
this.setSize(100,100);
}
public void move(String direction,int sleep){//方格的移動(dòng)
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);
}
}
}
網(wǎng)站欄目:華容道java源代碼 華容道編程
網(wǎng)頁(yè)網(wǎng)址:http://bm7419.com/article32/ddepesc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、、網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)公司、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容