📄 gdgamecanvas.java
字号:
package Com.JXT.MIDlet;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class GDGameCanvas extends GameCanvas implements Runnable {
private Image img, img1;
GDGameCanvas g;
int countX = 0; // 记录点击次数
int countY = 0;
int direction;
int commenZBJSX = 80;
int commenZBJSY = 80;
int commenceX = 80; // 开始x坐标
int commenceY = 80;
int roleMoveControl = -1;
private final static int R_STOP = -1;
private int status; // 游戏状态
private int currIndex = 0;
private final static int IMG_Width = 40;
private final static int IMG_Height = 40;
int speed = 4; // 移动2个像素 移动速度
int x = 0, y = 0;
int canvasx = 0;
int canvasy = 0;
int count = 0;
private Sprite s1; // 创建精灵
private Graphics gra;
private TiledLayer tlRoad;
private TiledLayer tlWall;
LayerManager Layer;
int FX = 10;
boolean bool = true;
/** 人物方向常量 */
private final static int DIRE_LEFT = 3;
private final static int DIRE_RIGHT = 9;
private final static int DIRE_UP = 6;
private final static int DIRE_DOWN = 0;
private final static int SCREEN_1 = 0;
int cellShuzhu[][] = new int[][] { { 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 2, 2, 2, 2, 2 }, { 1, 1, 2, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2, 2, 2, 2 }, { 1, 1, 2, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 2, 2, 2, 2, 2 }, { 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 } };
int[] canMoveTile = { 2 }; // 可执行
protected GDGameCanvas(boolean t) {
super(t);
g = this;
Layer = new LayerManager();// 创建图层管理器
gra = this.getGraphics(); //
img = this.createImage11();// 创建地图
img1 = this.createImage2();
s1 = new Sprite(img1, img1.getWidth() / 3, img1.getHeight() / 4); // 精灵
tlRoad = new TiledLayer(8, 8, img, img.getWidth() / 2, img.getHeight());
tlWall = new TiledLayer(8, 8, img, img.getWidth() / 2, img.getHeight());
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (cellShuzhu[i][j] == 1) {
tlRoad.setCell(j, i, cellShuzhu[i][j]);
} else {
tlWall.setCell(j, i, cellShuzhu[i][j]);
}
}
}
Layer.append(tlRoad); // 添加图层
Layer.append(tlWall);
new Thread(g).start();
Layer.paint(gra, 0, 0);
s1.move(commenceX, commenceY);
s1.paint(gra);
}
Runtime rt = Runtime.getRuntime();
public void run() {
while (bool) {
try {
Thread.sleep(100);
tick();
// 判断
if(rt.freeMemory() < 100000){
System.gc();
}
// 重新绘制
this.repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
Layer.paint(gra, 0, 0);
s1.paint(gra);
}
}
public void tick() // 判断游戏状态
{
// beiji SCREEN_1 ==0
if( roleMoveControl != R_STOP )
{
switch (direction) {
case DIRE_UP:
currIndex=7;
s1.setFrame(7);
if ((commenceY -= speed) >= 0) {
if (collis()) {
commenceY += speed;
} else {
s1.move(0, -speed);
}
}
break;
case DIRE_DOWN:
currIndex=1;
s1.setFrame(1);
if ((commenceY += speed) <= this.getHeight()) {
if (collis()) {
commenceY -= speed;
} else {
s1.move(0, speed);
}
}
break;
case DIRE_LEFT:
currIndex=4;
s1.setFrame(4);
if ((commenceX -= speed) >= 0) {
if (collis()) {
commenceX += speed;
} else {
s1.move(-speed, 0);
// commenceX -= speed;
}
}
break;
case DIRE_RIGHT:
currIndex=10;
s1.setFrame(10);
if ((commenceX += speed) <= this.getWidth()) {
if (collis()) {
commenceX -= speed;
} else {
s1.move(speed, 0);
// commenceX += speed;
}
}
break;
}
if( currIndex == direction + 1 )
{
currIndex = direction +2;
s1.setFrame(currIndex);
this.repaint();
}
else
{
currIndex = direction +1;
s1.setFrame(currIndex);
this.repaint();
}
}
else
{
currIndex =direction ;
s1.setFrame(currIndex );
}
}
public Image createImage11() {
Image img1 = null;
try {
img1 = Image.createImage("/2ge.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img1;
}
public Image createImage2() {
Image img1 = null;
try {
img1 = Image.createImage("/man.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img1;
}
public void keyReleased(int keyCode) { // 根据游戏状态实现按键释放处理
if (status ==SCREEN_1)
{
int action = this.getGameAction(keyCode);
switch (action) {
case UP:
case DOWN:
case LEFT:
case RIGHT:
roleMoveControl = R_STOP; // 不移动
}
}
}
//按键重复点击
public void keyPressed(int keyCode)
{
// System.out.println(keyCode);
// 根据游戏状态实现按键处理
if (status == SCREEN_1) {
int action = this.getGameAction(keyCode);
switch (action) {
case UP:
direction = DIRE_UP;
roleMoveControl = DIRE_UP;
//this.flushGraphics();
break;
case DOWN:
direction = DIRE_DOWN;
roleMoveControl = DIRE_DOWN;
//this.flushGraphics();
break;
case LEFT:
direction = DIRE_LEFT;
roleMoveControl = DIRE_LEFT;
//this.flushGraphics();
break;
case RIGHT:
direction = DIRE_RIGHT;
roleMoveControl = DIRE_RIGHT;
//this.flushGraphics();
break;
case FIRE:
break;
}
}
}
/**
* 第row行第col列图块是否可行走
*
* @param row
* 行号,从0开始
* @param col
* 列号,从0开始
* @return true代表可以行走,false代表不能行走
*/
private boolean canMove(int row, int col) // 判断是否碰撞碰撞为true
{
boolean bool = false;
for (int i = 0; i < canMoveTile.length; i++) {
if (canMoveTile[0] == cellShuzhu[row][col]) {
bool = true;
break;
}
}
return bool;
}
/**
* 碰撞检测 原理:以人物16X32区域的下半部12X12和地图坐标判断, 只有地图块 为可移动地图块数组的才能够行走
*
* @return true代表发生碰撞,false代表未碰撞
*/
int JH =0;
private boolean collis() {
// 获得 区域的 坐标
int leftX = commenceX;
int topY = commenceY;
// 左上角
int col = (40 + commenceX) / IMG_Width;
int row = (40 + commenceY) / IMG_Height; // 行
if (!canMove(row, col)) {
return true;
}
// 右shang
col = (40 + commenceX) / IMG_Width;
row = (40 + commenceY) / IMG_Height; // 行
if (!canMove(row, col)) {
return true;
}
// 左下角
col = ((40 + commenceX) / IMG_Width);
row = (40 + commenceY) / IMG_Height; // 行
if (!canMove(row, col)) {
return true;
}
// 右xia
col = (40 + commenceX) / IMG_Width;
row = (40 + commenceY) / IMG_Height; // 行
if (!canMove(row, col)) {
return true;
}
// 若果 不满足则返回false
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -