📄 isoj2mecanvas.java
字号:
import isoj2me.*;import com.nokia.mid.ui.*;import javax.microedition.lcdui.*;import java.io.*;/** * <p>Title: isoj2me: J2ME Isometric Engine</p> * <p>Description: An engine/framework for isometric games (like japanese RPGs) for mobile devices supporting J2ME (MIDP 1.0). This engine will manage maps, objects and characters. Visit http://sourceforge.net/projects/isoj2me</p> * <p>Copyright: Copyright (c) 2004</p> * <p>License: Lesser GPL (http://www.gnu.org)</p> * <p>Company: Mondonerd.com</p> * @author Massimo Maria Avvisati * @version 0.1 */public class isoj2meCanvas extends FullCanvas implements Runnable { int x = 0; int y = 0; private volatile Thread t = null; //This is used to start and stop the canvas private int delay = 20; //Refresh time for this canvas used in the run() method private Board board = new Board("city"); private isoj2me.Character player = new isoj2me.Character(); private isoj2me.Item candle = new isoj2me.Item(); public isoj2meCanvas() { t = new Thread(this); t.start(); board.setTileSize(32, 8); board.createMap("/map_layer", 6); candle.putAction("candle", 2); candle.setCurrentAction("candle"); candle.x = 6; candle.y = 5; candle.z = 0; board.putItem(candle); player.putAction("standSW", 0); player.putAction("standNW", 0); player.putAction("standSE", 0); player.putAction("standNE", 0); player.putAction("walkSW", 1); player.putAction("walkNW", 1); player.putAction("walkSE", 1); player.putAction("walkNE", 1); player.setCurrentAction("standSW"); isoj2me.Character c = new isoj2me.Character("Sarah"); c.putAction("girlstandSW", 0); c.setCurrentAction("girlstandSW"); c.x = 7; c.y = 5; c.z = 0; board.putCharacter(c); player.x = 3; player.y = 3; player.z = 0; board.putCharacter(player); } private boolean canGo(int x, int y, int z, int oldX, int oldY, int oldZ) { if (board.isCharacter(x, y, z) != null || board.isItem(x, y, z) != null) { return false; } if ( ( (board.map.getCell(z, x, y) == 49 || board.map.getCell(z, x, y) == 56) && (board.map.getCell(z + 1, x, y) == 49 || board.map.getCell(z + 1, x, y) == 56) && board.map.getCell(z + 2, x, y) == 48) || ( (board.map.getCell(z - 1, x, y) == 49 || board.map.getCell(z - 1, x, y) == 56) && board.map.getCell(z, x, y) == 48 && board.map.getCell(z + 1, x, y) == 48) || ( (board.map.getCell(z, x, y) == 49 || board.map.getCell(z, x, y) == 56) && board.map.getCell(z + 1, x, y) == 48 && board.map.getCell(z + 2, x, y) == 48)) { //System.out.println("can go!"); return true; } else { //System.out.println("cannot go!"); //System.out.println("z + 2 = " + board.map.getCell(z + 2, x, y)); //System.out.println("z + 1 = " + board.map.getCell(z + 1, x, y)); //System.out.println("z = " + board.map.getCell(z, x, y)); //System.out.println("z - 1 = " + board.map.getCell(z - 1, x, y)); return false; } } public void keyPressed(int keyCode) { int newX, newY, newZ, oldX, oldY, oldZ; oldX = player.x; oldY = player.y; oldZ = player.z; newX = player.x; newY = player.y; newZ = player.z; int incX = 0; int incY = 0; if (keyCode == KEY_NUM0) { System.out.println("x: " + player.x + " y: " + player.y + " z: " + player.z + " tile: " + board.map.getCell(player.z, player.x, player.z)); } if (keyCode == KEY_NUM2 || keyCode == -1) { newY = player.y - 1; incY = -1; player.setCurrentAction("walkNE"); } if (keyCode == KEY_NUM4 || keyCode == -3) { newX = player.x - 1; incX = -1; player.setCurrentAction("walkNW"); } if (keyCode == KEY_NUM6 || keyCode == -4) { newX = player.x + 1; incX = 1; player.setCurrentAction("walkSE"); } if (keyCode == KEY_NUM8 || keyCode == -2) { newY = player.y + 1; incY = 1; player.setCurrentAction("walkSW"); } if (canGo(newX, newY, newZ, oldX, oldY, oldZ)) { player.x = newX; player.y = newY; player.z = newZ; x = x + incX; y = y + incY; board.moveCharacter(player, oldX, oldY, oldZ, player.x, player.y, player.z, false); } } private void updatePlayer() { if (board.map.getCell(player.z + 1, player.x, player.y) == 49) { board.moveCharacter(player, player.x, player.y, player.z, player.x, player.y, player.z + 1, false); player.z++; } if (board.map.getCell(player.z, player.x, player.y) == 48) { board.moveCharacter(player, player.x, player.y, player.z, player.x, player.y, player.z - 1, false); player.z--; } } protected void paint(Graphics g) { updatePlayer(); g.setColor(0x9999cc); g.fillRect(0, 0, getWidth(), getHeight()); board.draw(getWidth() / 2 - 16, 0, x, y, 5, 5, g); if (player.getCurrentAction().equals("walkSW")) { player.setCurrentAction("standSW"); } if (player.getCurrentAction().equals("walkSE")) { player.setCurrentAction("standSE"); } if (player.getCurrentAction().equals("walkNW")) { player.setCurrentAction("standNW"); } if (player.getCurrentAction().equals("walkNE")) { player.setCurrentAction("standNE"); } } public void run() { Thread current = Thread.currentThread(); while (current == t) { repaint(); serviceRepaints(); try { Thread.sleep(delay); } catch (InterruptedException ex) { } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -