⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bullets.java

📁 《j2me开发精解〉(詹健飞)CD-rom附带源码。用netbeans
💻 JAVA
字号:
package com.j2medev.chapter5.example.hold;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import com.j2medev.chapter5.utility.ImageUtilities;

import java.util.Random;

public class Bullets {
    public GameWorld world;
    
    Sprite bulletSprite;
    
    private int[][] bullets;
    
    private int bulletstotal;
    
    private Random rnd;
    
    public static final int BULLET_TYPE_LEFT = 0;
    
    public static final int BULLET_TYPE_RIGHT = 1;
    
    public static final int BULLET_TYPE_TOP = 2;
    
    public static final int BULLET_TYPE_BOTTOM = 3;
    
    public static final int BULLET_TYPE_TOTAL = 4;
    
    public static final int BULLET_LIFE = 0;
    
    public static final int BULLET_X = 1;
    
    public static final int BULLET_Y = 2;
    
    public static final int BULLET_VX = 3;
    
    public static final int BULLET_VY = 4;
    
    static public Bullets createBullets(GameWorld world) {
        Bullets b = new Bullets(ImageUtilities.createImage("/bullet.png"), 6,
                6, 20);
        b.world = world;
        b.initBullets();
        return b;
    }
    
    protected Bullets(Image img, int picwidth, int picheight, int bulletstotal) {
        bulletSprite = new Sprite(img, picwidth, picheight);
        bulletSprite.defineReferencePixel(picwidth / 2, picheight / 2);
        this.bulletstotal = bulletstotal;
        bullets = new int[bulletstotal][5];
        rnd = new Random();
    }
    
    public void initBullets() {
        for (int i = 0; i < bullets.length; i++) {
            initBullet(i);
        }
    }
    
    private void initBullet(int i) {
        int direction = (rnd.nextInt() & 0x7fffffff) % BULLET_TYPE_TOTAL; // type
        bullets[i][BULLET_LIFE] = 1; // alive
        int height = world.viewport_height;
        int width = world.viewport_width;
        switch (direction) {
            case BULLET_TYPE_LEFT:
                bullets[i][BULLET_X] = -5;
                bullets[i][BULLET_Y] = (rnd.nextInt() & 0x7fffffff) % height;
                bullets[i][BULLET_VX] = (rnd.nextInt() & 0x7fffffff) % 3 + 1;
                bullets[i][BULLET_VY] = (rnd.nextInt()) % 3;
                break;
            case BULLET_TYPE_RIGHT:
                bullets[i][BULLET_X] = width + 5;
                bullets[i][BULLET_Y] = (rnd.nextInt() & 0x7fffffff) % height;
                bullets[i][BULLET_VX] = ((rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1;
                bullets[i][BULLET_VY] = (rnd.nextInt()) % 3;
                break;
            case BULLET_TYPE_TOP:
                bullets[i][BULLET_X] = (rnd.nextInt() & 0x7fffffff) % width;
                bullets[i][BULLET_Y] = -5;
                bullets[i][BULLET_VX] = (rnd.nextInt()) % 3;
                bullets[i][BULLET_VY] = (rnd.nextInt() & 0x7fffffff) % 3 + 1;
                break;
            case BULLET_TYPE_BOTTOM:
                bullets[i][BULLET_X] = (rnd.nextInt() & 0x7fffffff) % width;
                bullets[i][BULLET_Y] = height + 5;
                bullets[i][BULLET_VX] = (rnd.nextInt()) % 3; // vx
                bullets[i][BULLET_VY] = ((rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1; // vy
                break;
        }
    }
    
    public void updateBullet(int i) {
        bullets[i][BULLET_X] += bullets[i][BULLET_VX];
        bullets[i][BULLET_Y] += bullets[i][BULLET_VY];
        if (bullets[i][1] < -5 || bullets[i][1] > world.viewport_width + 5) {
            bullets[i][3] *= -1;
        }
        if (bullets[i][2] < -5 || bullets[i][2] > world.viewport_height + 5) {
            bullets[i][4] *= -1;
        }
    }
    
    public void updateBullets(Plane plane, boolean needcollision) {
        for (int i = 0; i < bullets.length; i++) {
            if (bullets[i][BULLET_LIFE] == 0) {
                continue;
            }
            if (needcollision) {
                // System.out.println("needcollision ");
                if (isCollision(plane, i, 16)) {
                    // System.out.println("collision ");
                    world.plane.collides();
                    bullets[i][BULLET_LIFE] = 0;
                    continue;
                }
            }
            updateBullet(i);
        }
    }
    private void paintBullet(Graphics g, int i, int x, int y) {
        if (bullets[i][BULLET_X] < 0
                || bullets[i][BULLET_X] > world.viewport_width-bulletSprite.getWidth()
                || bullets[i][BULLET_Y] < 0
                || bullets[i][BULLET_Y] > world.viewport_height-bulletSprite.getHeight()) {
            return;
        }
        bulletSprite.setPosition(bullets[i][BULLET_X] + x, bullets[i][BULLET_Y]
                + y);
        bulletSprite.paint(g);
    }
    
    public void paint(Graphics g, int x, int y) {
        for (int i = 0; i < bullets.length; i++) {
            if (bullets[i][BULLET_LIFE] == 0) {
                continue;
            }
            paintBullet(g, i, x, y);
        }
    }
    
    
    
    private boolean isCollision(Plane plane, int i, int range) {// 菱形检测法
        boolean result = false;
        int planeXCenter = plane.getRefPixelX();
        int planeYCenter = plane.getRefPixelY();
        bulletSprite.setPosition(bullets[i][BULLET_X], bullets[i][BULLET_Y]);
        int bulletXCenter = bulletSprite.getRefPixelX();
        int bulletYCenter = bulletSprite.getRefPixelY();
        if (Math.abs(planeXCenter - bulletXCenter) < range) {
            if (Math.abs(planeYCenter - bulletYCenter) < range) {
                result = true;
            }
        }
        return result;
    }
    
    public void killbullets(Plane plane, int range) {
        for (int i = 0; i < bullets.length; i++) {
            if (bullets[i][BULLET_LIFE] != 0) {// alive bullets
                if (isCollision(plane, i, range)) {
                    bullets[i][BULLET_LIFE] = 0;
                    initBullet(i);
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -