📄 hrgame.java
字号:
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=0; i<4; i++) for (int j=0; j<5; 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); melody.appendNote(MelodyComposer.TONE_C3, MelodyComposer.TONELENGTH_1_8); } else if (n == 2) // move { melody.setBPM(140); melody.appendNote(MelodyComposer.TONE_C1, MelodyComposer.TONELENGTH_1_32); } Melody music = melody.getMelody(); music.play(); // 播放 music = null; } catch (Exception exception){} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -