`

我的第一个原创J2ME游戏:砸飞机。

阅读更多

呵呵,前段时间在网上认识了个从业J2ME开发的前辈,觉得他好是客气,人应该很好很好。为了能够在他手下兼职,我和他提出了一个星期期限,就是在一个星期内从零开始到会开发一款简单的J2ME 2D游戏。哈哈哈,从来没有那么兴奋过,OK,在一个星期后,我发给了他这样的一个游戏:

本来想开发一款传统的打飞机游戏的,可是不知道该如何控制众多随机敌机从天而降的效果出来,所以干脆将错就错,用随机出现的一块陨石来砸也可以移动的飞机。哈哈哈,很烂吧。不过也实现了哈哈哈。心想,谁会玩我这款游戏呢?哈哈哈

本游戏用到的技术有:

游戏背景的布局方法;精灵类对象的移动方法;碰撞检测以及精灵可见或不可见的实现;和游戏屏幕滚动实现

以下是源代码:

BombCanvas.java

package bomb;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.Random;

public class BombCanvas extends GameCanvas implements Runnable {


private Image bkgImage,planeImage_1,shitou_1,shitou_2,shitou_3,shitou_4,bombImage;

private Graphics g;
private TiledLayer background;
private LayerManager layManager;

private int screenWidth = 0; //屏幕宽度

private int screenHeight = 0; //屏幕高度
private int drawX = 0; //要绘制的X坐标
private int drawY = 0; //要绘制的Y坐标

private int dx = 0;

private Sprite plane_1,shi_1,shi_2,shi_3,shi_4,bomb,bg;


    private int moveLR = 60;
private int moveUD = 180;
private int down = 60;

private int count1 = 0;
private int count2 = 0;
private int count3 = 0;
private int count4 = 0;

private Random random;

private byte[][] backgroundMap = { {1,2,3,4,5,6},
    {7,8,9,10,11,12},
    {13,14,15,16,17,18},
    {19,20,21,22,23,24},
    {25,26,27,28,29,30},
    {31,32,33,34,35,36},
    {1,2,3,4,5,6},
    {7,8,9,10,11,12},
    {13,14,15,16,17,18},
    {19,20,21,22,23,24},
    {25,26,27,28,29,30},
    {31,32,33,34,35,36}};

protected BombCanvas() {
   super(true);
  
   screenHeight = getHeight();
   screenWidth = getWidth();
  

   try {
    //图片大小为360*360,为了简单没有设置自己的地图格式
    bkgImage = Image.createImage("/backg.png");
    //图片大小为80*64
    planeImage_1 = Image.createImage("/plane1.png");
    //图片大小为19*15
    shitou_1 = Image.createImage("/shitou.png");
    shitou_2 = Image.createImage("/shitou.png");
    shitou_3 = Image.createImage("/shitou.png");
    shitou_4 = Image.createImage("/shitou.png");
    bombImage = Image.createImage("/bomb.png");

   } catch (Exception e) {
    System.out.print("加载不了图片");

   }


   int i,j;
  
   g = this.getGraphics();
  
   //绘画图层1,共6列12行
   background = new TiledLayer(6,12,bkgImage,60,60);
   for(i=0;i<12;i++)   
    for(j=0;j<6;j++){
     background.setCell(j,i,backgroundMap[i][j]);
    }
  
  
  
   plane_1= new Sprite(planeImage_1,19,15);
   shi_1= new Sprite(shitou_1,16,16);
   shi_2= new Sprite(shitou_1,16,16);
   shi_3= new Sprite(shitou_1,16,16);
   shi_4= new Sprite(shitou_1,16,16);
   bomb = new Sprite(bombImage,256,128);
  
    
  
   layManager = new LayerManager();
   //图层2现在在最前面
   layManager.append(plane_1);
   //大飞机在第2层
   layManager.append(shi_1);  
   //爆炸在第3层
   layManager.append(bomb);
   //没有发生碰撞,所以不可视
   bomb.setVisible(false);
   //添加背景图层
   layManager.append(background);
  

   plane_1.setPosition(60,180);
   shi_1.setPosition(80,0);
   shi_2.setPosition(60,0);
   shi_3.setPosition(40,0);
   shi_4.setPosition(10,0);
  
   //绘制在屏幕撒上
   layManager.paint(g,0,0); //这一步可以把之前所有的图片都添在屏幕上
   flushGraphics();
  
   Thread thread = new Thread(this);
   thread.start();

}

public void keyPressed() {  
   int x, y;
   int keyState = getKeyStates();

   //判断上下左右键是否被按下
   if ((keyState & UP_PRESSED) != 0) {
   
     moveUD = moveUD - 2;
     drawY = drawY +2;
     
   }
   if ((keyState & DOWN_PRESSED) != 0) {
   
     moveUD = moveUD + 2;
     drawY = drawY - 2;  

   }
   if ((keyState & LEFT_PRESSED) != 0) {
   
    moveLR = moveLR - 2;
    
   
   }
   if ((keyState & RIGHT_PRESSED) != 0) {
   
    moveLR = moveLR + 2;  

   }
    if ((keyState & FIRE_PRESSED) != 0) {
   
     random = new Random();
          count1 = (int)Math.abs(random.nextInt())%300;
          count2 = (int)Math.abs(random.nextInt())%200;
          count3 = (int)Math.abs(random.nextInt())%320;
          count4 = (int)Math.abs(random.nextInt())%350;
   }
  
  
}
  
public void run() {
   //无限循环检测键盘的输入
   while (true) {
    keyPressed();
    draw();
    try {
     Thread.sleep(50);
    } catch (InterruptedException ie) {
    }
   }
}
public void draw(){
   //移动背景 屏幕的移动效果一般我们是通过改变View Window的的位置来实现的,
   //比如你想屏幕向右移动,那么你要调整View Window的x坐标增加相应的数值,
   //如果想屏幕向左移动,那么调整View Window的x坐标减少相应的数值。
   //上下移动原理一样
   layManager.setViewWindow(drawX,drawY,screenWidth,screenHeight);
  
   shi_1.setPosition(drawX+count1,++down);
   shi_2.setPosition(drawX+count2,++down);
   shi_3.setPosition(drawX+count3,++down);
   shi_4.setPosition(drawX+count4,++down);
   if(down >= screenHeight)
   {
    down = 10;
   }
   plane_1.setPosition(moveLR,moveUD);
  
   //使用象素碰撞检查,如果发生碰撞则显示爆炸图片
   if(plane_1.collidesWith(shi_1,true)){
    bomb.setPosition(drawX+10,drawY+180);
    bomb.setVisible(true);
    plane_1.setVisible(false);
    shi_1.setVisible(false);
    shi_2.setVisible(false);
    shi_3.setVisible(false);
    shi_4.setVisible(false);
   }
  
   //绘制在屏幕撒上
   layManager.paint(g,0,0);  
   flushGraphics();
  
}
}

 

BombDemo.java:

 

package bomb;


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class BombDemo extends MIDlet implements CommandListener{

private Command exitCommand;
private BombCanvas bombCan;
public BombDemo() {  
   exitCommand = new Command("Exit", Command.EXIT, 1);
   bombCan = new BombCanvas();
   bombCan.addCommand(exitCommand);
   bombCan.setCommandListener(this);
  
    
}

protected void startApp() {
   Display.getDisplay(this).setCurrent(bombCan);
}

protected void pauseApp() {
}

protected void destroyApp(boolean d) {
}

public void commandAction(Command c, Displayable d) {
   if (c == exitCommand) {   
    destroyApp(false);   
    notifyDestroyed();
   }
}

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics