📄 mygamewithcollicanvas.java
字号:
import javax.microedition.lcdui.* ;
import javax.microedition.lcdui.game.* ;
public class MyGameWithColliCanvas extends GameCanvas
implements Runnable
{
private LayerManager lm;
private TankCanFireSprite c1 ;
private BadSprite bad ;
public static int Score = 10;
public MyGameWithColliCanvas()
{
super(true) ;
lm = new LayerManager();
c1 = createTank("/pic/tank.png") ;
c1.setPosition(80,100);
lm.append(c1);
bad = createBad("/pic/bad.png") ;
bad.setPosition(20,0);
lm.append(bad);
}
private TankCanFireSprite createTank(String pic)
{
Image img = null ;
try
{
img = Image.createImage(pic);
}catch(Exception exp)
{
System.out.println(exp);
}
return new TankCanFireSprite(img,32,32,128,128) ;
}
private BadSprite createBad(String pic)
{
Image img = null ;
try
{
img = Image.createImage(pic);
}catch(Exception exp)
{
System.out.println(exp);
}
return new BadSprite(img,64,64,128,128) ;
}
boolean conti = true ;
int rate = 100 ;
public void run()
{
long st = 0 ;
long et = 0 ;
Graphics g = getGraphics() ;
while(conti&&Score!=0)
{
st = System.currentTimeMillis() ;
input() ;
bad.AI();
if(collidect())
{
render(g) ;
break ;
}
render(g) ;
et = System.currentTimeMillis() ;
if((et-st)<rate)
{
try
{
Thread.sleep(rate-(et-st));
}catch(Exception exp){}
}
}
if(Score == 0)
render(g);
}
public boolean collidect()
{
if(bad.collidesWith(c1,false))
{
Image good = null ;
try
{
good = Image.createImage("/pic/good.png") ;
Score = Score - 2;
}catch(Exception exp){}
bad.setImage(good,64,64);
}
return bad.collidesWith(c1,false) ;
}
public void input()
{
int keystate = getKeyStates() ;
if((keystate & UP_PRESSED)!=0)
{
c1.moveUp() ;
}if((keystate & DOWN_PRESSED)!=0)
{
c1.moveDown();
}if((keystate & LEFT_PRESSED)!=0)
{
c1.moveLeft();
}if((keystate & RIGHT_PRESSED)!=0)
{
c1.moveRight();
}
}
public void render(Graphics g)
{
if(Score!=0)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
lm.paint(g,10,10);
g.setColor(0,0,0);
g.drawRect(10,10,128,128);
g.drawString("Score : "+Score,10,160,20);
flushGraphics() ;
}
else
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
lm.paint(g,10,10);
g.setColor(0,0,0);
g.drawRect(10,10,128,128);
g.drawString("Game Over! ",10,160,20);
flushGraphics() ;
}
}
public void start()
{
Thread t = new Thread(this) ;
t.start();
}
public void exit()
{
conti = false ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -