📄 mygamewithspritecanvas1.java
字号:
import javax.microedition.lcdui.* ;
import javax.microedition.lcdui.game.* ;
public class MyGameWithSpriteCanvas1 extends GameCanvas
implements Runnable
{
private LayerManager lm;
private Sprite c1 ;
public MyGameWithSpriteCanvas1()
{
super(true) ;
lm = new LayerManager();
c1 = createCharacter("/pic/character1.png") ;
int seq[] = new int[]{ 5,5,5,5,0,0,0,0} ;
c1.setFrameSequence(seq);
lm.append(c1);
}
private Sprite createCharacter(String pic)
{
Image img = null ;
try
{
img = Image.createImage(pic);
}catch(Exception exp)
{
System.out.println(exp);
}
return new Sprite(img,64,64) ;
}
boolean conti = true ;
int rate = 100 ;
public void run()
{
long st = 0 ;
long et = 0 ;
Graphics g = getGraphics() ;
while(conti)
{
st = System.currentTimeMillis() ;
input() ;
render(g) ;
et = System.currentTimeMillis() ;
if((et-st)<rate)
{
try
{
Thread.sleep(rate-(et-st));
}catch(Exception exp){}
}
}
}
int x = 10 ;
int y = 10 ;
public void input()
{
int keystate = getKeyStates() ;
if((keystate & UP_PRESSED)!=0)
{
y = y - 2 ;
}else if((keystate & DOWN_PRESSED)!=0)
{
y = y + 2 ;
}else if((keystate & LEFT_PRESSED)!=0)
{
x = x - 2 ;
}else if((keystate & RIGHT_PRESSED)!=0)
{
x = x + 2 ;
}
}
public void render(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
c1.nextFrame();
lm.setViewWindow(0,0,x,y);
lm.paint(g,10,10);
g.setColor(0,0,0);
g.drawRect(10,10,128,128);
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 + -