Bullets.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.game.*;
- import javax.microedition.lcdui.*;
- import java.util.Random;
- public class Bullets extends Sprite {
- private int[][] bullets;
- private int BULLETS_NUM;
- private Random random;
- private int frameWidth,frameHeight;
- 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;
- private static final int ALIVE = 1;
- private static final int DEFAULT_SPEED =3;
- private int CanvasWidth,CanvasHeight;
- public Bullets(Image image,int frameWidth,int frameHeight) {
- super(image,frameWidth,frameHeight);
- this.frameWidth = frameWidth;
- this.frameHeight = frameHeight;
- random=new Random();
- defineReferencePixel(frameWidth/2 , frameHeight/2 );
- }
- public void setBulletsNum(int BULLETS_NUM){
- this.BULLETS_NUM = BULLETS_NUM;
- }
- public void setCanvasSize(int CanvasWidth,int CanvasHeight){
- this.CanvasWidth = CanvasWidth;
- this.CanvasHeight = CanvasHeight;
- }
- public void initBullets(){
- bullets=new int[BULLETS_NUM][6];
- for (int i = 0; i < bullets.length; i++) {
- initBullet(i);
- }
- }
- private void initBullet(int i) {
- bullets[i][0] = (random.nextInt() & 0x7fffffff) % 4; //type
- bullets[i][5] = ALIVE; //alive
- switch (bullets[i][0]) {
- case BULLET_TYPE_LEFT:
- bullets[i][1] = -frameWidth;
- bullets[i][2] = (random.nextInt() & 0x7fffffff) % CanvasHeight;
- bullets[i][3] = (random.nextInt() & 0x7fffffff) % 3 + 1; //vx
- bullets[i][4] = (random.nextInt()) % 3; //vy
- break;
- case BULLET_TYPE_RIGHT:
- bullets[i][1] = CanvasWidth + frameWidth;
- bullets[i][2] = (random.nextInt() & 0x7fffffff) % CanvasHeight;
- bullets[i][3] = ((random.nextInt() & 0x7fffffff) % 3 + 1) * -1; //vx
- bullets[i][4] = (random.nextInt()) % 3; //vy
- break;
- case BULLET_TYPE_TOP:
- bullets[i][1] = (random.nextInt() & 0x7fffffff) % CanvasWidth;
- bullets[i][2] = -frameHeight;
- bullets[i][3] = (random.nextInt()) % 3; //vx
- bullets[i][4] = (random.nextInt() & 0x7fffffff) % 3 + 1; //vy
- break;
- case BULLET_TYPE_BOTTOM:
- bullets[i][1] = (random.nextInt() & 0x7fffffff) % CanvasWidth;
- bullets[i][2] = CanvasHeight + frameHeight;
- bullets[i][3] = (random.nextInt()) % 3; //vx
- bullets[i][4] = ( (random.nextInt() & 0x7fffffff) % 3 + 1) * -1; //vy
- break;
- }
- if(bullets[i][3]==0)
- {
- if(bullets[i][0] == BULLET_TYPE_RIGHT||bullets[i][0] == BULLET_TYPE_BOTTOM)
- bullets[i][3] = -DEFAULT_SPEED;
- else bullets[i][3] = DEFAULT_SPEED;
- }
- if(bullets[i][4]==0)
- {
- if(bullets[i][0] == BULLET_TYPE_RIGHT||bullets[i][0] == BULLET_TYPE_BOTTOM)
- bullets[i][4] = -DEFAULT_SPEED;
- else bullets[i][4] = DEFAULT_SPEED;
- }
- }
- public void tick(){
- for (int i = 0; i < bullets.length; i++) {
- tick(i);
- }
- }
- private void tick(int i){
- bullets[i][1]+=bullets[i][3];
- bullets[i][2]+=bullets[i][4];
- if(bullets[i][1]<-5 || bullets[i][1]>CanvasWidth+5){
- bullets[i][3]*=-1;
- }
- if(bullets[i][2]<-5 || bullets[i][2]>CanvasHeight+5){
- bullets[i][4]*=-1;
- }
- }
- public void draw(Graphics g) {
- for (int i = 0; i < bullets.length; i++) {
- if(bullets[i][5]!= ALIVE){
- continue;
- }
- draw(g,i);
- }
- }
- private void draw(Graphics g,int i){
- setPosition(bullets[i][1],bullets[i][2]);
- paint(g);
- }
- public boolean collidesWith(Sprite s){
- for (int i = 0; i < bullets.length; i++) {
- if(bullets[i][5]!= ALIVE){
- continue;
- }
- setPosition(bullets[i][1],bullets[i][2]);
- if(collidesWith(s,true)){
- return true;
- }
- }
- return false;
- }
- }