📄 dongsprite.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class DongSprite extends Sprite implements Runnable {
int bombNum;// 主角拥有炸弹的数量
int score; // 主角的积分
int lastdirection;// 保存主角的最后的方向64DOWN 2UP 4LEFT 32RIGHT
int screenW; // 背景宽度
int screenH; // 背景高度
Sprite meetSprite; // 精灵的脚
Bomb bomb;// 创建炸弹精灵
private int x;
private int y;// 保存当前精灵所在位置
private int[] down = { 0, 1, 2 };
private int[] left = { 6, 7, 8 };
private int[] right = { 3, 4, 5 };
private int[] up = { 9, 10, 11 };
private int[] bombNext = { 0, 1, 2, 3 };
// 为移动方向作准备
public DongSprite(Image image1, int width, int height) {
super(image1, width, height);
Image img1 = null;
try {
img1 = Image.createImage("/meet.png");
} catch (IOException e) {
e.printStackTrace();
}
meetSprite = new Sprite(img1);
try {
img1 = Image.createImage("/bomb1.png");
} catch (IOException e) {
e.printStackTrace();
}
bomb = new Bomb(img1, 75, 80);
this.bomb.setFrameSequence(bombNext);
this.bomb.setVisible(false);
this.defineReferencePixel(width / 2, height / 2);
this.setFrameSequence(down); // 初始化方向
lastdirection = 64;
bombNum = 0;
score = 0;
}
public void collidesWithBomb() {
// 捡到炸弹
this.bombNum++;
if (this.bombNum > 10) {
bombNum = 10;
}
}
public void useBomb(int direction, int distance) {
// 使用炸弹
if (this.bombNum > 0) {
this.bombNum--;
if (this.bombNum < 0) {
bombNum = 0;
}
int x, y;
x = this.meetSprite.getX();
y = this.meetSprite.getY();
if (direction == 64) {// 向下
y = y + distance;
if (y > this.screenH) {
y = this.screenH;
}
} else if (direction == 2) {// 向上
y = y - distance;
if (y < 0) {
y = 0;
}
} else if (direction == 4) {// 向左
x = x - distance;
if (x < 0) {
x = 0;
}
} else if (direction == 32) {// 向右
x = x + distance;
if (x > this.screenW) {
x = this.screenW;
}
}
this.bomb.setPosition(x, y);
this.bomb.setVisible(true);
GameCenter.zaLayer.setCell((x) / 61, (y) / 41, 0);
Thread t = new Thread(this);
t.start();
}
}
public void forward(int direction, int distance) {
// 移动精灵,direction方向,distance距离
if (direction == 64) { // 向下走
if (direction != this.lastdirection) {
this.setFrameSequence(down);
this.lastdirection = direction;
} else {
this.nextFrame();
}
spriteMove(0, distance);
} else if (direction == 2) {// 向上走
if (direction != this.lastdirection) {
this.setFrameSequence(up);
this.lastdirection = direction;
} else {
this.nextFrame();
}
spriteMove(0, -distance);
} else if (direction == 4) {// 向左走
if (direction != this.lastdirection) {
this.setFrameSequence(left);
} else {
this.nextFrame();
}
spriteMove(-distance, 0);
} else if (direction == 32) {// 向右走
if (direction != this.lastdirection) {
this.setFrameSequence(right);
} else {
this.nextFrame();
}
spriteMove(distance, 0);
}
this.lastdirection = direction;
}
private void spriteMove(int mx, int my) {
x = this.getX();
y = this.getY();
x += mx;
y += my;
if (x < 0 || x > (this.screenW - 38)) {
x -= mx;
}
if (y < 0 || y > (this.screenH - this.getHeight() - 30)) {
y -= my;
}
this.setPosition(x, y);
// this.meetSprite.setPosition(x, y);
this.meetSprite.setPosition(x
+ (this.getWidth() - this.meetSprite.getWidth()) / 2, y
+ (this.getHeight() - this.meetSprite.getHeight()));
this.bomb.setPosition(x, y);
}
public void run() {
try {
Thread.sleep(80);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.bomb.nextFrame();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.bomb.nextFrame();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.bomb.nextFrame();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.bomb.setVisible(false);
Thread.yield();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -