可以实现手机射击效果,控制射击方向, 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
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 该项目为一个集数据抓取与展示一体的ACM队员数据系统,基于Django、python实现。.zip
- 辅助背单词软件,基于艾宾浩斯记忆曲线(其实背啥都行)的Python重构版,增加在线查词与翻译等功能.zip
- 基于C开发的命令行输入输出流重定向与实时分析工具_支持快捷按键和文本框输入实时过滤计算分析多格式结果呈现文本提示弹窗曲线表格支持批量测试和日志抓取_用于开发调试协议分.zip
- 各种有用的web api 基于Golang, Python(tornado django scrapy gevent).zip
- 华南理工大学找到卷王,基于 Python 的综测系统数据爬虫.zip
- 湖南大学(HNU)数据库系统课程大作业 ATM系统 前端基于Python的PyQt5,后端基于MySQL.zip
- (新闻爬虫),基于python+Flask+Echarts,实现首页与更多新闻页面爬取
- 基于 Flask + Requests 的全平台音乐接口 Python 版.zip
- 基于 FFmpeg ,使用 Python 开发的批量媒体文件格式转换器。.zip
- 基于 CAI 的 OneBot Python 实现.zip
- 基于 nonebot2 开发的消息交互式 Python 解释器,依赖 docker SDK.zip
- 基于 Python 3 + Django 2 开发的用于适配手机的简单 Jenkins 构建平台.zip
- Python 语言的爬楼梯问题实现-计算爬到第 n 级台阶的方法数
- 基于 Napcat, NcatBot, JMComic-Crawler-Python 的 QQ 机器人。.zip
- 基于 Python Tornado 的博客程序 (练习).zip
- 基于 Python 3.5 + Django 2.0 开发的简单个人博客.zip


