escapeeCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:7k
源码类别:
J2ME
开发平台:
Java
- import java.util.Random;
- import java.util.Vector;
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import javax.microedition.media.*;
- import java.io.*;
- class escapeeCanvas extends GameCanvas implements Runnable
- {
- // shared direction constants
- static final int BULLETS_NUM = 50;
- static final int UP = 0;
- static final int LEFT = 1;
- static final int DOWN = 2;
- static final int RIGHT = 3;
- private TiledLayer background;
- private Escapee escapee;
- private Bullets bullets;
- private Sprite explosion;
- private int CanvasWidth = getWidth();
- private int CanvasHeight = getHeight();
- private Image image;
- private static final int MILLIS_PER_TICK = 50;
- private final escapeeMIDlet midlet;
- private boolean isGameOver = false;
- private boolean isCollidesWith = false;
- private int explosionCnt = 4;
- private final Graphics g;
- private long gameDuration = 0;
- private long startTime;
- private volatile Thread animationThread = null;
- escapeeCanvas(escapeeMIDlet midlet)
- {
- super(true); // suppress key events for game keys
- this.midlet = midlet;
- setFullScreenMode(false);
- g = getGraphics();
- load();
- init();
- }
- public void keyPressed(int keyCode)
- {
- // The constructor suppresses key events for game keys, so we'll
- // only get key events for non-game keys. The number keys, * & #
- // have positive keyCodes, so negative keyCodes mean non-game
- // special keys like soft-keys. We'll use key-presses on special
- // keys to take us to the menu.
- if (keyCode < 0)
- {
- stop();
- midlet.GameCanvasMenu();
- }
- }
- private void load()
- {
- image = midlet.createImage("/back_water.png");
- int backColumns = CanvasWidth/image.getWidth()+1;
- int backRows = CanvasHeight/image.getHeight()+1;
- background = new TiledLayer(backColumns,backRows,image,image.getWidth(),image.getHeight());
- int x,y;
- for (int i = 0; i < backColumns*backRows; i++)
- {
- x=i%backColumns;
- y=i/backColumns;
- background.setCell(x,y,1);
- }
- image = midlet.createImage("/Escapee.png");
- escapee = new Escapee(image,image.getWidth()/3,image.getHeight());
- escapee.setCanvasSize(CanvasWidth,CanvasHeight);
- image = midlet.createImage("/bullet.png");
- bullets = new Bullets(image,image.getWidth(),image.getHeight());
- bullets.setBulletsNum(BULLETS_NUM);
- bullets.setCanvasSize(CanvasWidth,CanvasHeight);
- image = midlet.createImage("/explosion.png");
- explosion = new Sprite(image,image.getWidth()/4,image.getHeight());
- image = midlet.createImage("/gameover.png");
- }
- public void init(){
- escapee.setPosition(CanvasWidth/2,CanvasHeight/2);
- bullets.initBullets();
- explosion.setVisible(false);
- explosion.setFrame(0);
- explosionCnt = 4;
- isCollidesWith = false;
- isGameOver = false;
- gameDuration = 0;
- escapee.setAlive(true);
- }
- public synchronized void start()
- {
- animationThread = new Thread(this);
- animationThread.start();
- startTime = System.currentTimeMillis() - gameDuration;
- }
- public synchronized void stop()
- {
- gameDuration = System.currentTimeMillis() - startTime;
- animationThread = null;
- }
- public void run()
- {
- Thread currentThread = Thread.currentThread();
- try
- {
- // This ends when animationThread is set to null, or when
- // it is subsequently set to a new thread; either way, the
- // current thread should terminate
- while (currentThread == animationThread)
- {
- long startTime = System.currentTimeMillis();
- // Don't advance game or draw if canvas is covered by
- // a system screen.
- if (isShown())
- {
- input();
- tick();
- draw();
- }
- long timeTaken = System.currentTimeMillis() - startTime;
- if (timeTaken < MILLIS_PER_TICK)
- {
- synchronized (this)
- {
- wait(MILLIS_PER_TICK - timeTaken);
- }
- }
- else
- {
- currentThread.yield();
- }
- }
- }
- catch (InterruptedException e)
- {
- // won't be thrown
- }
- }
- private void tick()
- {
- // If player presses two or more direction buttons, we ignore them
- // all. But pressing fire is independent. The code below also ignores
- // direction buttons if GAME_A..GAME_D are pressed.
- escapee.tick();
- if(!isCollidesWith)bullets.tick();
- if(isCollidesWith&&(explosionCnt!=0)){
- explosion.setFrame((explosionCnt-1));
- explosionCnt--;
- if(explosionCnt == 0)isGameOver = true;
- }
- if(bullets.collidesWith(escapee)){
- isCollidesWith = true;
- escapee.setAlive(false);
- SoundEffects.getInstance().startBlastSound();
- explosion.setPosition(escapee.getRefPixelX()-explosion.getWidth()/2,
- escapee.getRefPixelY()-explosion.getHeight()/2);
- explosion.setVisible(true);
- }
- if(isGameOver){
- long time = (System.currentTimeMillis() - startTime);
- midlet.GameCanvasGameOver(time,BULLETS_NUM);
- }
- }
- private void input() {
- // If player presses two or more direction buttons, we ignore them
- // all. But pressing fire is independent. The code below also ignores
- // direction buttons if GAME_A..GAME_D are pressed.
- int keyStates = getKeyStates();
- if(escapee.isAlive()){
- if ((keyStates & LEFT_PRESSED) != 0) escapee.move(LEFT);
- else if ((keyStates & RIGHT_PRESSED) != 0) escapee.move(RIGHT);
- else if ((keyStates & UP_PRESSED) != 0) escapee.move(UP);
- else if ((keyStates & DOWN_PRESSED) != 0) escapee.move(DOWN);
- }
- }
- void vibrate(int millis)
- {
- midlet.vibrate(millis);
- }
- // draw game
- private void draw()
- {
- background.paint(g);
- escapee.paint(g);
- bullets.draw(g);
- explosion.paint(g);
- if(isCollidesWith&&!isGameOver)g.drawImage(image,
- CanvasWidth/2,
- CanvasHeight/2,
- g.VCENTER|g.HCENTER);
- flushGraphics();
- }
- }