import java.awt.event.*
public class MoveExample//主类
{
public static void main(String args[]) //定义主方法
{
new Hua_Rong_Road() //创建对象
}
}
class Person extends Button implements FocusListener
{
int number
Color c = new Color(128,128,128)
Person(int number,String s)//构造方法
{
super(s)/团行/调用父类s的构造方法
setBackground(c)//设置组件的背景色
this.number = number//调用当前的number
c = getBackground()
addFocusListener(this)//添加焦点事件监听器
}
public void focusGained(FocusEvent e)//焦点事件触发
{
setBackground(Color.blue)
}
public void focusLost(FocusEvent e)//焦点事件失去
{
setBackground(c)
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10]//person类的数组
Button left,right,above,below
Button restart = new Button("重新开始")
public Hua_Rong_Road()//华容道的构造方法
{
init()//初始化
setBounds(100,100,320,360)//设置窗口唯或碧在屏幕上出现位置,和窗口大小
setVisible(true)//设置窗口可见
setResizable(true)//设置窗口可调节
validate()//刷新
addWindowListener( new WindowAdapter()//获得窗口事件监视器
{
public void windowClosing(WindowEvent e)//窗口正在被关闭时,窗口监视器调用该方法
{
System.exit(0)
}
}
)
}
public void init()
{
setLayout(null)//设置默认布局
add(restart)//添加重新开始
restart.setBounds(100,320,120,25)//重新开始按钮大小
restart.addActionListener(this)//事件源获得监视器
String name[] = {"曹 *** ","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"}
for(int k = 0k<name.lengthk++)
{
person[k] = new Person(k,name[k])//给按钮添加名指举字
person[k].addMouseListener(this)//每个按钮都注册鼠标事件
person[k].addKeyListener(this)//每个按钮都注册键盘事件
add(person[k])//添加人物
}
person[0].setBounds(104,54,100,100)
person[1].setBounds(104,154,100,50)
person[2].setBounds(54,154,50,100)
person[3].setBounds(204,154,50,100)
person[4].setBounds(54,54,50,100)
person[5].setBounds(204,54,50,100)
person[6].setBounds(54,254,50,50)
person[7].setBounds(204,254,50,50)
person[8].setBounds(104,204,50,50)
person[9].setBounds(154,204,50,50)//为每个人物按钮设置位置和大小
person[9].requestFocus()//把焦点先设置在这个按钮上
left = new Button()//画出游戏界面边框,并用定义的left,right,above,below控制大小
right = new Button()
above = new Button()
below = new Button()
add(left)
add(right)
add(above)
add(below)
left.setBounds(49,49,5,260)
right.setBounds(254,49,5,260)
above.setBounds(49,49,210,5)
below.setBounds(49,304,210,5)
validate()//刷新
}//完成界面布局
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)//响应键盘事件,按键,释放键,按下和释放组合
{
Person man = (Person)e.getSource()//获得事件源
if(e.getKeyCode()==KeyEvent.VK_DOWN)//响应用户按下方向光标的 *** 作;用KeyEvent类中的getkeycode()判断哪个键被按下
{
go(man,below) //go方法控制移动
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above)
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left)
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right)
}
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource()
int x = -1,y = -1
x = e.getX()
y = e.getY()
int w = man.getBounds().width
int h = man.getBounds().height
if(y>h/2)
{
go(man,below)
}
if(y<h/2)
{
go(man,above)
}
if(x<w/2)
{
go(man,left)
}
if(x>w/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 = 0k<10k++)
{
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()
}
}
这个我试了的没有任务问题,稀望对你有点帮助,记得类名要改为Hua_Rong_Road ,因为只有Hua_Rong_Road 这个类是公开的.另外包名也改下package xxxx(你自己建的包名),玩游戏时移动人物,用键盘(上下左右 ,<--,-->,上,下) *** 作,鼠标是不能移动 人物的,照着我说的做,应该是没什么问题的:package baidu.testfive
import java.applet.Applet
import java.awt.Button
import java.awt.Color
import java.awt.Graphics
import java.awt.Rectangle
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import java.awt.event.KeyEvent
import java.awt.event.KeyListener
class People extends Button implements FocusListener // 代表华容道人物的类。
{
Rectangle rect = null
int left_x, left_y// 按扭的左上角坐标.
int width, height// 按扭的宽和高.
String name
int number
People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 构造函数
{
super(s)
name = s
this.number = number
left_x = x
left_y = y
width = w
height = h
setBackground(Color.orange)
road.add(this)
addKeyListener(road)
setBounds(x, y, w, h)
addFocusListener(this)
rect = new Rectangle(x, y, w, h)
}
public void focusGained(FocusEvent e) {
setBackground(Color.red)
}
public void focusLost(FocusEvent e) {
setBackground(Color.orange)
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,
ActionListener {
People people[] = new People[10]
Rectangle left, right, above, below// 华容道的边界 .
Button restart = new Button("重新开始迹笑毁")
public void init() {
setLayout(null)
add(restart)
restart.setBounds(5, 5, 80, 25)
restart.addActionListener(this)
people[0] = new People(0, "曹 *** ", 104, 54, 100, 100, this)// 构造曹升谨 ***
people[1] = new People(1, "关姿备羽", 104, 154, 100, 50, this)// 构造关羽
people[2] = new People(2, "张飞", 54, 154, 50, 100, this)
people[3] = new People(3, "刘备", 204, 154, 50, 100, this)
people[4] = new People(4, "张辽", 54, 54, 50, 100, this)
people[5] = new People(5, "曹仁", 204, 54, 50, 100, this)
people[6] = new People(6, "兵 ", 54, 254, 50, 50, this)
people[7] = new People(7, "兵 ", 204, 254, 50, 50, this)
people[8] = new People(8, "兵 ", 104, 204, 50, 50, this)
people[9] = new People(9, "兵 ", 154, 204, 50, 50, this)
people[9].requestFocus()
left = new Rectangle(49, 49, 5, 260)
people[0].setForeground(Color.white)
right = new Rectangle(254, 49, 5, 260)
above = new Rectangle(49, 49, 210, 5)
below = new Rectangle(49, 304, 210, 5)
}
public void paint(Graphics g) {// 画出华容道的边界:
g.setColor(Color.cyan)
g.fillRect(49, 49, 5, 260)// left.
g.fillRect(254, 49, 5, 260)// right.
g.fillRect(49, 49, 210, 5)// above.
g.fillRect(49, 304, 210, 5)// below.
// 提示曹 *** 逃出位置和按键规则:
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动", 100, 20)
g.setColor(Color.red)
g.drawString("曹 *** 到达该位置", 110, 300)
}
public void keyPressed(KeyEvent e) {
People man = (People) e.getSource()// 获取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y)
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
man.left_y = man.left_y + 50// 向下前进50个单位。
man.setLocation(man.left_x, man.left_y)
man.rect.setLocation(man.left_x, man.left_y)
// 判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
for (int i = 0i <10i++) {
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 = 0i <10i++) {
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 = 0i <10i++) {
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 = 0i <10i++) {
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()
}
}
package huaroadimport 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 = 0i <COMMANDS.lengthi++) // 添加菜单
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=0i<5i++) 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=0i<4i++) // 根据编号初始化游戏矩阵
for (j=0j<5j++)
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=0i<4i++)
for(j=0j<5j++)
{
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=0i<4i++)
for (int j=0j<5j++)
{
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 (img>0)
{
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=0i<4i++)
for (int j=0j<5j++)
{
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)
以上为核心代码 由于代码太多无法一次传上来
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)