📄 menu_midp2canvas.java
字号:
package tians_;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import tool.Tools;
public class Menu_Midp2Canvas extends GameCanvas implements Runnable {
//
static final int WIDTH = 176;
static final int HEIGHT = 208;
int W = 73, H = 89;
int h = 21;
int y1, sd;
// -----------------------
static final byte LOGO1 = 1;
static final byte LOGO2 = 2;
static final byte MENU = 3;
static final byte PLAY = 4;
final int XST;
int YST;
// ---------------------------
byte state;
static final byte _UP = 1;
static final byte _DOWN = 2;
static final byte _LEFT = 3;
static final byte _RIGHT = 4;
// =============================
Graphics g;
MenuMidlet ml;
int x, y, count, speed, d, s, direct;
Image logo, logo1, logo2, npc;
Image image[][], timage;
Random rand;
Vector npcq;
public Menu_Midp2Canvas(MenuMidlet ml) {
super(false);// 是否屏蔽游戏键之外的按键
this.ml = ml;
setFullScreenMode(true);
XST = 0;
YST = 0;
g = getGraphics();// 获得画笔
Player player = new Player();
state = LOGO1;
bult=new int[30][3];
try {
logo = Image.createImage("/yyy/logo1.png");
logo1 = Image.createImage("/yyy/logo2.png");
logo2 = Image.createImage("/yyy/963258.png");
npc = Tools.getFrameIm(Image.createImage("/yyy/npc1.png"), 32 * 2,
32 * 3, 32, 32);
zi=Image.createImage("/yyy/e01.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(this).start();
}
void intNewGame() {
npcq = new Vector();
rand = new Random();
}
// npc敌机----------------------------
void drawNpc(Graphics g) {
for (int i = 0; i < npcq.size(); i++) {
Role temp = (Role) npcq.elementAt(i);
temp.drawNpc(g);
}
}
int n;
void addNpc() {
if (state % 60 == 0) {
Role temp = null;
int rannum = (rand.nextInt() >>> 1) % 2;
switch (rannum) {
case 0:
temp = new Npc1(npc, "Npc" + n++);
break;
case 1:
temp = new Npc2();
break;
}
npcq.addElement(temp);
}
}
void moveNpc() {
for (int i = 0; i < npcq.size(); i++) {
Role temp = (Role) npcq.elementAt(i);
temp.move();
if (temp instanceof Npc1) {
Npc1 npctemp = (Npc1) temp;
if (npctemp.hasRemove()) {
npcq.removeElementAt(i);
}
}
}
}
byte log;
void paint() {
g.setColor(0xffffff);
g.fillRect(0, 0, WIDTH, HEIGHT);
switch (state) {
case LOGO1:
g.drawImage(logo, XST, YST, Graphics.TOP | Graphics.LEFT);
break;
case LOGO2:
g.drawImage(logo1, XST, YST, Graphics.TOP | Graphics.LEFT);
break;
case MENU:
g.drawImage(logo2, XST, YST, Graphics.TOP | Graphics.LEFT);
Image buffer = Image.createImage(105, 26);
Graphics g1 = buffer.getGraphics();
g1.setColor(0x0000ff);
g1.fillArc(0, 0, 105, 26, 0, 360);
buffer = Tools.getAphiImage(buffer, 0, 0xffffffff);
buffer = Tools.getAphiImage(buffer, 70, 0xff0000ff);
g.drawImage(buffer, x, y, Graphics.TOP | Graphics.LEFT);
break;
case PLAY:
// g.setColor(0xffffff);
// g.fillRect(0, 208, getWidth(), getHeight());
// clean();
g.drawImage(logo, XST, YST + y1, Graphics.TOP | Graphics.LEFT);
g.drawImage(logo, XST, YST + y1 - logo.getHeight(), Graphics.TOP
| Graphics.LEFT);
bmove();
clean();
g.drawImage(image[d][s], x, y, Graphics.LEFT | Graphics.TOP);
sd++;
if (sd % 3 == 0) {
if (s++ >= 3) {
s = 0;
}
}
drawbult(g);
break;
}
flushGraphics();
}
void logic() {
switch (state) {
case LOGO1:
case LOGO2:
if (count % (2000 / 33) == 0 && state < 3) {
state++;
x = XST + 36;
y = YST + 38;
}
break;
case MENU:
break;
case PLAY:
move();
// moveAnim();
movebult();
addbult();
break;
}
}
// 子弹 ==================
Image zi;
int bult[][];
// 子弹的移动
void movebult() {
for (int i = 0; i < bult.length; i++) {
if (bult[i][0] == 1) {
bult[i][2] -= 5;
}
if (bult[i][2] <= 0 ) {
bult[i][0] = 0;
}
}
}
// 画子弹
void drawbult(Graphics g) {
for (int i = 0; i < bult.length; i++) {
if (bult[i][0] ==1) {
g.drawImage(zi, bult[i][1], bult[i][2], 0);
}
}
}
// 添加子弹
void addbult() {
if(count++%14==0){
for (int i = 0; i < bult.length; i++) {
if (bult[i][0] == 0) {
bult[i][1] = x + 10;
bult[i][2] = y - 6;
bult[i][0] = 1;
break;
}
}
}
}
protected void keyPressed(int keyCode) {
int keystate = getGameAction(keyCode);
switch (state) {
case LOGO1:
case LOGO2:
state++;
x = XST + 36;
y = YST + 38;
break;
case MENU:
switch (keystate) {
case UP:
y = y - 21 < YST + 38 ? YST + 31 + 24 * 3 : y - 21;
break;
case DOWN:
y = y + 21 > YST + 38 + 24 * 3 ? YST + 38 : y + 21;
break;
case FIRE:
if (y == YST + 38) {
// 新游戏
try {
logo = Image.createImage("/yyy/sky.png");
timage = Image.createImage("/yyy/007...png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x = 70;
y = 165;
d = 0;
s = 0;
speed = 3;
image = getAnim(timage, 0, 0, 1, 4, timage.getWidth() / 4,
timage.getHeight());
state = PLAY;
} else if (y == YST + 31 + 24 * 3) {
ml.exit();
}
break;
}
case PLAY:
switch (keystate) {
case UP:
direct = _UP;
break;
case DOWN:
direct = _DOWN;
break;
case LEFT:
direct = _LEFT;
break;
case RIGHT:
direct = _RIGHT;
break;
}
break;
}
}
protected void keyReleased(int keyCode) {
direct = 0;
s = 0;
}
public void run() {
while (true) {
count++;
logic();
paint();
try {
Thread.sleep(33);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
void move() {
int step = speed;
switch (direct) {
case _UP:
y = (y - step) >= 0 ? y - step : 0;
// - image[d][s].getHeight();
break;
case _DOWN:
y = (y + step) <= 160 ? y + step : 160;// -
// image[d][s].getHeight()
break;
case _LEFT:
x = (x - step) >= 0 ? x - step : 0;
// - image[d][s].getWidth();
break;
case _RIGHT:
x = (x + step) <= 144 ? x + step : 144;// -
// image[d][s].getWidth()
break;
}
}
// void moveAnim() {
// switch (direct) {
// case _UP:
// d = 3;
// if (count % 3 == 0) {
// if (s + 1 == 4) {
// s = 0;
// } else {
// s++;
// }
// }
//
// break;
//
// case _DOWN:
// d = 0;
// if (count % 3 == 0) {
// if (s + 1 == 4) {
// s = 0;
// } else {
// s++;
// }
// }
// break;
// case _LEFT:
// d = 1;
// if (count % 3 == 0) {
// if (s + 1 == 4) {
// s = 0;
// } else {
// s++;
// }
// }
// break;
// case _RIGHT:
// d = 2;
// if (count % 3 == 0) {
// if (s + 1 == 4) {
// s = 0;
// } else {
// s++;
// }
// }
// break;
// }
// }
void bmove() {
y1 = y1 + 1 >= HEIGHT ? 0 : y1 + 1;
}
void clean() {
g.setColor(0xffffff);
g.fillRect(0, 208, getWidth(), getHeight());
}
// ---------------------------
Image getSell(Image res, int x, int y, int width, int height) {
Image temp = Image.createImage(width, height);
Graphics g = temp.getGraphics();
g.setColor(0xff1155cc);
g.fillRect(0, 0, width, height);
int argb[] = new int[width * height];
g.drawImage(res, -x, -y, Graphics.TOP | Graphics.LEFT);
temp.getRGB(argb, 0, width, 0, 0, width, height);
for (int i = 0; i < argb.length; i++) {
if (argb[i] == 0xff1155cc) {
argb[i] = 0x00123456;
}
}
return Image.createRGBImage(argb, width, height, true);
}
Image[][] getAnim(Image res, int x, int y, int h, int l, int width,
int height) {
Image[][] temp = new Image[h][l];
for (int i = 0; i < h; i++) {
for (int j = 0; j < l; j++) {
temp[i][j] = getSell(res, j * width + x, i * height + y, width,
height);
}
}
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -