📄 maincanvas.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MainCanvas extends Canvas implements Runnable {
Thread t;
private int[][] map;
private int[][] mapHit;
final static private int INT_MAP_SIZE = 16;
final static private int INT_WIDTH = 176;
final static private int INT_HEIGHT = 208;
final static private int ROW = INT_WIDTH/INT_MAP_SIZE;
final static private int COL = INT_HEIGHT/INT_MAP_SIZE;
/**
* 储存按键的
*/
private int intKeyCode = 0;
final static private int KEY_UP = -1;
final static private int KEY_DOWN = -2;
final static private int KEY_LEFT = -3;
final static private int KEY_RIGHT = -4;
final static private int KEY_FIRE = -5;
final static private int KEY_LEFT_SOFT = -6;
final static private int KEY_RIGHT_SOFT = -7;
final static private int MY_KEY_UP = 1<<1;
final static private int MY_KEY_DOWN = 1<<2;
final static private int MY_KEY_LEFT = 1<<3;
final static private int MY_KEY_RIGHT = 1<<4;
final static private int MY_KEY_FIRE = 1<<5;
final static private int MY_KEY_LEFT_SOFT = 1<<6;
final static private int MY_KEY_RIGHT_SOFT = 1<<7;
//*************************************************地图相关
private int intMapStartX = 0;
private int intMapStartY = 0;
private int intMapStepX = 0;
private int intMapStepY = 0;
private Image imageMap=null;
//*************************************************人物相关
final static private int DIR_DOWN = 0;//X Y -4
final static private int DIR_LEFT = 1;//X-4 Y
final static private int DIR_RIGHT = 2;//X+4 Y
final static private int DIR_UP = 3;//X Y+4
final static private int[][] INT_CHANGE = {
{0,1},
{-1,0},
{1,0},
{0,-1},
};
final static private int[][] INT_ROLE_POS = {
{ 199, 32, 22, 27, 0 },// xia
{ 224, 31, 21, 27, 0 },
{ 245, 30, 23, 29, 0 },
{ 0, 57, 22, 29, 0 },
{ 211, 0, 22, 31, 0 },// zuo
{ 211, 0, 22, 31, 0 }, { 236, 0, 25, 30, 0 },
{ 0, 27, 25, 30, 0 },
{ 211, 0, 22, 31, 1 },// you
{ 211, 0, 22, 31, 1 }, { 236, 0, 25, 30, 1 }, { 0, 27, 25, 30, 1 },
{ 0, 0, 22, 27, 0 },// shang
{ 25, 0, 21, 27, 0 }, { 46, 0, 23, 29, 0 }, { 69, 0, 23, 29, 0 }, };
private Image imageRole = null;
// final static private int INT_ROLE_WIDTH = 32;
// final static private int INT_ROLE_HEIGHT= 48;
/**
* 人物所在的地图格X
*/
private int intRoleStartX = 3;
/**
* 人物所在的地图格Y
*/
private int intRoleStartY = 3;
/**
* 人物对于地图格的偏移X
*/
private int intRoleStepX = 0;
/**
* 人物对于地图格的偏移Y
*/
private int intRoleStepY = 0;
final static private int INT_SPEED = 4;
private int intDir = DIR_RIGHT;
private int intMotion = 0;
final static private int INT_MOTION_MAX = 2;
private boolean blnIsWalk = false;
final static private int[] INT_MAP_HIT = {
0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0
};
public MainCanvas() {
map = new int[Res.MAP.length][Res.MAP[0].length];
mapHit = new int[Res.MAP.length][Res.MAP[0].length];
for(int i=0;i<map.length;i++){
System.arraycopy(Res.MAP[i], 0, map[i], 0, map[0].length);
//生成碰撞数组
for(int j = 0;j<map[0].length;j++){
mapHit[i][j] = INT_MAP_HIT[map[i][j]];
//10000// 2<< 3
//11010000//index 3<<6
}
}
try {
imageMap = Image.createImage("/map1.png");
imageRole = Image.createImage("/role1.png");
} catch (IOException e) {
e.printStackTrace();
}
// map = Res.MAP;
// map[0][0] = 10;
// System.out.println(Res.MAP[0][0]);
t=new Thread(this);
t.start();
}
protected void keyPressed(int keyCode) {
switch(keyCode){
case KEY_UP:
intKeyCode |= MY_KEY_UP;
break;
case KEY_DOWN:
intKeyCode |= MY_KEY_DOWN;
break;
case KEY_LEFT:
intKeyCode |= MY_KEY_LEFT;
break;
case KEY_RIGHT:
intKeyCode |= MY_KEY_RIGHT;
break;
case KEY_FIRE:
intKeyCode |= MY_KEY_FIRE;
break;
case KEY_LEFT_SOFT:
intKeyCode |= MY_KEY_LEFT_SOFT;
break;
case KEY_RIGHT_SOFT:
intKeyCode |= MY_KEY_RIGHT_SOFT;
break;
}
}
protected void keyReleased(int keyCode) {
switch(keyCode){
case KEY_UP:
intKeyCode &= (~MY_KEY_UP);
break;
case KEY_DOWN:
intKeyCode &= (~MY_KEY_DOWN);
break;
case KEY_LEFT:
intKeyCode &= (~MY_KEY_LEFT);
break;
case KEY_RIGHT:
intKeyCode &= (~MY_KEY_RIGHT);
break;
case KEY_FIRE:
intKeyCode &= (~MY_KEY_FIRE);
break;
case KEY_LEFT_SOFT:
intKeyCode &= (~MY_KEY_LEFT_SOFT);
break;
case KEY_RIGHT_SOFT:
intKeyCode &= (~MY_KEY_RIGHT_SOFT);
break;
}
}
protected void paint(Graphics g) {
//绘制地图
for (int i = -1; i < ROW + 1; i++) {
for (int j = -1; j < COL + 1; j++) {
if(i+intMapStartX < 0 || j+intMapStartY < 0
|| i+intMapStartX > map[0].length -1
|| j+intMapStartY > map.length - 1)
continue;
int mapX = i * INT_MAP_SIZE + intMapStepX;
int mapY = j * INT_MAP_SIZE + intMapStepY;
g.setClip(mapX, mapY, INT_MAP_SIZE, INT_MAP_SIZE);
int temp = map[j+intMapStartY][i+intMapStartX] - 1;
g.drawImage(imageMap, mapX - temp % 12
* INT_MAP_SIZE, mapY - temp / 12
* INT_MAP_SIZE, 20);
}
}
//绘制人
// 人物所在的地图块在屏幕位置
int tempX = (intRoleStartX - intMapStartX)*INT_MAP_SIZE + intMapStepX + intRoleStepX;
int tempY = (intRoleStartY - intMapStartY)*INT_MAP_SIZE + intMapStepY + intRoleStepY;
int roleWidth = INT_ROLE_POS[intDir*4+intMotion + 2][2];
int roleHeight = INT_ROLE_POS[intDir*4+intMotion + 2][3];
int width = (roleWidth - INT_MAP_SIZE) / 2;
int height = roleHeight - INT_MAP_SIZE;
// int width = (INT_ROLE_WIDTH - INT_MAP_SIZE) / 2;
// int height = INT_ROLE_HEIGHT - INT_MAP_SIZE;
int x = tempX - width;//实际setClip的XY坐标
int y = tempY - height;
if (INT_ROLE_POS[intDir * 4 + intMotion + 2][4] == 0) {
g.setClip(x, y, roleWidth, roleHeight);
// X方向的偏移取决于当前哪一个动作
// Y方向的偏移取决于当前人物方向
g.drawImage(imageRole, x
- INT_ROLE_POS[intDir * 4 + intMotion + 2][0], y
- INT_ROLE_POS[intDir * 4 + intMotion + 2][1], 20);
} else {
drawMirror(g, imageRole, x, y, roleWidth, roleHeight,
INT_ROLE_POS[intDir * 4 + intMotion + 2][0],
INT_ROLE_POS[intDir * 4 + intMotion + 2][1]);
}
g.setClip(0, 0, INT_WIDTH, INT_HEIGHT);
}
private void drawMirror(Graphics g, Image image,int x, int y, int width, int height,
int imageX, int imageY){
for(int i=0;i<width;i++){
g.setClip(x + i, y, 1, height);
g.drawImage(image, x + i - (imageX + (width - 1) - i), y - imageY, 20);
}
}
public void run() {
while(true){
try {
long start = System.currentTimeMillis();
//logic和绘制
judgeKey();
logic(intDir);
repaint();
//休息
long last = System.currentTimeMillis() - start;
if(last < 80){
Thread.sleep(80 - last);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void judgeKey(){
if((intKeyCode & MY_KEY_LEFT)!=0){
blnIsWalk = true;
intDir = DIR_LEFT;
// intMapStartX --;
// if(intMapStartX < 0){
// intMapStartX = 0;
// }
} else if((intKeyCode & MY_KEY_RIGHT)!=0){
blnIsWalk = true;
intDir = DIR_RIGHT;
// intMapStartX ++;
// if(intMapStartX > map[0].length - ROW){
// intMapStartX = map[0].length - ROW;
// }
}
if((intKeyCode & MY_KEY_UP)!=0){
blnIsWalk = true;
intDir = DIR_UP;
} else if((intKeyCode & MY_KEY_DOWN)!=0){
blnIsWalk = true;
intDir = DIR_DOWN;
}
}
private int num = 0;
private void logic(int dir){
if(blnIsWalk){
intMotion++;
intMotion %= INT_MOTION_MAX;
if(!judgeMap(dir)){//不能走
blnIsWalk = false;
return;
}
intRoleStepX += INT_CHANGE[dir][0] * INT_SPEED;
intRoleStepY += INT_CHANGE[dir][1] * INT_SPEED;
int tempX = (intRoleStartX - intMapStartX)*INT_MAP_SIZE + intMapStepX+ intRoleStepX;
int tempY = (intRoleStartY - intMapStartY)*INT_MAP_SIZE + intMapStepY+ intRoleStepY;
//向右的卷屏
if (dir == DIR_RIGHT && tempX > INT_WIDTH / 2) {
if (intMapStepX != 0 || intMapStartX != (map[0].length - ROW)) {
intMapStepX -= INT_CHANGE[dir][0] * INT_SPEED;
if (Math.abs(intMapStepX) >= INT_MAP_SIZE) {
intMapStartX -= intMapStepX / INT_MAP_SIZE;
intMapStepX %= INT_MAP_SIZE;
}
}
}
//向左的卷屏
if (dir == DIR_LEFT && tempX < INT_WIDTH / 2) {
if (intMapStartX != 0 || intMapStepX != 0) {
intMapStepX -= INT_CHANGE[dir][0] * INT_SPEED;
if (Math.abs(intMapStepX) >= INT_MAP_SIZE) {
intMapStartX -= intMapStepX / INT_MAP_SIZE;
intMapStepX %= INT_MAP_SIZE;
}
}
}
System.out.println(tempY);
//向下的卷屏
if (dir == DIR_DOWN && tempY > INT_HEIGHT / 2) {
if (intMapStepY != 0 || intMapStartY != (map.length - COL)) {
intMapStepY -= INT_CHANGE[dir][1] * INT_SPEED;
if (Math.abs(intMapStepY) >= INT_MAP_SIZE) {
intMapStartY -= intMapStepY / INT_MAP_SIZE;
intMapStepY %= INT_MAP_SIZE;
}
}
}
//向上的卷屏
if (dir == DIR_UP && tempY < INT_HEIGHT / 2) {
if (intMapStartY != 0 || intMapStepY != 0) {
intMapStepY -= INT_CHANGE[dir][1] * INT_SPEED;
if (Math.abs(intMapStepY) >= INT_MAP_SIZE) {
intMapStartY -= intMapStepY / INT_MAP_SIZE;
intMapStepY %= INT_MAP_SIZE;
}
}
}
//更新格数
if(Math.abs(intRoleStepX) >= INT_MAP_SIZE){
intRoleStartX += intRoleStepX / INT_MAP_SIZE;
intRoleStepX %= INT_MAP_SIZE;
}
if(Math.abs(intRoleStepY) >= INT_MAP_SIZE){
intRoleStartY += intRoleStepY / INT_MAP_SIZE;
intRoleStepY %= INT_MAP_SIZE;
}
}
blnIsWalk = false;
}
private boolean judgeMap(int dir){
if((dir == DIR_LEFT || dir == DIR_RIGHT) && intRoleStepX == 0){
int x = intRoleStartX + INT_CHANGE[dir][0];
//半步
if(intRoleStepY < 0){
int temp1 = mapHit[intRoleStartY - 1][x];
int temp2 = mapHit[intRoleStartY][x];
if(temp1 == 1 && temp2 == 1){
return false;
} else if(temp1 == 0 && temp2 == 1){
if (num == 0) {
num = 1;
logic(DIR_UP);
num = 0;
}
return false;
} else if(temp1 == 1 && temp2 == 0){
if (num == 0) {
num = 1;
logic(DIR_DOWN);
num = 0;
}
return false;
}
// if(mapHit[intRoleStartY - 1][x] != 0){
// return false;
// }
// if(mapHit[intRoleStartY][x] != 0){
// //判断能不能通过向上移动 然后从上一格走
// if (num == 0) {
// num = 1;
// logic(DIR_UP);
// num = 0;
// }
// return false;
// }
} else if(intRoleStepY > 0){
int temp1 = mapHit[intRoleStartY + 1][x];
int temp2 = mapHit[intRoleStartY][x];
if(temp1 == 1 && temp2 == 1){
return false;
} else if(temp1 == 0 && temp2 == 1){
if (num == 0) {
num = 1;
logic(DIR_DOWN);
num = 0;
}
return false;
} else if(temp1 == 1 && temp2 == 0){
if (num == 0) {
num = 1;
logic(DIR_UP);
num = 0;
}
return false;
}
// if(mapHit[intRoleStartY + 1][x] != 0){
// return false;
// }
// if(mapHit[intRoleStartY][x] != 0){
// if (num == 0) {
// num = 1;
// logic(DIR_DOWN);
// num = 0;
// }
// return false;
// }
} else if(intRoleStepY == 0){
if(mapHit[intRoleStartY][x] != 0){
return false;
}
}
// if(intRoleStepY < - (INT_MAP_SIZE - 4)){
// if(mapHit[intRoleStartY - 1][x] != 0){
// return false;
// }
// if(mapHit[intRoleStartY][x] != 0){
// return false;
// }
// } else if(intRoleStepY < 0){
// if(mapHit[intRoleStartY][x] != 0){
// return false;
// }
// intRoleStepY = 0;
// } else if(intRoleStepY == 0){
// if(mapHit[intRoleStartY][x] != 0){
// return false;
// }
// } else if(intRoleStepY > 0 && intRoleStepY <= 4){
// if(mapHit[intRoleStartY][x] != 0){
// return false;
// }
// if(mapHit[intRoleStartY + 1][x] != 0){
// return false;
// }
// } else if(intRoleStepY > 4){
// if(mapHit[intRoleStartY + 1][x] != 0){
// return false;
// }
// intRoleStepY = INT_MAP_SIZE;
// }
}
if((dir == DIR_UP || dir == DIR_DOWN) && intRoleStepY == 0){
int y = intRoleStartY + INT_CHANGE[dir][1];
// 半步
if(intRoleStepX < 0){
int temp1 = mapHit[y][intRoleStartX - 1];
int temp2 = mapHit[y][intRoleStartX];
if(temp1 == 1 && temp2 == 1){
return false;
} else if(temp1 == 0 && temp2 == 1){
if (num == 0) {
num = 1;
logic(DIR_LEFT);
num = 0;
}
return false;
} else if(temp1 == 1 && temp2 == 0){
if (num == 0) {
num = 1;
logic(DIR_RIGHT);
num = 0;
}
return false;
}
// if(mapHit[y][intRoleStartX - 1] != 0){
// return false;
// }
// if(mapHit[y][intRoleStartX] != 0){
// if (num == 0) {
// num = 1;
// logic(DIR_LEFT);
// num = 0;
// }
// return false;
// }
} else if(intRoleStepX > 0){
int temp1 = mapHit[y][intRoleStartX + 1];
int temp2 = mapHit[y][intRoleStartX];
if(temp1 == 1 && temp2 == 1){
return false;
} else if(temp1 == 0 && temp2 == 1){
if (num == 0) {
num = 1;
logic(DIR_RIGHT);
num = 0;
}
return false;
} else if(temp1 == 1 && temp2 == 0){
if (num == 0) {
num = 1;
logic(DIR_LEFT);
num = 0;
}
return false;
}
// if(mapHit[y][intRoleStartX + 1] != 0){
// return false;
// }
// if(mapHit[y][intRoleStartX] != 0){
// if (num == 0) {
// num = 1;
// logic(DIR_RIGHT);
// num = 0;
// }
// return false;
// }
} else if(intRoleStepX == 0){
if(mapHit[y][intRoleStartX] != 0){
return false;
}
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -