📄 maincanvas.java
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class MainCanvas extends Canvas implements Runnable {
private boolean start;
private MIDlet midlet;
private Image screen;
private Graphics g;
private int width, height;
private int key[];
// ----------------player-----------------------------
private int px, py = 250;
private Image pimgL, pimgR;
private Image fireL, fireR;
private int pw, ph;
public static final int LEFT = 0;
public static final int RIGHT = 1;
private int dir = RIGHT;
public static final int STAND = 0;
public static final int CROUCH = 1;
public static final int JUMP = 2;
public static final int RUN = 3;
public static final int ATTACH = 4;
public static int state = STAND;
public int frame = 0;
private int v0 = 20;
private int vg = 2;
/**
* 0当前刀光有效,1 x坐标,2 y坐标, 3 时间,4速度 5,杀伤力,6 方向
*/
private int[][] fireLR;
// ---------------------------------------------
//
public MainCanvas(MIDlet mid) {
setFullScreenMode(true);
width = getWidth();
height = getHeight();
midlet = mid;
start = true;
key = new int[20];
screen = Image.createImage(width, height);
g = screen.getGraphics();
try {
pimgL = Image.createImage("/roleLeft.png");
pimgR = Image.createImage("/roleRight.png");
fireL = Image.createImage("/roleFireL.png");
fireR = Image.createImage("/roleFireR.png");
} catch (Exception e) {
}
pw = 55;
ph = 43;
fireLR = new int[5][10];
System.gc();
}
public void start() {
start = true;
new Thread(this).start();
}
public void stop() {
start = false;
}
protected void paint(Graphics arg0) {
paintScreen();
arg0.drawImage(screen, 0, 0, Graphics.TOP | Graphics.LEFT);
}
private void clear(int color) {
int old = g.getColor();
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(old);
}
// 后台绘制
private void paintScreen() {
clear(0xffffff);
paintPlayer();
paintFire();
}
private void paintFire() {
for (int i = 0; i < fireLR.length; i++) {
if (fireLR[i][0] == 1) {
if (fireLR[i][6] == RIGHT) {
g.drawImage(fireR, fireLR[i][1], fireLR[i][2], 20);
} else {
g.drawImage(fireL, fireLR[i][1] - 28, fireLR[i][2], 20);
}
}
}
}
private void paintPlayer() {
if (dir == RIGHT) {
g.setClip(px, py, pw, ph);
g.drawImage(pimgR, px - frame % 3 * pw, py - frame / 3 * ph, 20);
g.setClip(0, 0, width, height);
} else if (dir == LEFT) {
g.setClip(px, py, pw, ph);
g.drawImage(pimgL, px - frame % 3 * pw, py - frame / 3 * ph, 20);
g.setClip(0, 0, width, height);
}
// switch (state) {
// case STAND:
// break;
// case CROUCH:
// break;
// case JUMP:
// break;
// case RUN:
// break;
// case ATTACH:
// break;
// }
}
private boolean isHaveKey() {
for (int i = 0; i < key.length; i++) {
if (key[i] == 1) {
return true;
}
}
return false;
}
public void keyPressed(int keyCode) {
switch (keyCode) {
case -1:
key[11] = 1;
break;
case -2:
key[12] = 1;
break;
case -3:
key[13] = 1;
break;
case -4:
key[14] = 1;
break;
case -5:
key[15] = 1;
break;
case -6:
key[16] = 1;
break;
case -7:
key[17] = 1;
break;
}
}
public void keyReleased(int keyCode) {
switch (keyCode) {
case -1:
key[11] = 0;
break;
case -2:
key[12] = 0;
break;
case -3:
key[13] = 0;
break;
case -4:
key[14] = 0;
break;
case -5:
key[15] = 0;
break;
case -6:
key[16] = 0;
break;
case -7:
key[17] = 0;
break;
}
}
public void run() {
long st, ed;
while (start) {
// game
st = System.currentTimeMillis();
input();
logic();
repaint();
Runtime run = Runtime.getRuntime();
if (run.freeMemory() < 5000) {
System.gc();
}
ed = System.currentTimeMillis();
try {
if (ed - st < 80) {
Thread.sleep(80 - (ed - st));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 游戏逻辑
private void logic() {
fireControl();
// 玩家控制
switch (state) {
case STAND:
frame = 0;
break;
case CROUCH:
frame = 1;
break;
case JUMP:
frame = 2;
if (frame >= 6 && frame <= 8) {
frame++;
if (frame == 8) {
createFire();
state = STAND;
}
} else {
frame = 6;
}
if (dir == LEFT) {
px -= Data.speed;
}
if (dir == RIGHT) {
px += Data.speed;
}
py -= v0;
v0 -= vg;
// TODO跳跃碰撞的判断
if (Math.abs(v0) > 20) {
state = STAND;
v0 = 20;
}
break;
case RUN:
if (dir == LEFT) {
px -= Data.speed;
}
if (dir == RIGHT) {
px += Data.speed;
}
if (frame >= 3 && frame < 5) {
frame++;
} else {
frame = 3;
}
break;
case ATTACH:
if (frame >= 6 && frame <= 8) {
frame++;
if (frame == 8) {
createFire();
state = STAND;
}
} else {
frame = 6;
}
break;
}
}
private void fireControl() {
for (int i = 0; i < fireLR.length; i++) {
if (fireLR[i][0] == 1) {
if (fireLR[i][6] == LEFT) {
fireLR[i][1] -= fireLR[i][4];
} else {
fireLR[i][1] += fireLR[i][4];
}
fireLR[i][3]--;
if (fireLR[i][3] <= 0) {
fireLR[i][0] = 0;
}
}
}
}
private void createFire() {
for (int i = 0; i < fireLR.length; i++) {
if (fireLR[i][0] == 0) {
fireLR[i][0] = 1;
fireLR[i][1] = px + 28;
fireLR[i][2] = py + 10;
fireLR[i][3] = 20;
fireLR[i][4] = 5;
fireLR[i][6] = dir;
break;
}
}
}
// 用户输入
private void input() {
if (!isHaveKey()) {
if (state == RUN) {
state = STAND;
}
}
if (key[11] == 1) {
switch (state) {
case STAND:
case RUN:
state = JUMP;
break;
case CROUCH:
state = STAND;
break;
}
}
if (key[12] == 1) {
if (state == STAND || state == RUN)
state = CROUCH;
}
if (key[13] == 1) {
dir = LEFT;
switch (state) {
case STAND:
case CROUCH:
state = RUN;
break;
}
} else if (key[14] == 1) {
dir = RIGHT;
switch (state) {
case STAND:
case CROUCH:
state = RUN;
break;
}
}
if (key[15] == 1) {
switch (state) {
case STAND:
case CROUCH:
state = ATTACH;
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -