Midlet:
- package STF;
- import javax.microedition.lcdui.Display;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class Midlet extends MIDlet {
- Display display;
- MainCanvas main;
- public Midlet() {
- display = Display.getDisplay(this);
- main = new MainCanvas(this);
- }
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- display.setCurrent(main);
- }
- }
MainCanvas:
- package STF;
- import java.io.IOException;
- import java.util.Random;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.GameCanvas;
- import javax.microedition.lcdui.game.LayerManager;
- import javax.microedition.lcdui.game.Sprite;
- public class MainCanvas extends GameCanvas implements Runnable {
- Image imgTank,imgE,imgMenu,imgLogo;//资源图片
- int[] oldRgb;//实现进入时渐变效果的时的数组
- int alpha = 0;//透明度
- boolean isOK=false;//记录是否载入完成
- static boolean isDie=false;//记录我方坦克是否死亡
- static boolean isETDie=false;//记录敌方坦克是否死亡
- Map map;//创建地图对象
- EnemyTank ets[]=new EnemyTank[10];//创建一个长度为10的敌方坦克数组
- MainTank mt;//创建一个我方坦克
- LayerManager lm;//创建图层管理对象
- static int score=0;//得分
- static int level=1;//级别
- static int lifeCount=3;//我方坦克生命数
- static int enemyCount=1;//敌方坦克记数器,用于记录死亡坦克的数目
- int waitCount=0;//显示信息的等待时间
- int controller=0;//开始菜单目录选择控制器
- Random random;
- //游戏的几种状态
- static final int LOADING=0;//载入游戏
- static final int WAITSTART=1;//等待开始
- static final int PLAYING=2;//游戏进行中
- static final int NEXTLEVEL=3;//提示进入下一关
- static final int GAMEOVER=4;//游戏线束
- static int gameState=LOADING;//记录游戏状态,初值为载入游戏
- Midlet m;//创建一个Midlet对象,用于实现退出游戏操作
- //构造函数,传入参数一个Midlet对象
- protected MainCanvas(Midlet m) {
- super(true);
- random=new Random();
- this.m=m;
- map=new Map();//初始化地图对象
- map.loadMap();//载入地图
- lm=new LayerManager();//初始化图层管理器
- //加载图片资源
- try {
- imgTank=Image.createImage("/tank.png");
- imgE = Image.createImage("/enemytank.png");
- imgMenu = Image.createImage("/menu.png");
- imgLogo = Image.createImage("/tank_logo.png");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //保存图像信息,用于实现渐变效果
- oldRgb = new int[imgLogo.getWidth() * imgLogo.getHeight()];
- imgLogo.getRGB(oldRgb, 0, imgLogo.getWidth(), 0, 0, imgLogo.getWidth(), imgLogo
- .getHeight());
- mt=new MainTank(imgTank,13,13); //初始化我方坦克对象
- mt.setPosition(16*3+13, 16*13+2);//设置我方坦克位置
- mt.setTransform(Sprite.TRANS_MIRROR_ROT90);//设置坦克朝向
- //初始化敌方坦克数组中的所有对象
- for(int i=0;i<ets.length;i++ ){
- ets[i]=new EnemyTank(imgE,13,13);
- ets[i].setVisible(false);
- }
- //在图层管理器中依次加入对象
- lm.append(map.ty_grass);//最上层为草地
- lm.append(mt);
- //lm.append(et);
- for(int i=0;i<ets.length;i++ ){
- lm.append(ets[i]);//敌方坦克
- }
- for(int i=0;i<ets.length;i++ ){
- for(int j=0;j<ets[i].bullets.length;j++ ){
- lm.append(ets[i].bullets[j]);//所有子弹
- lm.append(ets[i].bullets[j].s_exp);//所有敌方坦克子弹爆炸精灵
- }
- }
- for(int i=0;i<mt.bullets.length;i++)
- {
- lm.append(mt.bullets[i]);//我方坦克子弹
- lm.append(mt.bullets[i].s_exp);//我方坦克子弹爆炸精灵
- }
- lm.append(map.ty_oldhome);//老窝
- lm.append(map.ty_brick);//砖块
- lm.append(map.ty_shuini);//水泥
- lm.append(map.ty_water);//水泽
- new Thread(this).start();//启动线程
- }
- //run方法,根据游戏状态执行不同的"按键输入,逻辑处理,绘制图像"操作
- public void run() {
- while (true) {
- switch(gameState){
- case LOADING:
- loadingInput();
- loadingLogic();
- loadingPaint();
- break;
- case WAITSTART:
- waitStartInput();
- waitStartLogic();
- waitStartPaint();
- break;
- case PLAYING:
- input();
- logic();
- paint();
- break;
- case NEXTLEVEL:
- nextLevelInput();
- nextLevelLogic();
- nextLevelPaint();
- break;
- case GAMEOVER:
- gameOverInput();
- gameOverLogic();
- gameOverPaint();
- break;
- }
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- //游戏结束的绘制方法,显示相关信息
- private void gameOverPaint() {
- Graphics g = getGraphics();
- g.setColor(0);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(0x00ffffff);
- g.drawString("游戏结束!", this.getWidth() / 2, this.getHeight() / 2-20,
- Graphics.HCENTER | Graphics.TOP);
- g.drawString("总得分: "+score+" 分", this.getWidth() / 2, this.getHeight() / 2+20,
- Graphics.HCENTER | Graphics.TOP);
- g.drawString("按任意键返回", this.getWidth() / 2, this.getHeight() / 2+40,
- Graphics.HCENTER | Graphics.TOP);
- flushGraphics();
- }
- //开始菜单选择
- private void loadingInput() {
- int k=this.getKeyStates();
- if ((k & GameCanvas.UP_PRESSED) != 0){
- controller--;
- //循环效果
- if(controller<0){
- controller=4;
- }
- }
- if ((k & GameCanvas.DOWN_PRESSED) != 0){
- controller++;
- //循环效果
- if(controller>4){
- controller=0;
- }
- }
- if((k & GameCanvas.FIRE_PRESSED) != 0){
- switch(controller){
- case 0://NewGame
- gameState=NEXTLEVEL;
- break;
- case 4://Exit
- m.notifyDestroyed();
- break;
- }
- }
- }
- //实现图片的渐变效果
- private void loadingLogic() {
- for (int i = 0; i < oldRgb.length; i++) {
- oldRgb[i] = (oldRgb[i] & 0x00ffffff) | (alpha << 24);
- }
- if(alpha<250){
- alpha += 10;
- if(alpha==250){
- isOK=true;//图片完全显示,载入完毕
- }
- }
- }
- private void loadingPaint() {
- Graphics g = getGraphics();
- g.setColor(0);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.drawRGB(oldRgb, 0, imgLogo.getWidth(), (this.getWidth()-imgLogo.getWidth())/2, 40, imgLogo.getWidth(), imgLogo
- .getHeight(), true);
- if(!isOK){
- g.setColor(0x00ffffff);
- g.drawString("游戏载入中……", this.getWidth() / 2, this.getHeight() / 2+imgLogo.getHeight(),
- Graphics.HCENTER | Graphics.TOP);
- }else{
- //绘制菜单图片
- g.drawImage(imgMenu, (this.getWidth()-imgMenu.getWidth())/2, 80+imgLogo.getHeight(), 20);
- //根据控制器绘制选择箭头的显示
- switch(controller){
- case 0:
- g.setColor(0x00ffffff);
- g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15, 85+imgLogo.getHeight(), 10, 10, 270, 180);
- break;
- case 1:
- g.setColor(0x00ffffff);
- g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15, 103+imgLogo.getHeight(), 10, 10, 270, 180);
- break;
- case 2:
- g.setColor(0x00ffffff);
- g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15, 121+imgLogo.getHeight(), 10, 10, 270, 180);
- break;
- case 3:
- g.setColor(0x00ffffff);
- g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15, 139+imgLogo.getHeight(), 10, 10, 270, 180);
- break;
- case 4:
- g.setColor(0x00ffffff);
- g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15, 157+imgLogo.getHeight(), 10, 10, 270, 180);
- break;
- }
- }
- flushGraphics();
- }
- private void waitStartInput() {
- }
- private void waitStartLogic() {
- }
- private void waitStartPaint() {
- flushGraphics();
- }
- private void nextLevelPaint() {
- Graphics g = getGraphics();
- g.setColor(0);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(0x00ffffff);
- g.drawString("第 "+level+" 关", this.getWidth() / 2, this.getHeight() / 2-20,
- Graphics.HCENTER | Graphics.TOP);
- g.drawString("目标:"+enemyCount+"个坦克", this.getWidth() / 2, this.getHeight() / 2+20,
- Graphics.HCENTER | Graphics.TOP);
- flushGraphics();
- }
- private void nextLevelLogic() {
- mt.setPosition(16*3+13, 16*13+2);
- mt.setTransform(Sprite.TRANS_MIRROR_ROT90);
- waitCount++;
- if(waitCount==20){
- waitCount=0;
- gameState=PLAYING;
- for(int i=0;i<mt.bullets.length;i++){
- mt.bullets[i].setVisible(false);
- mt.bullets[i].s_exp.setVisible(false);
- }
- for(int i=0;i<ets.length;i++){
- ets[i].setVisible(false);
- for(int j=0;j<ets[i].bullets.length;j++){
- ets[i].bullets[j].setVisible(false);
- ets[i].bullets[j].s_exp.setVisible(false);
- }
- }
- map.loadMap();
- createET(32,20);
- createET(50,20);
- createET(100,20);
- }
- }
- private void nextLevelInput() {
- }
- private void gameOverLogic() {
- for(int i=0;i<ets.length;i++){
- ets[i].setVisible(false);
- for(int j=0;j<ets[i].bullets.length;j++){
- ets[i].bullets[j].setVisible(false);
- ets[i].bullets[j].s_exp.setVisible(false);
- }
- }
- }
- private void gameOverInput() {
- int k=this.getKeyStates();
- if(k!=0){
- gameState = LOADING;
- init();
- }
- }
- private void init() {
- score=0;
- level=1;
- lifeCount=3;
- enemyCount=1;
- waitCount=0;
- controller=0;
- isDie=false;
- map.loadMap();
- mt.setVisible(true);
- mt.setPosition(16*3+13, 16*13+2);
- mt.setTransform(Sprite.TRANS_MIRROR_ROT90);
- for(int i=0;i<mt.bullets.length;i++){
- mt.bullets[i].setVisible(false);
- mt.bullets[i].s_exp.setVisible(false);
- }
- }
- private void paint()
- {
- Graphics g = getGraphics();
- g.setColor(0);
- g.fillRect(0, 0, getWidth(), getHeight());
- lm.paint(g, 23, 23);
- g.setColor(0x00ffffff);
- g.drawString("第 "+level+" 关", 40, 10,
- Graphics.HCENTER | Graphics.TOP);
- g.drawString("得分: "+score, this.getWidth()-80,10,
- Graphics.HCENTER | Graphics.TOP);
- g.drawString("生命: "+lifeCount+" ×", 16*3,this.getHeight()-20,
- Graphics.HCENTER | Graphics.TOP);
- g.setColor(0x00ffffff);
- g.drawRegion(imgTank, 0, 13, 13, 13, Sprite.TRANS_MIRROR_ROT90, 16*5, this.getHeight()-18, 0);
- g.drawString("任务: "+enemyCount+" ×", 16*11,this.getHeight()-20,
- Graphics.HCENTER | Graphics.TOP);
- g.setColor(0x00ffffff);
- g.drawRegion(imgE, 0, 13, 13, 13, Sprite.TRANS_MIRROR_ROT90, 16*13, this.getHeight()-18, 0);
- flushGraphics();
- }
- private void input()
- {
- int k = getKeyStates();
- mt.input(k);
- }
- private void logic()
- {
- if(isDie){
- mt.setVisible(true);
- mt.setPosition(16*3+13, 16*13+2);
- mt.setTransform(Sprite.TRANS_MIRROR_ROT90);
- isDie=false;
- }
- if(isETDie){
- createET(32*(random.nextInt(4)+1),20);
- isETDie=false;
- }
- if(enemyCount==0){
- gameState=NEXTLEVEL;
- level++;
- enemyCount=(level-1)*5;
- }
- mt.checkCollidesWith(map);
- for(int i=0;i<ets.length;i++){
- if(ets[i].isVisible()&&mt.collidesWith(ets[i], true)){
- mt.move(-mt.lastX, -mt.lastY);
- }
- }
- for(int i=0;i<ets.length;i++){
- if(ets[i].isVisible()){
- ets[i].checkCollidesWith(map);
- for(int j=0;j<ets.length;j++){
- if(i!=j&&ets[j].isVisible()){
- checkCollidesWith(ets[i],ets[j]);
- }
- }
- ets[i].logic(map,mt);
- mt.logic(map,ets[i]);
- }
- }
- }
- private void checkCollidesWith(EnemyTank et1, EnemyTank et2) {
- if(et1.collidesWith(et2, true)){
- et1.move(-et1.lastX, -et1.lastY);
- et2.move(-et2.lastX, -et2.lastY);
- et1.dir = (et1.dir+2)%4;//碰撞后方向反向
- et2.dir =(et2.dir+2)%4;
- }
- }
- public void createET(int x,int y){
- for (int i = 0; i < ets.length; i++) {
- if (!ets[i].isVisible()) {
- ets[i].setPosition(x,y);
- ets[i].setVisible(true);
- break;
- }
- }
- }
- }
Map:
- package STF;
- import java.io.IOException;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.TiledLayer;
- public class Map {
- Image img;
- TiledLayer ty_grass, ty_water, ty_shuini, ty_brick, ty_oldhome;
- int[][] map = {
- { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
- { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
- { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
- { 3, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 3 },
- { 3, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 3 },
- { 3, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 3 },
- { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
- { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
- { 3, 3, 0, 4, 0, 3, 0, 3, 0, 4, 0, 3, 3 },
- { 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3 },
- { 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3 },
- { 3, 0, 2, 4, 4, 4, 4, 4, 4, 4, 2, 0, 3 },
- { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
- { 3, 0, 4, 0, 0, 4, 4, 4, 0, 0, 4, 0, 3 },
- { 3, 0, 0, 0, 0, 4, 5, 4, 0, 0, 0, 0, 3 },
- { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }
- };
- public Map() {
- try {
- img = Image.createImage("/bg.png");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ty_brick = new TiledLayer(13, 16, img, 15, 15);
- ty_grass = new TiledLayer(13, 16, img, 15, 15);
- ty_shuini = new TiledLayer(13, 16, img, 15, 15);
- ty_oldhome = new TiledLayer(13, 16, img, 15, 15);
- ty_water = new TiledLayer(13, 16, img, 15, 15);
- }
- public void loadMap() {
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[i].length; j++) {
- // ty.setCell(j, i, map[i][j]);
- if (map[i][j] == 1) {
- ty_grass.setCell(j, i, map[i][j]);
- }
- if (map[i][j] == 2) {
- ty_water.setCell(j, i, map[i][j]);
- }
- if (map[i][j] == 3) {
- ty_shuini.setCell(j, i, map[i][j]);
- }
- if (map[i][j] == 4) {
- ty_brick.setCell(j, i, map[i][j]);
- }
- if (map[i][j] == 5) {
- ty_oldhome.setCell(j, i, map[i][j]);
- }
- }
- }
- }
- }
GameSprite:
- package STF;
- import java.io.IOException;
- import java.util.Random;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.Sprite;
- /****************************************************
- * GameSprite类继承自Sprite类,封装坦克的公共属性和方法。
- ****************************************************/
- public class GameSprite extends Sprite{
- //坦克运动的四个方向
- static final int UP = 0;
- static final int RIGHT = 1;
- static final int DOWN = 2;
- static final int LEFT = 3;
- int dir=0;//记录坦克正在运动的方向
- int lastX, lastY;//记录坦克最后一次运动的距离,为防止坦克与碰到的东西重叠,使其返回
- Random ran;//随机数,用于敌方坦克的随机产生,方向的随机变化等
- int bCount=0;//子弹记数器,用于实现连发的间断效果
- Image imgBullet;//子弹图片
- Bullet bullets[]=new Bullet[20];//子弹数组(参见子弹类Bullet)
- //构造函数
- public GameSprite(Image image, int frameWidth, int frameHeight) {
- super(image, frameWidth, frameHeight);
- //设置相对坐标点为精灵的中心
- this.defineReferencePixel(this.getWidth() / 2, this.getHeight() / 2);
- ran=new Random();//初始化随机数
- try {
- //导入子弹图片
- imgBullet = Image.createImage("/bullet.png");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //初始化所有子弹
- for(int i=0;i<bullets.length;i++){
- bullets[i]=new Bullet(imgBullet,3,3);
- bullets[i].setVisible(false);
- }
- }
- //检查并处理坦克与地图相碰
- public void checkCollidesWith(Map map){
- if (this.collidesWith(map.ty_brick, true) || this.collidesWith(map.ty_shuini, true)
- || this.collidesWith(map.ty_water, true)) {
- this.move(-lastX, -lastY);//相碰后返回最后次运动的距离
- }
- }
- }
MainTank:
- package STF;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.GameCanvas;
- import javax.microedition.lcdui.game.Sprite;
- /**************************************************
- * MainTank类继承自GamSprite类,封装我方坦克的特有方法。
- *************************************************/
- public class MainTank extends GameSprite{
- //构造函数
- public MainTank(Image image, int frameWidth, int frameHeight) {
- super(image, frameWidth, frameHeight);
- }
- /*我方坦克的逻辑处理,包括子弹的移动,子弹的碰撞处理,子弹的爆炸
- *传入地图参数map,处理与子弹与地图的碰撞
- *传入敌方坦克参数et,处理与子弹与敌方坦克的碰撞 */
- public void logic(Map map,EnemyTank et){
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].move();//子弹移动
- }
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].checkCollidesWith(map);//子弹与地图的碰撞处理
- bullets[i].checkCollidesWith(et);//子弹与敌方坦克的碰撞处理
- }
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].exp();//子弹的爆炸
- }
- }
- //我坦克的按键处理
- public void input(int k){
- //首先设置最后一次移动的距离为0
- lastX = 0;
- lastY = 0;
- //按向上键
- if ((k & GameCanvas.UP_PRESSED) != 0) {
- this.setTransform(Sprite.TRANS_MIRROR_ROT90);//设置坦克炮筒朝向
- this.nextFrame();//实现坦克的运动时闪动效果
- this.move(0, -2);//向上移动2
- lastY = -2;//设置最后一次在y方向向上移动的距离为2
- dir=UP;//设置坦克正在运动的方向为向上
- }
- //按向下键
- if ((k & GameCanvas.DOWN_PRESSED) != 0) {
- this.setTransform(Sprite.TRANS_MIRROR_ROT270);//设置坦克炮筒朝向
- this.nextFrame();//实现坦克的运动时闪动效果
- this.move(0, 2);//向下移动2
- lastY = 2;//设置最后一次在y方向向下移动的距离为2
- dir=DOWN;//设置坦克正在运动的方向为向下
- }
- //按向左键
- if ((k & GameCanvas.LEFT_PRESSED) != 0) {
- this.setTransform(Sprite.TRANS_MIRROR);//设置坦克炮筒朝向
- this.nextFrame();//实现坦克的运动时闪动效果
- this.move(-2, 0);//向左移动2
- lastX = -2;//设置最后一次在x方向各左移动的距离为2
- dir=LEFT;//设置坦克正在运动的方向为向左
- }
- //按向右键
- if ((k & GameCanvas.RIGHT_PRESSED) != 0) {
- this.setTransform(Sprite.TRANS_NONE);//设置坦克炮筒朝向
- this.nextFrame();//实现坦克的运动时闪动效果
- this.move(2, 0);//向右移动2
- lastX = 2;//设置最后一次在x方向向右移动的距离为2
- dir=RIGHT;//设置坦克正在运动的方向为向右
- }
- //按向上键
- if((k & GameCanvas.FIRE_PRESSED) != 0){
- createBullets();//产生子弹
- }
- }
- //产生子弹方法
- public void createBullets(){
- bCount++;//记数器加1
- if(bCount==2)//两次记数后再产生子弹
- {
- //遍历
- for (int i = 0; i < bullets.length; i++) {
- //先判断子弹不可见再产生
- if (!bullets[i].isVisible()) {
- //设置子弹初始位置
- bullets[i].setPosition(this.getRefPixelX(), this
- .getRefPixelY());
- bullets[i].setVisible(true);//设置子弹可见
- bullets[i].dir=this.dir;//设置子弹初始位置
- bullets[i].launcher=Bullet.MT;//设置子弹为我坦克发出
- break;//产生一个子弹后,立即跳出循环
- }
- }
- bCount=0;//产生子弹设设置记数器为0
- }
- }
- public void bulletLogic(Bullet b) {
- b.move();
- //b.checkCollidesWith(map);
- }
- }
EnemyTank:
- package STF;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.Sprite;
- /**************************************************
- * EnemyTank类继承自GamSprite类,封装敌方坦克的特有方法。
- *************************************************/
- public class EnemyTank extends GameSprite{
- //构造函数
- public EnemyTank(Image image, int frameWidth, int frameHeight) {
- super(image, frameWidth, frameHeight);
- }
- //检查并处理坦克与地图相碰
- public void checkCollidesWith(Map map){
- if (this.collidesWith(map.ty_brick, true) || this.collidesWith(map.ty_shuini, true)
- || this.collidesWith(map.ty_water, true)) {
- this.move(-lastX, -lastY);
- this.dir = ran.nextInt(4);//碰撞后随机变向
- }
- }
- /*敌方坦克的逻辑处理,包括坦克的随机移动,子弹的移动,子弹的碰撞处理,子弹的爆炸
- *传入地图参数map,处理与子弹与地图的碰撞
- *传入敌方坦克参数mt,处理与子弹与我方坦克的碰撞 */
- public void logic(Map map,MainTank mt){
- if(this.isVisible()){
- checkCollidesWith(map);
- lastX = 0;
- lastY = 0;
- switch (dir) {
- case UP:
- this.setTransform(Sprite.TRANS_MIRROR_ROT90);
- this.nextFrame();
- this.move(0, -2);
- lastY = -2;
- break;
- case DOWN:
- this.setTransform(Sprite.TRANS_MIRROR_ROT270);
- this.nextFrame();
- this.move(0, 2);
- lastY = 2;
- break;
- case LEFT:
- this.setTransform(Sprite.TRANS_MIRROR);
- this.nextFrame();
- this.move(-2, 0);
- lastX = -2;
- break;
- case RIGHT:
- this.setTransform(Sprite.TRANS_NONE);
- this.nextFrame();
- this.move(2, 0);
- lastX = 2;
- break;
- }
- this.createBullets();//产生子弹
- setDir();//设置坦克方向的随机改变
- }
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].move();
- }
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].checkCollidesWith(map);
- bullets[i].checkCollidesWith(mt);
- }
- for (int i = 0; i < bullets.length; i++) {
- bullets[i].exp();
- }
- }
- //设置坦克方向的随机改变的方法
- public void setDir() {
- int m = ran.nextInt(3);
- if (m != 0) {
- return;
- }
- int mx = this.getRefPixelX();
- int my = this.getRefPixelY();
- int gezix = 15 / 2;
- int geziy = 15 / 2;
- if (mx % 15 != gezix) {
- return;
- }
- if (my % 15 != geziy) {
- return;
- }
- this.dir = ran.nextInt(4);
- }
- //产生子弹方法
- public void createBullets(){
- bCount++;
- //8次后再产生子弹
- if(bCount==8)
- {
- for (int i = 0; i < bullets.length; i++) {
- if (!bullets[i].isVisible()) {
- bullets[i].setPosition(this.getRefPixelX(), this
- .getRefPixelY());
- bullets[i].setVisible(true);
- bullets[i].s_exp.setVisible(false);
- bullets[i].dir=this.dir;
- bullets[i].launcher=Bullet.ET;
- break;
- }
- }
- bCount=0;
- }
- }
- }