import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.rms.*;
import java.util.*;
import java.io.*;
//简单的动画canvas by sword2008@程序之巢
//我原本的图片有张超过了200k,传不上来。
public class test2 extends MIDlet{
private Display display;
public test2(){
}
public void startApp(){
new HCanvas(Display.getDisplay(this)).start();}
public void pauseApp(){ }
public void destroyApp(boolean unkowmn){ }
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;
private Player musicPlayer;
private Player celebratePlayer;
private Player squishPlayer;
private Player gameoverPlayer;
private int pass,lever,nextlever=1;//增加游戏难度 ,nextlever记录当前的难度,为难度提高+1
private int []hiScores=new int[5];
public HCanvas(Display d){
super(true);
display=d;
frameDelay=50;
inputDelay=0;
}
///////////////////////////////////////////////////////////////////////////////////
public void start(){
display.setCurrent(this);
rand=new Random();
gameOver=false;
numLives=3;
score=0;
lever=0;
//声音文件初始化
/* try{
InputStream is=getClass().getResourceAsStream("0.mid");
musicPlayer=Manager.createPlayer(is,"audio/midi");
musicPlayer.prefetch();
is=getClass().getResourceAsStream("1.mid");
celebratePlayer=Manager.createPlayer(is,"audio/midi");//通过道路
celebratePlayer.prefetch();
is=getClass().getResourceAsStream("2.mid");
squishPlayer=Manager.createPlayer(is,"audio/midi");
squishPlayer.prefetch();
is=getClass().getResourceAsStream("0.mid");
gameoverPlayer=Manager.createPlayer(is,"audio/midi");
gameoverPlayer.prefetch();
}
catch(IOException ioe){
System.out.println(ioe.toString());
}
catch(MediaException me){
}
*/
//动画图片初始化
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(0,77);
carSprite[0]=new Sprite(Image.createImage("/36.png"));
carSprite[0].setPosition(40,0);
carYSpeed[0]=0;
carSprite[1]=new Sprite(Image.createImage("/37.png"));
carSprite[1].setPosition(75,0);
carYSpeed[1]=-3;
carSprite[2]=new Sprite(Image.createImage("/44.png"));
carSprite[2].setPosition(105,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 checkLever(){
if(lever==nextlever)
{ for(int j=0;j<4;j++)
carYSpeed[j]=carYSpeed[j]+1;
nextlever+=1;
}
}
///////////////////////-------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////
public void stop(){
sleeping=true;
//关闭声音
/* musicPlayer.close();
celebratePlayer.close();
gameoverPlayer.close();
squishPlayer.close();
*/
}
public void run(){
Graphics g=getGraphics();
while(!sleeping){
update();
draw(g);
try{
Thread.sleep(frameDelay);
}
catch(InterruptedException id)
{}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
private void update(){
chickenSprite.nextFrame();//飞啊飞
if(gameOver){
int keyState=getKeyStates();
if((keyState&FIRE_PRESSED)!=0){
//声音也开始了
/* try{
musicPlayer.setMediaTime(0);
musicPlayer.start();
}
catch(MediaException me){}
*/
chickenSprite.setPosition(2,77);
gameOver=false;
score=0;
lever=0;
numLives=3;
}
return;
}
burnmechine();//是否爆机
if(++inputDelay>2){
int keyState=getKeyStates();
if((keyState&LEFT_PRESSED)!=0){
chickenSprite.move(-10,0);
//chickenSprite.nextFrame();
}
else if((keyState&RIGHT_PRESSED)!=0){
chickenSprite.move(10,0);
//chickenSprite.nextFrame();
}
if((keyState&UP_PRESSED)!=0){
chickenSprite.move(0,-10);
//chickenSprite.nextFrame();
}
else if((keyState&DOWN_PRESSED)!=0){
chickenSprite.move(0,10);
//chickenSprite.nextFrame();
}
checkBounds(chickenSprite,false);
inputDelay=0;
}
if(chickenSprite.getX()>100){
if(pass==5)
{pass=0;
lever+=1;
}
else pass+=1;//难度增加
checkLever();//游戏难度升级
//通过了的WAV
/* try{
celebratePlayer.start();
}
catch(MediaException me){}
*/
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)
{
//结束音乐
/* try{
musicPlayer.stop();
gameoverPlayer.start();
}
catch(MediaException me){}
*/
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,20-((i+1)*8),0,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,50,70,Graphics.TOP|Graphics.HCENTER);
g.drawString("制作人:剑2008",50,90,Graphics.TOP|Graphics.HCENTER);
flushGraphics();
try{
Thread.sleep(5000);
}
catch(InterruptedException id)
{}
paintHighScore();//显示分数榜
}
g.drawString(""+score+" 难度"+lever,35,0,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
private void burnmechine(){
if(lever>9)//爆机
{Graphics g=getGraphics();
g.setColor(255,255,0);
g.drawString("好强哦,爆机了!",20,55,Graphics.TOP|Graphics.LEFT);
g.drawString("请联系QQ14923349",20,75,Graphics.TOP|Graphics.LEFT);
flushGraphics();//不调用这个涵数会首先被图画覆盖,爆机留言显示不出来。
}
}
private void paintHighScore(){//分数排行榜,RMS
Graphics g=getGraphics();
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,255,0);
g.drawString("分数龙虎榜",20,0,Graphics.TOP|Graphics.LEFT);
g.drawString("http://blog.programfan.com",5,20,Graphics.TOP|Graphics.LEFT);
g.drawString("/blog.asp?author=qq14923349",5,30,Graphics.TOP|Graphics.LEFT);
g.setColor(255,255,255);
readHiScores();
updateHiScore();
writeHiScores();
for(int i=0;i<5;i++)
{
g.drawString("第 "+(i+1)+"名: "+hiScores[i],10,60+i*10,Graphics.TOP|Graphics.LEFT);
}
flushGraphics();
try{
Thread.sleep(5000);
}
catch(InterruptedException id)
{}
}
private void readHiScores()//读出记录
{
RecordStore rs=null;
try{
rs=RecordStore.openRecordStore("HiScores",false);
}
catch(Exception e){}
if( rs!=null ){
try{
int len;
byte[] recordData=new byte[8];
for(int i=1;i<=rs.getNumRecords();i++)
{
if(rs.getRecordSize(i)>recordData.length)
recordData =new byte[rs.getRecordSize(i)];
len=rs.getRecord(i,recordData,0);
hiScores[i-1]=(Integer.parseInt(new String(recordData,0,len)));
}
}
catch(Exception e){
System.err.println("Failed reading hi scores!");
}
try{
rs.closeRecordStore();
}
catch(Exception e){
System.err.println("Failed closing hi score record store!");
}
}
else{
for(int i=0;i<5;i++)
hiScores[i]=0;
}
}
private void writeHiScores()//添加记录
{ try{//删除前面的记录
RecordStore.deleteRecordStore("HiScores");
}
catch(Exception e){}
RecordStore rs=null;
try{
rs=RecordStore.openRecordStore("HiScores",true);
}
catch(Exception e){
System.err.println("failed!!");
}
for(int i=0;i<5;i++)
{ if(hiScores[i]>(int)score)
hiScores[i]=(int)score;}
for(int i=0;i<5;i++)
{ if(hiScores[i]>(int)score)
hiScores[i]=(int)score;
byte[] recordData=Integer.toString(hiScores[i]).getBytes();
try{
rs.addRecord(recordData,0,recordData.length);
}
catch(Exception e){
System.err.println("Failed writing hi scores!");
}
}
try{//关闭记录存储
rs.closeRecordStore();
}
catch(Exception e){
System.err.println("Failed closing hi score record store!");
}
}
private void updateHiScore(){
int i;
for(i=0;i<5;i++)
if(score>hiScores[i])
break;
if(i<5)
{
for(int j=4;j>i;j--)
{
hiScores[j]=hiScores[j-1];
}
hiScores[i]=score;
}
}
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());
}
}
}
}
评论