日期:2014-05-20 浏览次数:20754 次
public class Game extends GameCanvas implements Runnable{
private Sprite s,p;
private LayerManager lm;
private Image imgbg;
private Image imgp;
private int px,py;
public Graphics offg;
public int[] FRAME_SEQUENCE_DOWN={0, 1, 2, 3};
public int[] FRAME_SEQUENCE_UP={12,13,14,15};
public int[] FRAME_SEQUENCE_LEFT={4, 5, 6, 7};
public int[] FRAME_SEQUENCE_RIGHT={8,9,10,11 };
public Game() {
super(true);
px=12;
py=23;
lm=new LayerManager();
try {
imgbg=Image.createImage("/bg.png");
imgp=Image.createImage("/frames.png");
} catch (IOException e) {}
s=new Sprite(imgbg);
p=new Sprite(imgp,24,36);
lm.append(p);
lm.append(s);
lm.setViewWindow(0, 0, getWidth(), getHeight());
offg=this.getGraphics();
(new Thread(this)).start();
}
public void run() {
while(true){
input();
offg.setColor(255);
offg.fillRect(0,0,getWidth(),getHeight());
lm.paint(offg,0, 0);
paint();
try {
Thread.currentThread();
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(){
this.flushGraphics();
}
public void input(){
int k=this.getKeyStates();
System.out.println("k="+k);
if((k&GameCanvas.DOWN_PRESSED)>0){
int[] FRAME_SEQUENCE_DOWN={0, 1, 2};
p.setFrameSequence(FRAME_SEQUENCE_DOWN);
p.nextFrame();
System.out.println(p.getFrame());
p.move(0, 4);
}
if((k&GameCanvas.UP_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_UP);
p.nextFrame();
p.move(0, -4);
}
if((k&GameCanvas.LEFT_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_LEFT);
p.nextFrame();
p.move(-4, 0);
}
if((k&GameCanvas.RIGHT_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_RIGHT);
p.nextFrame();
p.move(4,0);
}
if((k&GameCanvas.FIRE_PRESSED)>0){
boolean vis=p.isVisible();
p.setVisible(!vis);
}
}
}