正文

j2me 书上完整游戏代码2006-10-31 08:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/19833.html

分享到:

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
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; 

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{
 InputStream is=getClass().getResourceAsStream("sword.wav");
 musicPlayer=Manager.createPlayer(is,"audio/x-wav");
 musicPlayer.prefetch();

 is=getClass().getResourceAsStream("sword1.wav");
 celebratePlayer=Manager.createPlayer(is,"audio/x-wav");//通过道路
 celebratePlayer.prefetch();

 is=getClass().getResourceAsStream("sword.wav");
 squishPlayer=Manager.createPlayer(is,"audio/x-wav");
 squishPlayer.prefetch();

 is=getClass().getResourceAsStream("sword2.wav");
 gameoverPlayer=Manager.createPlayer(is,"audio/x-wav");
 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(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;
//关闭声音
 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(){
 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;
 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){
//通过了的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,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());
      }
          }

 

          }
}

阅读(3774) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册