這個我試了的沒有任務(wù)問題,稀望對你有點(diǎn)幫助,記得類名要改為Hua_Rong_Road ,因?yàn)橹挥蠬ua_Rong_Road 這個類是公開的.另外包名也改下package xxxx(你自己建的包名),玩游戲時移動人物,用鍵盤(上下左右 ,--,--,上,下)操作,鼠標(biāo)是不能移動 人物的,照著我說的做,應(yīng)該是沒什么問題的:
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、京山ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的京山網(wǎng)站制作公司
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;// 按扭的左上角坐標(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("重新開始");
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) {// 畫出華容道的邊界:
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)的人物,然后按鍵盤上的上下左右箭頭移動", 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個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或下邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回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; // 向上前進(jìn)50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或上邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回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; // 向左前進(jìn)50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或左邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回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; // 向右前進(jìn)50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或右邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回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();
}
}
import java.awt.*;
import java.awt.event.*;
public class MoveExample //主類
{
public static void main(String args[]) //定義主方法
{
new Hua_Rong_Road(); //創(chuàng)建對象
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(128,128,128);
Person(int number,String s)//構(gòu)造方法
{
super(s);//調(diào)用父類s的構(gòu)造方法
setBackground(c);//設(shè)置組件的背景色
this.number = number;//調(diào)用當(dāng)前的number
c = getBackground();
addFocusListener(this);//添加焦點(diǎn)事件監(jiān)聽器
}
public void focusGained(FocusEvent e)//焦點(diǎn)事件觸發(fā)
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦點(diǎn)事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person類的數(shù)組
Button left,right,above,below;
Button restart = new Button("重新開始");
public Hua_Rong_Road() //華容道的構(gòu)造方法
{
init(); //初始化
setBounds(100,100,320,360);//設(shè)置窗口在屏幕上出現(xiàn)位置,和窗口大小
setVisible(true);//設(shè)置窗口可見
setResizable(true);//設(shè)置窗口可調(diào)節(jié)
validate();//刷新
addWindowListener( new WindowAdapter()//獲得窗口事件監(jiān)視器
{
public void windowClosing(WindowEvent e)//窗口正在被關(guān)閉時,窗口監(jiān)視器調(diào)用該方法
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);//設(shè)置默認(rèn)布局
add(restart);//添加重新開始
restart.setBounds(100,320,120,25);//重新開始按鈕大小
restart.addActionListener(this);//事件源獲得監(jiān)視器
String name[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.length;k++)
{
person[k] = new Person(k,name[k]);//給按鈕添加名字
person[k].addMouseListener(this);//每個按鈕都注冊鼠標(biāo)事件
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);//為每個人物按鈕設(shè)置位置和大小
person[9].requestFocus();//把焦點(diǎn)先設(shè)置在這個按鈕上
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)//響應(yīng)鍵盤事件,按鍵,釋放鍵,按下和釋放組合
{
Person man = (Person)e.getSource();//獲得事件源
if(e.getKeyCode()==KeyEvent.VK_DOWN)//響應(yīng)用戶按下方向光標(biāo)的操作;用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){}//鼠標(biāo)事件
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();
}
}
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-關(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ā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 },
},
{ // 左右夾擊
{ 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="開啟聲音", closeSnd="關(guān)閉聲音", openVib="開啟振動", closeVib="關(guān)閉振動";
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; // 棋子的大小偏移量(相對于當(dāng)前塊)。
// 比如當(dāng)前塊是曹操的右上角,那么offx=-1,offy=1
// 表示x負(fù)方向還有一塊,y的正方向有一塊
private int steps, tmpstep; // 已走步數(shù)
private Image nameImg[]; // 名字圖片(由于可寫文字太大,所以用圖片)
private SelNew gamelist;
private Display display;
private Quitable winQuit;
private boolean selected, gameOver; // 是否選中/是否游戲結(jié)束
private ExtendedImage ExScreenImg; // 擴(kuò)展圖片作為繪圖緩存(Siemens擴(kuò)展)
private Graphics Exg; // 緩存的Graphics對象
private MelodyComposer melody; // midi播放(Siemens擴(kuò)展)
public HRGame()
{
if (getHeight() 64) // 根據(jù)屏幕高度得到圖形尺寸,以便針對不同機(jī)型
{
// 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"); // 關(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對象
melody = new MelodyComposer(); // 新建midi
Snd = true; // 音效開啟
Vib = true; // 震動開啟
/////////////////////////////////////////////
// 讀取按鍵設(shè)置 共7字節(jié),依次表示:左按鍵、右按鍵、上按鍵、下按鍵、選中按鍵、音效、震動
try
{
byte bflag[] = new byte[2];
bflag[0] = bflag[1] = 1;
File keyfile = new File();
int fid = keyfile.open("CtrlKey"); // 打開文件(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-開啟,其他-關(guān)閉)
Vib = (bflag[1] == 1); // 震動參數(shù) (1-開啟,其他-關(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() // 顯示游戲畫面
{
display.setCurrent(this);
setCommandListener(this);
}
public void newGame(SelNew gmlist) // 新游戲
{
gamelist = gmlist;
int gnum = gamelist.getSelectedIndex(); // 獲取游戲布局編號
int i, j;
for (i=0; i4; i++) // 根據(jù)編號初始化游戲矩陣
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; // 沒有選中某一塊
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 = "五關(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);
// 畫步數(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);
// 畫棋子
byte layout[] = {0,0,0,0,0, 0,0,0,0,0}; // 分別表示 10個棋子是否已經(jīng)處理過,依次為:
// 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) // 如果這個棋子沒有畫過
{
moveSelbox(i, j, false); // 畫一個棋子
layout[grid[i][j]-1] = 1; // 已經(jīng)畫過了,標(biāo)志置1
}
}
drawSelbox(); // 畫選擇框
SoundPlay(0); // 播放游戲開始音效
}
protected void paint(Graphics parag)
{
redraw();
}
// 畫選擇框
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); //畫外框
}
}
// 移動選擇框
// i,j當(dāng)前塊的(x,y)坐標(biāo), 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) // 左側(cè)
if (grid[i][j] == grid[i-1][j]) ox = -1;// 左側(cè)屬于同一個棋子
if (i 3) // 右側(cè)
if (grid[i][j] == grid[i+1][j]) ox = 1; // 右側(cè)屬于同一個棋子
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)焦點(diǎn)格坐標(biāo),(ox,oy)整塊相對焦點(diǎn)的偏移量,
// 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) // 按鍵響應(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; // 靠邊,移不動
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;
}
// 重畫焦點(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();
}
// 計算、畫 步數(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); // 震動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)
{
/////////////////////////////////////////////
// 寫入?yún)?shù)
try
{
byte bflag[] = new byte[2];
File keyfile = new File();
int fid = keyfile.open("CtrlKey");
keyfile.write(fid, ctrlkey, 0, 5); // 寫入按鍵數(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ù)直接輸出到屏幕上
}
//音樂
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);
以上為核心代碼 由于代碼太多無法一次傳上來
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[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
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組件寫的,你應(yīng)該是要用swing寫的吧,照這個改改吧...