可以实现手机射击效果,控制射击方向, public static synchronized GameView sharedGameView(Context context,int level){ if(gameView==null){ gameView = new GameView(context,level); } return gameView; } private GameView(Context context,int level) { super(context); this.context = context; this.level = level; heroBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.hero); projectileBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.projectile); targetBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.target); Tools.sharedTools(context).playBgSnd(); projectiles = new Vector(); targets = new Vector(); handler = new Handler(){ @Override public void handleMessage(Message msg) { if(over==true){ return; } switch(msg.what){ case kWin: //should be produce target. { overOutpost(true); break; } case kFail: overOutpost(false); break; case kPostInvalidate: invalidate(); break; case kProduceTarget: Random random = new Random(System.currentTimeMillis()); if(getHeight()!=0){ //because before gameView was display,this value is 0 float y = random.nextInt(getHeight()-targetBmp.getHeight()); float speed = Tools.sharedTools(GameView.this.context).getSetting(GameView.this.level).targetSpeed; targets.add(new Target(getWidth(), y,speed)); } break; default: break; } } }; timer = new Timer(true); timer.scheduleAtFixedRate(new TimerTask() { int j=0; int i=0; @Override public void run() { if(over==true){ return; } handler.sendEmptyMessage(kPostInvalidate); i=i+1; j=j+1; Log.d("waj", "i:"+i+Thread.currentThread().getName()+" "+GameView.this.toString()); int a = (int)Math.ceil(Tools.sharedTools(GameView.this.context).getSetting(GameView.this.level).targetDensity*60.0/1000.0); if(j == a){ handler.sendEmptyMessage(kProduceTarget); j=0; } if(i<1800){ for (Target target : targets) { if (target.latestX <= 0) { i=0; j=0; handler.sendEmptyMessage(kFail); break; } } } else{//30 second handler.sendEmptyMessage(kWin); i = 0; j = 0; } } }, 0, (long) (1000.0/60.0)); //flush 60 per second } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); canvas.drawBitmap(heroBmp, 0, getHeight()/2-heroBmp.getHeight()/2, paint); if(over==true){ return; } // remove projectiles which had outof bound of gameView. List destroyedProjectiles = new ArrayList(); for (Projectile projectile : projectiles) { Point p = projectile.nextPoint(); if (p.x > getWidth() | p.x + projectileBmp.getWidth() < 0 | p.y + projectileBmp.getHeight() < 0 | p.y > getHeight()) { destroyedProjectiles.add(projectile); continue; } canvas.drawBitmap(projectileBmp, p.x, p.y, paint); } projectiles.removeAll(destroyedProjectiles); // remove targets which's body had outof left bound of gameView List destroyedTargets = new ArrayList(); for (Target target : targets) { Point p = target.nextPoint(); canvas.drawBitmap(targetBmp, p.x, p.y, paint); } targets.removeAll(destroyedTargets); // crash detect destroyedProjectiles.clear(); destroyedTargets.clear(); for (Projectile projectile : projectiles) { for (Target target : targets) { // 判断是否相交 float x1 = projectile.latestX; float x2 = projectile.latestX + projectileBmp.getWidth(); float y1 = projectile.latestY; float y2 = projectile.latestY + projectileBmp.getHeight(); float x3 = target.latestX; float x4 = target.latestX + targetBmp.getWidth(); float y3 = target.latestY; float y4 = target.latestY + targetBmp.getHeight(); if (x1 < x4 && x2 > x3 && y1 < y4 && y2 > y3) { destroyedProjectiles.add(projectile); destroyedTargets.add(target); } } } projectiles.removeAll(destroyedProjectiles); targets.removeAll(destroyedTargets); } //outpost is over, include win and fail private void overOutpost(boolean isWin) { over = true; projectiles.clear(); targets.clear(); Intent intent = new Intent(context,SecondActivity.class); Activity mainActivity = (MainActivity)context; if(isWin){ intent.putExtra("message","You are winner of "+level+" outpost!"); mainActivity.startActivityForResult(intent,100); } else{ intent.putExtra("message","You failed on the "+level+" outpost"); mainActivity.startActivityForResult(intent,200); } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ int count = Tools.sharedTools(context).getSetting(level).projectileNum; for(int i=1;i<=count;i++){ Tools.sharedTools(context).playPewPew(); Projectile projectile = new Projectile(heroBmp.getWidth()/2, getHeight()/2-projectileBmp.getHeight()/2, event.getX(), event.getY(), Tools.sharedTools(context).getSetting(level).projectileSpeed*i); projectiles.add(projectile); } } return super.onTouchEvent(event); } //data area private final int kFail = 1; private final int kWin = 2; private final int kPostInvalidate=3; private final int kProduceTarget=4; private List<Projectile> projectiles; private List<Target> targets; private Bitmap heroBmp; private Bitmap projectileBmp; private Bitmap targetBmp; private Context context; private Timer timer; private Handler handler; private int level; //from 1...10 private boolean over=false; private static GameView gameView; void setLevel(int level){ this.level = level; } void stop(){ over = true; } void start(){ over = false; }
- 1
- 粉丝: 44
- 资源: 21
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 毕设和企业适用springboot智能云服务平台类及物流信息平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及用户体验优化平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及员工管理平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及智能农业解决方案源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及智能语音助手平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及车联网管理平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及互联网金融平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及个性化推荐平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及机器学习平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及交通信息平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及健康风险评估平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及企业管理智能化平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及旅游数据平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及企业培训平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及视觉识别平台源码+论文+视频.zip
- 毕设和企业适用springboot智能制造类及数字营销平台源码+论文+视频.zip