飞机大战-子弹的实现
来源:互联网 发布: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
- 飞机大战-子弹的实现
- 飞机大战开发05按角度发射的子弹和拖尾子弹的实现
- 项目飞机大战01:主要实现背景的移动,飞机的动画和触控,子弹的生成和发射
- 飞机大战开发04子弹与敌机的碰撞检测
- 基于cocos2dx的飞机大战学习[四]-添加英雄子弹
- 飞机大战-MyPlane自己飞机的实现
- js实现的飞机大战
- swing 飞机大战 三 自己飞机的移动和发射子弹
- 飞机大战之三:添加子弹
- pygame编写飞机大战(5)-子弹类
- cocos2d-x 3.2 |飞机大战:飞机与子弹
- 飞机大战03之飞机发射子弹,添加敌人
- swing 飞机大战 四 生成敌人飞机和敌人子弹
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 06.游戏界面 主角飞机产生子弹
- cocos2d中分步实现飞机大战----自己飞机的实现
- 【cocos2d-x入门实战】微信飞机大战之六:子弹层的处理
- 微信经典飞机大战之四:子弹层的完全解析
- unity学习之飞机大战制作 关键点(三) 设置子弹的生成和移动
- Cookie和Session详解
- linux中c语言errno的使用
- 萃取(traits)编程技术的介绍和应用
- 心情
- java并发笔记
- 飞机大战-子弹的实现
- NGUI所见即所得之深入剖析UIPanel,UIWidget,UIDrawCall底层原理
- memcached linux安装
- android Touch事件分发机制
- 那些真实
- uibutton(上下左右箭头加放大和缩小)
- jackson的循环问题和hibernate懒加载问题
- memcached 项目中使用
- javaweb学习总结(三十一)——国际化(i18n)