1.首先编写敌人,主角,子d 3个类
package com.example.myapplication10.entity;import com.example.myapplication10.R;//敌人public class DiRenEntity { public float x; public float y; public int image_src = R.drawable.diren;}
package com.example.myapplication10.entity;import com.example.myapplication10.R;//主角public class ZhuJueEntity { public float x; public float y; public int image_src = R.drawable.zhujue;}
package com.example.myapplication10.entity;import com.example.myapplication10.R;;//子dpublic class ZIDanEntity { public float x; public float y; public int image_src = R.drawable.zIDan;}
2.自定义页面.java
package com.example.myapplication10.vIEw;import androID.content.Context;import androID.content.Intent;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.PorterDuff;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import java.util.ArrayList;import java.util.List;import androIDx.annotation.NonNull;import com.example.myapplication10.R;import com.example.myapplication10.entity.DiRenEntity;import com.example.myapplication10.entity.ZhuJueEntity;import com.example.myapplication10.entity.ZIDanEntity;public class GameVIEw extends SurfaceVIEw implements SurfaceHolder.Callback { private SurfaceHolder surfaceHolder; private ZhuJueEntity zhuJueEntity ;//一个主角 private List<ZIDanEntity> zIDanEntitIEs = new ArrayList<>();//子d集合 private List<DiRenEntity> diRenEntitIEs = new ArrayList<>();//敌人集合 private int fen = 0;//分数 public GameVIEw(Context context) { super(context); } public GameVIEw(Context context, AttributeSet attrs) { super(context, attrs); surfaceHolder = getHolder(); surfaceHolder.addCallback(this); } public GameVIEw(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public GameVIEw(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @OverrIDe public voID surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { new GameThread().start(); } @OverrIDe public voID surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @OverrIDe public voID surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { } //触摸更换主角的位置 @OverrIDe public boolean ontouchEvent(MotionEvent event) { int action = event.getAction(); float x = event.getX(); float y = event.getY(); if(action == MotionEvent.ACTION_MOVE){ zhuJueEntity.x = x; zhuJueEntity.y = y; } return true; } class GameThread extends Thread{ @OverrIDe public voID run() { super.run(); //Todo 1:初始化主角的位置 zhuJueEntity = new ZhuJueEntity(); zhuJueEntity.y = getHeight()-100; zhuJueEntity.x = getWIDth()/2; //Todo 2:画笔 Paint paint = new Paint(); //Todo 3:绘制主角+敌人+子d while (true){ Canvas canvas = surfaceHolder.lockCanvas();//锁定画布 if(canvas == null){//空判断 return; } canvas.drawcolor(color.WHITE, PorterDuff.Mode.CLEAR);//画布清空 canvas.drawcolor(color.WHITE);//画布刷白 //绘制主角 { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), zhuJueEntity.image_src); canvas.drawBitmap(bitmap, zhuJueEntity.x, zhuJueEntity.y, paint); } //遍历集合绘制子d for (int i = zIDanEntitIEs.size()-1; i >= 0; i--) { ZIDanEntity bulletEntity = zIDanEntitIEs.get(i); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), bulletEntity.image_src); bulletEntity.y-=10;//子d从下向上飞 canvas.drawBitmap(bitmap,bulletEntity.x,bulletEntity.y,paint); //如果子d飞出屏幕从集合移除 if(bulletEntity.y<=0){ zIDanEntitIEs.remove(i); } } //遍历集合绘制敌人 for (int i = diRenEntitIEs.size()-1; i >=0; i--) { DiRenEntity diRenEntity = diRenEntitIEs.get(i); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), diRenEntity.image_src); diRenEntity.y+=10; canvas.drawBitmap(bitmap, diRenEntity.x, diRenEntity.y,paint); if(diRenEntity.y>getHeight()){//如果敌人飞出屏幕从集合移除 diRenEntitIEs.remove(i); } } //判断是否打中敌人,子d和图片中心点的距离:勾股定理 for (int i = 0; i < zIDanEntitIEs.size(); i++) { ZIDanEntity bulletEntity = zIDanEntitIEs.get(i); float x = bulletEntity.x; float y = bulletEntity.y; x+=132/2;//132是子d图片的宽度 y+=132/2;//132是子d图片的宽度 for (int j = 0; j < diRenEntitIEs.size(); j++) { DiRenEntity diRenEntity = diRenEntitIEs.get(j); float x1 = diRenEntity.x; float y1 = diRenEntity.y; x1+=132/2;//132是敌人图片的宽度 y1+=132/2;//132是敌人图片的宽度 double he = Math.pow(x-x1,2)+ Math.pow(y-y1,2); double juli = Math.sqrt(he); if(juli<=132){ diRenEntitIEs.remove(j);//打中敌人,从集合中移除 fen++;//分数 //发送广播給主页面,分数+1 Intent intent = new Intent(); intent.setAction("fen"); intent.putExtra("fen",fen); getContext().sendbroadcast(intent); } } } //判断主角死亡:主角和敌人中心点的距离:勾股定力 for (int i = 0; i < diRenEntitIEs.size(); i++) { DiRenEntity diRenEntity = diRenEntitIEs.get(i); float x = diRenEntity.x; float y = diRenEntity.y; x+=132/2; y+=132/2; float x1 = zhuJueEntity.x+132/2; float y1 = zhuJueEntity.y+132/2; double he = Math.pow(x-x1,2)+ Math.pow(y-y1,2); double juli = Math.sqrt(he); if(juli<=132){ diRenEntitIEs.remove(i); zhuJueEntity.image_src = R.drawable.baozha;//死亡更换爆炸图片 //发送广播給主页面d出对话框 Intent intent = new Intent(); intent.setAction("over"); getContext().sendbroadcast(intent); break; } } surfaceHolder.unlockCanvasAndPost(canvas); } } } //添加子d:初始位置和主角位置一致 public voID addZIDan(){ if(zhuJueEntity != null){ ZIDanEntity bulletEntity = new ZIDanEntity(); bulletEntity.x = zhuJueEntity.x; bulletEntity.y = zhuJueEntity.y; zIDanEntitIEs.add(bulletEntity); } } //添加敌人 public voID addDiren(){ float x = (float) (Math.random()*getWIDth());//随机 DiRenEntity diRenEntity = new DiRenEntity(); diRenEntity.x = x; diRenEntity.y = 0; diRenEntitIEs.add(diRenEntity); }}
4.xml文件布局
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity"> <com.example.myapplication10.vIEw.GameVIEw androID:ID="@+ID/game" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"></com.example.myapplication10.vIEw.GameVIEw> <TextVIEw androID:textStyle="bold" androID:textSize="30sp" androID:textcolor="#E91E63" androID:text="当前分数:0" androID:ID="@+ID/tv_fen" androID:layout_alignParentRight="true" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> </TextVIEw></relativeLayout>
5.MainActivity.java 实现类
package com.example.myapplication10;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AlertDialog;import androIDx.appcompat.app.AppCompatActivity;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.DialogInterface;import androID.content.Intent;import androID.content.IntentFilter;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.Widget.TextVIEw;import com.example.myapplication10.vIEw.GameVIEw;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity { private Timer timer_zIDan = new Timer(); private Timer timer_diren = new Timer(); private GameVIEw gameVIEw; private TextVIEw textVIEw; private MyRecevIEr myRecevIEr; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); gameVIEw = findVIEwByID(R.ID.game); textVIEw = findVIEwByID(R.ID.tv_fen); //注册广播,当子d打中敌人和主角碰上敌人的时候 myRecevIEr = new MyRecevIEr(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("fen"); intentFilter.addAction("over"); registerReceiver(myRecevIEr,intentFilter); //每隔2秒产生一颗子d timer_zIDan.schedule(new TimerTask() { @OverrIDe public voID run() { gameVIEw.addZIDan(); } },0,2000); //每隔1秒产生一个敌人 timer_diren.schedule(new TimerTask() { @OverrIDe public voID run() { gameVIEw.addDiren(); } },0,1000); } private AlertDialog alertDialog; //广播接受者 class MyRecevIEr extends broadcastReceiver{ @OverrIDe public voID onReceive(Context context, Intent intent) { if(intent.getAction().equals("fen")){//打中敌人 int fen = intent.getIntExtra("fen", 0); textVIEw.setText("当前分数:"+fen+""); }else if(intent.getAction().equals("over")){ final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("是否继续"); builder.setNegativebutton("取消", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface, int i) { finish(); } }); builder.setPositivebutton("继续", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface, int i) { alertDialog.dismiss(); } }); alertDialog = builder.create(); alertDialog.show(); } } }}
6.照片资料
@H_419_85@
以上是内存溢出为你收集整理的Android开发飞机大战游戏代码全部内容,希望文章能够帮你解决Android开发飞机大战游戏代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)