📄 gamescreen.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -