飞机大战-子弹的实现

来源:互联网 发布:win10怎么更改公用网络 编辑:程序博客网 时间:2024/04/29 10:30


子弹的位置与自己的敌机位置有关,大家可以随意设置不同样式的子弹,比如双排子弹,三排子弹,实现不同的效果,通过改变刷新的频率和设置他的速率效果都不一样,

看看下面我设置的几种简单的子弹。


1.Bullet

<span style="font-size:18px;">package com.example.qgns;import java.util.Random;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;public class Bullet extends GameObject {private Bitmap bullet;//</span><span style="font-size:14px;">子弹图片</span><span style="font-size:18px;">private Bitmap bulletCheck;//</span><span style="font-size:14px;">打中敌机时显示的子弹</span><span style="font-size:18px;">public Bullet(Resources res) {super(res);initBitmap();this.harm=1;//</span><span style="font-size:14px;">子弹的威力大小</span><span style="font-size:18px;">Random ran=new Random();speed=ran.nextInt(8)+100;//</span><span style="font-size:14px;">子弹的速率,尽量设为不同,效果不一样</span><span style="font-size:18px;">}@Overridepublic void initScreen(float screen_width, float screen_height) {super.initScreen(screen_width, screen_height);//</span><span style="font-size:14px;">得到屏幕的宽和高,注意不要删掉哦</span><span style="font-size:18px;">}@Overridepublic void initial(int i, float m, float n, int j) {super.initial(i, m, n, j);object_x=m-object_width/2;//</span><span style="font-size:14px;">子弹的位置</span><span style="font-size:18px;">object_y=n-4*object_height;}@Overridepublic void initBitmap() {//</span><span style="font-size:14px;">初始化操作</span><span style="font-size:18px;">bullet=BitmapFactory.decodeResource(res, R.drawable.bullet1);bulletCheck=BitmapFactory.decodeResource(res, R.drawable.bullet4bao);object_width=bullet.getWidth();object_height=bullet.getHeight();}@Overridepublic void myDraw(Canvas canvas) {if(isAlive){if(!isExplosion){canvas.drawBitmap(bullet, object_x, object_y, paint);move();}else{canvas.drawBitmap(bulletCheck, object_x, object_y, paint);//currentFrome++;//if(currentFrome>=4){isExplosion=false;isAlive=false;//}}}}@Overridepublic void move() {//</span><span style="font-size:14px;">移动方法,在屏幕外则不再绘制</span><span style="font-size:18px;">if(object_y>0){object_y-=speed;}else{isAlive=false;}}@Overridepublic void release() {//</span><span style="font-size:14px;">回收处理</span><span style="font-size:18px;">if(!bullet.isRecycled()){bullet.recycle();}}@Overridepublic boolean isCollide(GameObject obj) {//</span><span style="font-size:14px;">碰撞检测</span><span style="font-size:18px;">return super.isCollide(obj);}}</span>


2.Bullet1

package com.example.qgns;import java.util.Random;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;public class Bullet1 extends GameObject {//这里设置两颗子弹在一条直线上private Bitmap bullet;private Bitmap bulletCheck;private boolean isAlive1;private boolean isExplosion1;private float object_x1;private float object_y1;public Bullet1(Resources res) {super(res);initBitmap();this.harm=1;Random ran=new Random();speed=ran.nextInt(5)+100;}@Overridepublic void initScreen(float screen_width, float screen_height) {super.initScreen(screen_width, screen_height);}@Overridepublic void initial(int i, float m, float n, int j) {super.initial(i, m, n, j);isAlive1=true;object_x=m-object_width/2;object_y=n-4*object_height;object_x1=m-object_width/2;object_y1=n-5*object_height-25;}@Overridepublic void initBitmap() {bullet=BitmapFactory.decodeResource(res, R.drawable.bullet2);bulletCheck=BitmapFactory.decodeResource(res, R.drawable.bullet4bao);object_width=bullet.getWidth();object_height=bullet.getHeight();}@Overridepublic void myDraw(Canvas canvas) {if(isAlive){if(!isExplosion){canvas.drawBitmap(bullet, object_x, object_y, paint);move();}else{canvas.drawBitmap(bulletCheck, object_x, object_y, paint);isExplosion=false;isAlive=false;}}if(isAlive1){if(!isExplosion1){canvas.drawBitmap(bullet, object_x1, object_y1, paint);move1();}else{isExplosion1=false;isAlive1=false;}}}@Overridepublic void move() {if(object_y>0){object_y-=speed;}else{isAlive=false;}}public void move1(){if(object_y1>0){object_y1-=speed;}else{isAlive1=false;}}@Overridepublic void release() {if(!bullet.isRecycled()){bullet.recycle();}}@Overridepublic boolean isCollide(GameObject obj) {if (Math.abs((object_x + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion = true;return true;}if (Math.abs((object_x1 + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y1 + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion1 = true;return true;}return false;}}


3.Bullet2

package com.example.qgns;import java.util.Random;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;public class Bullet2 extends GameObject {//设置三颗并排子弹,注意每颗子弹都要进行碰撞检测private Bitmap bullet;private boolean isAlive1;private boolean isAlive2;private boolean isExplosion1;private boolean isExplosion2;private float object_x1;private float object_x2;private float object_y1;private float object_y2;public Bullet2(Resources res) {super(res); initBitmap(); this.harm=1; Random ran=new Random(); this.speed=ran.nextInt(8)+80;}@Overridepublic void initScreen(float screen_width, float screen_height) {super.initScreen(screen_width, screen_height);}@Overridepublic void initial(int i, float m, float n, int j) {isAlive=true;isAlive1=true;isAlive2=true;object_x=m-object_width/2;object_y=n-object_height*4;object_x1=m-object_width*5/2;object_y1=n-object_height*4;object_x2=m+object_width*3/2;object_y2=n-object_height*4;}@Overridepublic void initBitmap() {bullet=BitmapFactory.decodeResource(res, R.drawable.bullet2);object_width=bullet.getWidth();object_height=bullet.getHeight();}@Overridepublic void myDraw(Canvas canvas) {if(isAlive){if(!isExplosion){canvas.drawBitmap(bullet, object_x, object_y, paint);}else{isAlive=false;isExplosion=false;}}if(isAlive1){if(!isExplosion1){canvas.drawBitmap(bullet, object_x1, object_y1, paint);}else{isAlive1=false;isExplosion1=false;}}if(isAlive2){if(!isExplosion2){canvas.drawBitmap(bullet, object_x2, object_y2, paint);}else{isAlive2=false;isExplosion2=false;}}move();}@Overridepublic void move() {if(object_y>0){object_y-=speed;}else{isAlive=false;}if(object_y1>0){object_x1-=5;object_y1-=speed;}else{isAlive1=false;}if(object_y2>0){object_x2+=5;object_y2-=speed;}else{isAlive2=false;}}@Overridepublic void release() {if(!bullet.isRecycled()){bullet.recycle();}}@Overridepublic boolean isCollide(GameObject obj) {if (Math.abs((object_x + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion = true;return true;}if (Math.abs((object_x1 + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y1 + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion = true;return true;}if (Math.abs((object_x2 + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y2 + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion = true;return true;}return false;}}


0 0