飞机大战-子弹的实现
子弹的位置与自己的敌机位置有关,大家可以随意设置不同样式的子弹,比如双排子弹,三排子弹,实现不同的效果,通过改变刷新的频率和设置他的速率效果都不一样,
看看下面我设置的几种简单的子弹。
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;}}
飞机大战-子弹的实现相关推荐
- java飞机大战子弹怎么修改_java改版飞机大战源码
[实例简介] 利用java写的飞机大战,有BOSS 导弹,散射子弹,追踪弹,清屏技能,爆炸效果等等.适合用来练习面向对象: [实例截图] [核心代码] 4692cf8f-6049-4755-a40a- ...
- pygame 飞机大战子弹的编写(三)自定义子弹位置、速度、角度
目标:实现子弹的出现位置(为飞机发射做准备),子弹的速度,角度自定义. 添加两个函数 set_post(self, x, y) #实现 x ,y 坐标出现,这里的坐标是相对于父坐标的. set_sp ...
- pygame 飞机大战子弹的编写(一)绘制子弹
目标:子弹出现在屏幕上 先从子弹开始写.子弹是敌机.BOSS机和英雄机都要用到的,涉及到各种花样玩法 定义子弹类. 先简单的定义,建个bullet.py文件,建立Bullet类,从pygame.spr ...
- python飞机大战子弹不显示_飞机大战游戏 飞机打出一发子弹后就不出现子弹了...
马上注册,结交更多好友,享用更多功能^_^ 您需要 登录 才可以下载或查看,没有帐号?立即注册 x # main.py import pygame import sys import tracebac ...
- pygame 飞机大战子弹的编写(七)花样年华
目标:实现子弹的花样玩法,不同的子弹类型(image),不同的发射方式(speed,angle),不同的子弹数量. 前面几篇文字,已经实现了子弹的不同特性,组合下就能开启子弹的花样年华. 想了两个方案 ...
- pygame 飞机大战子弹的编写(二)让子弹动起来
目标:子弹有速度,也能斜着飞 子弹有了,该动起来了.在Bullet类里添加一个函数就行. def update(self):def __init__(self,type = 1):pygame.spr ...
- pygame之飞机大战子弹360度角度的算法
当子弹拥有一定角度的时候,可以在子弹的set_speed()函数里,算出子弹x,y两个方向的速度,输入角度是degree换成弧度radian def set_speed(self, speed, an ...
- pygame小项目 ~ 3 :Python完成简易飞机大战
pygame小项目 ~ 3 :Python完成简易飞机大战 子弹 敌机 我方战斗机全部采用图片 子弹击中和战斗机被击中全部采用碰撞检测 游戏的主函数代码 import spite from spite ...
- Python飞机大战(完整版)
简介:一共分为2个py文件,分别是主类.和精灵类 飞机大战图片地址:链接: https://pan.baidu.com/s/18T6n9JFIDxBqYX9CnHi7ZQ 密码: tqbr 注释:项 ...
最新文章
- 北理工校友发明文言文“填词大师”,断句、造词都能做,高考文言文满分靠它了...
- C语言-二维数组与指针
- VTK:Utilities之BoundingBox
- 22543!Windows 11 新预览版发布
- 生活质量衡量系统_数据质量与数据质量八个维度指标
- IntelliJ IDEA常用快捷键——基于Eclipse
- 苹果电脑裸机和不裸机的区别_将Kubernetes带到裸机边缘
- Office 365系列之八:配置和体验Exchange和Lync
- 山寨一个PetShop(Task002)——数据类库Model
- OpenBoard 白板交互式应用程序
- java 抽奖算法_Java实现游戏抽奖算法
- 烽火狼烟丨Microsoft多个安全漏洞风险提示
- 电子名片+在线商城=?现在居然可以用名片卖货了
- Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
- Python必学的OS模块详解
- Python线图点图--matplotlib.pyplot.plot
- 随机梯度下降法概述与实例
- 火狐开发版_Firefox普通版和开发版之间有什么区别?
- 9个酒瓶子砸向Google:google不做坏事么?
- 使用jquery.wordexport.js导出word文档 设置行间距不生效问题