import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
public class HCanvas extends GameCanvas implements Runnable{
private Display display;
private boolean sleeping;
private long frameDelay;
private int inputDelay;
private Random rand;
private Image background;
private Image chickenHead;
private Sprite chickenSprite;
private Sprite[] carSprite=new Sprite[4];
private int[] carYSpeed=new int[4];
private boolean gameOver;
private int numLives;
private int score;
public HCanvas(Display d){
super(true);
display=d;
frameDelay=33;
inputDelay=0;
}
///////////////////////////////////////////////////////////////////////////////////
public void start(){
display.setCurrent(this);
rand=new Random();
gameOver=false;
numLives=3;
score=0;
try{
background=Image.createImage("/001-G_Water01.png");
chickenHead=Image.createImage("/019-Accessory04.png");
chickenSprite=new Sprite(Image.createImage("/048-Fairy01.png"),22,22);
chickenSprite.setPosition(2,77);
carSprite[0]=new Sprite(Image.createImage("/029-Key01.png"));
carSprite[0].setPosition(27,0);
carYSpeed[0]=3;
carSprite[1]=new Sprite(Image.createImage("/032-Item01.png"));
carSprite[1].setPosition(62,0);
carYSpeed[1]=1;
carSprite[2]=new Sprite(Image.createImage("/037-Item06.png"));
carSprite[2].setPosition(93,67);
carYSpeed[2]=-2;
carSprite[3]=new Sprite(Image.createImage("/040-Item09.png"));
carSprite[3].setPosition(128,64 );
carYSpeed[3]=-5;
}
catch(IOException e){
System.out.println("failed loading image!");
}
sleeping=false;
Thread t=new Thread(this);
t.start();
}
////////////////////////////////////////////////////////////////////////////////////
public void stop(){
sleeping=true;
}
public void run(){
Graphics g=getGraphics();
while(!sleeping){
update();
draw(g);
try{
Thread.sleep(frameDelay);
}
catch(InterruptedException id)
{}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
private void update(){
if(gameOver){
int keyState=getKeyStates();
if((keyState&FIRE_PRESSED)!=0){
chickenSprite.setPosition(2,77);
gameOver=false;
score=0;
numLives=3;
}
return;
}
if(++inputDelay>2){
int keyState=getKeyStates();
if((keyState&LEFT_PRESSED)!=0){
chickenSprite.move(-6,0);
chickenSprite.nextFrame();
}
else if((keyState&RIGHT_PRESSED)!=0){
chickenSprite.move(6,0);
chickenSprite.nextFrame();
}
if((keyState&UP_PRESSED)!=0){
chickenSprite.move(0,-6);
chickenSprite.nextFrame();
}
else if((keyState&DOWN_PRESSED)!=0){
chickenSprite.move(0,6);
chickenSprite.nextFrame();
}
checkBounds(chickenSprite,false);
inputDelay=0;
}
if(chickenSprite.getX()>154){
AlertType.WARNING.playSound(display);
chickenSprite.setPosition(2,77);
score+=25;
}
for(int i=0;i<4;i++){
carSprite[i].move(0,carYSpeed[i]);
checkBounds(carSprite[i],true);
if(chickenSprite.collidesWith(carSprite[i],true)){
AlertType.ERROR.playSound(display);
if(--numLives==0)
{
gameOver=true;
}
else
{chickenSprite.setPosition(2,77);}
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
private void draw(Graphics g){
g.drawImage(background,0,0,Graphics.TOP|Graphics.LEFT);
for(int i=0;i<numLives;i++)
g.drawImage(chickenHead,180-((i+1)*8),170,Graphics.TOP|Graphics.LEFT);
chickenSprite.paint(g);
for(int i=0;i<4;i++)
carSprite[i].paint(g);
if(gameOver){
g.setColor(255,255,255);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString("game over",90,40,Graphics.TOP|Graphics.HCENTER);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
g.drawString("你的分数为"+score,90,70,Graphics.TOP|Graphics.HCENTER);
}
flushGraphics();
}
private void checkBounds(Sprite sprite,boolean wrap){
if(wrap){
if(sprite.getX()<-sprite.getWidth())
sprite.setPosition(getWidth(),sprite.getY());
else if(sprite.getX()>getWidth())
sprite.setPosition(-sprite.getWidth(),sprite.getY());
if(sprite.getY()<-sprite.getHeight())
sprite.setPosition(sprite.getX(),getHeight());
else if(sprite.getY()>getHeight())
sprite.setPosition(sprite.getX(),-sprite.getHeight());
}
else {
if(sprite.getX()<0)
sprite.setPosition(0,sprite.getY());
else if(sprite.getX()>(getWidth()-sprite.getWidth()))
sprite.setPosition(getWidth()-sprite.getWidth(),sprite.getY());
if(sprite.getY()<0)
sprite.setPosition(sprite.getX(),0);
else if(sprite.getY()>(getHeight()-sprite.getHeight()))
sprite.setPosition(sprite.getX(),getHeight()-sprite.getHeight());
}
}
}
评论