gamescreen.java

来自「一个小程序,提供参考」· Java 代码 · 共 79 行

JAVA
79
字号
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;


public class GameScreen extends Canvas implements Runnable {

	/**主角坐标*/
	int x,y;
	/**目标坐标*/
	int tx,ty;
	
	/**是否碰撞*/
	boolean isColl;
	
	int width=40;
	boolean tempb;
	public GameScreen(){
		tx=getWidth()-40;
		ty=getHeight()-40;
		new Thread(this).start();
	}
	protected void paint(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(0, 0, getWidth(), getHeight());
		isColle();
		if(isColl){
			tempb=!tempb;
			if(tempb)
				g.setColor(0xFF0000);
			else
				g.setColor(0x00FF00);
			g.fillRect(0, 0, getWidth(), getHeight());
			g.setColor(0x000000);
			g.drawString("Game over", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.HCENTER);
		}
		else{
			
			g.setColor(0xFF0000);
			g.fillRect(x, y, width, width);
			g.setColor(0x00FF00);
			g.fillRect(tx, ty, width, width);
		}

	}

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

	}
	public void isColle(){
		if(x+40>=tx&&y+40>=ty)
			isColl=true;
	}
	protected void keyPressed(int keyCode) {
		int key=getGameAction(keyCode);
		switch(key){
		case UP:
			y-=5;
			break;
		case DOWN:
			y+=5;
			break;
		case LEFT:
			x-=5;
			break;
		case RIGHT:
			x+=5;
			break;
		}
	}

}

⌨️ 快捷键说明

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