图:
懒得写解释了直接上代码:
代码:
package chenlong.chenlong.viewstudy.myview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; import androidx.annotation.Nullable; import chenlong.chenlong.viewstudy.MainActivity; public class flyer extends LinearLayout { public int fraction; Paint mPaint = new Paint(); Paint TPaint = new Paint(); Paint DPaint = new Paint(); aircraft aircraft; List<bullet> bullet_list = new ArrayList<bullet>(); List<aircraft> enemy_flyer_list = new ArrayList<aircraft>(); Thread T; int NUM = 0; int mw; int mh; int FPS = 60; int num_move = 0; public flyer(Context context) { this(context, null); // initP(); } public flyer(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); // initP(); } public flyer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initP(); } protected void initP() { // fraction= MainActivity.fraction_history; aircraft = new aircraft(); System.out.println(aircraft.x); System.out.println(aircraft.y); mPaint.setColor(Color.BLACK);//画笔颜色 mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL);//画笔模式为填充 mPaint.setStrokeWidth(10f);//画笔宽度 TPaint.setColor(Color.BLACK);//画笔颜色 TPaint.setAntiAlias(true); TPaint.setStyle(Paint.Style.FILL);//画笔模式为填充 TPaint.setStrokeWidth(5);//画笔宽度 TPaint.setTextSize(50); DPaint.setColor(Color.RED);//画笔颜色 DPaint.setAntiAlias(true); DPaint.setStyle(Paint.Style.FILL);//画笔模式为填充 DPaint.setStrokeWidth(5);//画笔宽度 DPaint.setTextSize(50); //生成敌机 } int randint(int max, int min) { int ran2 = (int) (Math.random() * (max - min) + min); return ran2; } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int X = (int) event.getX(); int Y = (int) event.getY(); Log.i("view", String.format("X:%d , Y:%d", X, Y)); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.i("按下", String.format("X:%d , Y:%d", X, Y)); float l = (float) Math.pow(Math.pow((X - aircraft.x), 2) + Math.pow((Y - aircraft.y), 2), 0.5); System.out.println(l); if (l < 80) { System.out.println("发射"); bullet_list.add(new bullet(X, Y)); } break; case MotionEvent.ACTION_MOVE: Log.i("移动", String.format("X:%d , Y:%d", X, Y)); num_move += 1; if (num_move > 2) { num_move = 0; bullet_list.add(new bullet(X, Y)); } aircraft.move(X, Y); break; case MotionEvent.ACTION_UP: Log.i("抬起", String.format("X:%d , Y:%d", X, Y)); break; } invalidate(); return true; } float p2pline(int x1, int y1, int x2, int y2) { return (float) Math.pow(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2), 0.5); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); try { aircraft.draw_(canvas);//绘制飞机 for (bullet bu : bullet_list) {//绘制子弹 bu.draw_(canvas); } for (aircraft air : enemy_flyer_list) {//绘制敌机 canvas.drawCircle(air.x, air.y, 20, DPaint); } canvas.drawText(String.format("飞机坐标:X:%d , Y:%d,飞行子弹数量:%d", aircraft.x, aircraft.y, bullet_list.size()), 20, 80, TPaint); canvas.drawText(String.format("敌机数量:%d", enemy_flyer_list.size()), 20, 160, TPaint); canvas.drawText(String.format("分数:%d", fraction), 20, 220, TPaint); } catch (Exception e) { System.out.println(e); } } private void updata() { for (bullet bu : bullet_list) { bu.y -= 1; for (aircraft air : enemy_flyer_list) { float l = (float) Math.pow(Math.pow(bu.x - air.x, 2) + Math.pow(bu.y - air.y, 2), 0.5); if (l < 30) { System.out.println("击中"); fraction += 1; MainActivity.pref.edit().putInt("fraction", fraction).apply();//保存分数 enemy_flyer_list.remove(air);//移除敌机 bullet_list.remove(bu);//移除子弹 } if (enemy_flyer_list.size() < 10) { for (int o = 0; o < 20; o++) { enemy_flyer_list.add(new aircraft(randint(100, mw - 100), randint(100, mh / 2))); } } if (bu.y < 0) { bullet_list.remove(bu); } } } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mw = w; mh = h; aircraft.x = mw / 2; aircraft.y = mh - 200; for (int o = 0; o < 30; o++) { enemy_flyer_list.add(new aircraft(randint(100, mw - 100), randint(100, mh / 2))); } T = new Thread() { public void run() { long oldtime = System.currentTimeMillis(); while (true) { NUM += 1; long newtime = System.currentTimeMillis(); if (NUM > 5000) { oldtime = newtime; try { updata(); NUM = 0; invalidate(); } catch (Exception e) { System.out.println(e); } } } } }; T.start(); invalidate(); System.out.println(String.format("W:%d , H:%d", mw, mh)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } class bullet { int x; int y; bullet(int x, int y) { this.x = x; this.y = y; } void draw_(Canvas canvas) { canvas.drawCircle(this.x, this.y, 5, mPaint); } } class aircraft { int x = mw / 2; int y = mh - 80; String name = "我方战斗机"; int health = 100; public aircraft() { this(mw / 2, mh - 80); } public aircraft(int x, int y) { this.x = x; this.y = y; } boolean move(int x, int y) { try { this.x = x; this.y = y; return true; } catch (Exception e) { return false; } } void fire() { bullet_list.add(new bullet(this.x, this.y)); } void draw_(Canvas canvas) { canvas.drawCircle(aircraft.x, aircraft.y, 50, mPaint); } } }
用法