⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamescreen.java

📁 一个小程序,提供参考
💻 JAVA
字号:
import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class GameScreen extends Canvas implements Runnable {

	Vector bv;

	// 子弹向量

	Vector outCBv;

	int planeCurFrame = -1;

	int planeX, planeY;

	int planeCurState = -1;

	public Image[] imgPlane = new Image[3];

	public Image imgBa;

	public int bFrameWidth, bFrameHeight;

	public final static int B_ROW = 2;

	public final static int B_COL = 3;
	
	public long lastFire;

	public GameScreen() {
		try {
			loadRec();
		} catch (IOException e) {
		}
		bv = new Vector();
		outCBv = new Vector();
		planeX = getWidth() / 2 - imgPlane[0].getWidth() / 2;
		planeY = getHeight() - imgPlane[0].getHeight();
		bFrameWidth = imgBa.getWidth() / 3;
		bFrameHeight = imgBa.getHeight() / 2;

		new Thread(this).start();
	}

	public void loadRec() throws IOException {
		imgPlane[0] = Image.createImage("/role_plane1_1.png");
		imgPlane[1] = Image.createImage("/role_plane1_2.png");
		imgPlane[2] = Image.createImage("/role_plane1_3.png");
		imgBa = Image.createImage("/orb.png");

	}

	public void run() {

		while (true) {
			repaint();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	protected void keyPressed(int keyCode) {
		int key = getGameAction(keyCode);
		switch (key) {
		case UP:
			planeCurState = 0;
			break;
		case DOWN:
			planeCurState = 1;
			break;
		case LEFT:
			planeCurState = 2;
			break;
		case RIGHT:
			planeCurState = 3;
			break;
		case FIRE:
			if(System.currentTimeMillis()-lastFire>500){
			int bx,by;
			bx = planeX + imgPlane[0].getWidth() / 2 - bFrameWidth / 2;
			by = planeY - bFrameHeight;
			int[] t = null;
			if (outCBv.size() > 0) {
				t = (int[]) outCBv.elementAt(0);
				outCBv.removeElementAt(0);
				bv.addElement(t);
				t[0] = bx;
				t[1] = by;
				t[2] = 0;
			} else {
				bv.addElement(new int[] { bx, by, 0 });

			}
			lastFire=System.currentTimeMillis();
		}
	}
		}
	protected void paint(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());
		planeMove();
		drawPlane(g);
		drawB(g);

	}

	protected void keyReleased(int arg0) {
		planeCurState = -1;

	}

	public void planeMove() {
		switch (planeCurState) {
		case 0:
			planeCurFrame = 0;
			planeY -= 2;
			break;
		case 1:
			planeCurFrame = 0;
			planeY += 2;
			break;
		case 2:
			planeCurFrame = 1;
			planeX -= 2;
			break;
		case 3:
			planeCurFrame = 2;
			planeX += 2;
			break;
		default:
			planeCurFrame = 0;

		}
	}

	public void drawPlane(Graphics g) {
		g.drawImage(imgPlane[planeCurFrame], planeX, planeY, 20);
	}

	public void drawB(Graphics g) {
		for (int i = 0; i < bv.size(); i++) {
			int[] t = (int[]) bv.elementAt(i);
			int tx, ty;
			tx = t[2] % B_COL * bFrameWidth;
			ty = t[2] / B_COL * bFrameHeight;
			g.drawRegion(this.imgBa, tx, ty, this.bFrameWidth,
					this.bFrameHeight, Sprite.TRANS_NONE, t[0], t[1], 20);
			t[1] -= 4;
			t[2]++;
			if (t[2] > 5)
				t[2] = 0;
			if (t[1] < -bFrameHeight) {
				bv.removeElementAt(i);
				outCBv.addElement(t);
			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -