边做游戏边学android—4(飞机大战③添加敌机和碰撞检测)
2015-05-16 23:26
411 查看
敌机的不会只有1个,而且也不会只有一种敌机。所以敌机也可以建一个继承与gameobject的父类。命名为EnemyPlane,代码如下:
创建一个继承于EnemyPlane的类:
在factary类里添加创建敌机的方法:
然后在mainview里添加创建敌机:
运行结果如下图:
由于敌机有多个。用到了list。开始出现的位置是顶部。x坐标随机。出现以后就向下移动。到底部后消失。再从顶部创建。碰到飞机的话会触发飞机爆炸动画。到这里就是一个简单的躲避掉落物品的游戏了
package com.example.object; import android.content.res.Resources; import android.graphics.Canvas; public class EnemyPlane extends GameObject { protected int score; protected int blood; protected int bloodVolume; protected boolean isExplosion; protected boolean isVisible; public EnemyPlane(Resources resources) { super(resources); // TODO Auto-generated constructor stub } public void initial(int arg0,float arg1, float arg2){ } @Override protected void initBitmap() { // TODO Auto-generated method stub } @Override public void drawSelf(Canvas canvas) { // TODO Auto-generated method stub } @Override public void release() { // TODO Auto-generated method stub } public void logic(){ if(object_y < screen_height){ object_y += speed; } else{ isAlive = false; } if(object_y + object_height >0){ isVisible = true; } else{ isVisible = false; } } //碰撞检测 public boolean isCollide(GameObject obj){ if(object_x + object_width <= obj.getObject_x()){ return false; } else if(obj.getObject_x() +obj.getObject_width() <object_x){ return false; } else if(object_y +object_width < obj.getObject_y()){ return false; } else if(obj.getObject_x() + obj.getObject_height() <object_y){ return false; } return true; } public boolean isCanCollide() { return isAlive &&isVisible; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getBlood() { return blood; } public void setBlood(int blood) { this.blood = blood; } public int getBloodVolume() { return bloodVolume; } public void setBloodVolume(int bloodVolume) { this.bloodVolume = bloodVolume; } public boolean isExplostion(){ return isExplosion; } }
创建一个继承于EnemyPlane的类:
package com.example.object; import java.util.Random; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import com.example.planewars.R; public class SmallPlane extends EnemyPlane { private static int currentCount = 0; private Bitmap smallPlane; public static int sumCount =5; public SmallPlane(Resources resources) { super(resources); // TODO Auto-generated constructor stub initBitmap(); this.score = 100; } public void initial(int arg0,float arg1, float arg2){ if (getCurrentCount() < sumCount) { isAlive = true; bloodVolume = 1; blood = 1; // speed = ran.nextInt(8) + 8 * arg0; speed = 7; Random ran = new Random(); object_x = ran.nextInt((int) (screen_width - object_width)); object_y = 0; setCurrentCount(getCurrentCount() + 1); } } public void initBitmap(){ smallPlane = BitmapFactory.decodeResource(resources, R.drawable.small); object_width = smallPlane.getWidth(); object_height = smallPlane.getHeight()/3; } public void drawSelf(Canvas canvas){ // if(isVisible){ canvas.save(); canvas.clipRect(object_x,object_y, object_x + object_width, object_y + object_height); canvas.drawBitmap(smallPlane, object_x, object_y,paint); canvas.restore(); // } logic(); } public void release(){ if(!smallPlane.isRecycled()){ smallPlane.recycle(); } } public int getCurrentCount() { return currentCount; } public void setCurrentCount(int currentCount) { SmallPlane.currentCount = currentCount; } }
在factary类里添加创建敌机的方法:
package com.example.factory; import android.content.res.Resources; import com.example.object.GameObject; import com.example.object.MyPlane; import com.example.object.SmallPlane; public class GameObjectFactory { public GameObject createMyPlane(Resources resources){ return new MyPlane(resources); } public GameObject createSmallPlane(Resources resource){ return new SmallPlane(resource); } }
然后在mainview里添加创建敌机:
package com.example.view; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.os.Message; import android.view.MotionEvent; import android.view.SurfaceHolder; import com.example.object.EnemyPlane; import com.example.object.GameObject; import com.example.object.MyPlane; import com.example.object.SmallPlane; import com.example.factory.GameObjectFactory; public class MainView extends BaseView{ private MyPlane myPlane; private GameObjectFactory factory; private boolean isPlay; private boolean isTouchPlane; private List<EnemyPlane> enemyPlanes; private int speedTime; public MainView(Context context) { super(context); // TODO Auto-generated constructor stub speedTime = 1; factory = new GameObjectFactory(); enemyPlanes = new ArrayList<EnemyPlane>(); myPlane = (MyPlane) factory.createMyPlane(getResources()); myPlane.setMainView(this); for(int i=0;i<SmallPlane.sumCount;i++){ SmallPlane smallPlane = (SmallPlane) factory.createSmallPlane(getResources()); enemyPlanes.add(smallPlane); } thread = new Thread(this); } public void surfaceCreated(SurfaceHolder arg0){ super.surfaceCreated(arg0); initBitmap(); for(GameObject obj :enemyPlanes){ obj.setScreenWH(screen_width, screen_height); } myPlane.setScreenWH(screen_width, screen_height); myPlane.setAlive(true); if(thread.isAlive()){ thread.start(); } else { thread = new Thread(this); thread.start(); } } public void surfaceDestoryed(SurfaceHolder arg0){ super.surfaceDestroyed(arg0); release(); } public boolean onTouchEvent(MotionEvent event){ if(event.getAction() == MotionEvent.ACTION_UP){ isTouchPlane = false; } else if(event.getAction() == MotionEvent.ACTION_DOWN){ float x = event.getX(); float y = event.getY(); if(x > myPlane.getObject_x() && x < myPlane.getObject_x() + myPlane.getObject_width()&& y > myPlane.getObject_y() && y < myPlane.getObject_y() + myPlane.getObject_height()){ // if(isPlay){ isTouchPlane = true; // } } } else if(event.getAction() == MotionEvent.ACTION_MOVE && event.getPointerCount() ==1){ if(isTouchPlane){ float x = event.getX(); float y = event.getY(); if(x > myPlane.getMiddle_x() + 20){ if(myPlane.getMiddle_x() + myPlane.getSpeed() < screen_width){ myPlane.setMiddle_x(myPlane.getMiddle_x() + myPlane.getSpeed()); } } else if(x < myPlane.getMiddle_x() - 20) { if(myPlane.getMiddle_x() - myPlane.getSpeed() > 0) { myPlane.setMiddle_x(myPlane.getMiddle_x() - myPlane.getSpeed()); } } else if(y > myPlane.getMiddle_y() + 20){ if( myPlane.getMiddle_y() + myPlane.getSpeed() < screen_height) { myPlane.setMiddle_y(myPlane.getMiddle_y() + myPlane.getSpeed()); } } else if(y < myPlane.getMiddle_y() - 20){ if(myPlane.getMiddle_y() - myPlane.getSpeed() > 0){ myPlane.setMiddle_y(myPlane.getMiddle_y() - myPlane.getSpeed()); } } } } return true; } public void initBitmap(){ } public void initObject(){ for(EnemyPlane obj:enemyPlanes){ if(obj instanceof SmallPlane){ obj.initial(speedTime, 0, 0); continue; } } } public void release(){ for(GameObject obj:enemyPlanes){ obj.release(); } myPlane.release(); } public void drawSelf(){ try{ canvas = sfh.lockCanvas(); canvas.drawColor(Color.BLACK); //canvas.scale(scalex, scaley, 0, 0); myPlane.drawSelf(canvas); for(EnemyPlane obj:enemyPlanes){ obj.drawSelf(canvas); if(obj.isCanCollide() && myPlane.isAlive()){ if(obj.isCollide(myPlane)){ myPlane.setAlive(false); } } } } catch(Exception err){ err.printStackTrace(); }finally{ if(canvas != null); sfh.unlockCanvasAndPost(canvas); } } public void viewLogic(){ for(EnemyPlane obj:enemyPlanes){ if(obj instanceof SmallPlane){ if(obj.getObject_y() > screen_height){ SmallPlane sPlane = (SmallPlane)obj; sPlane.setCurrentCount(sPlane.getCurrentCount() - 1); } } } } public void run(){ while(threadFlag){ initObject(); drawSelf(); viewLogic(); // if(!isPlay){ // synchronized(thread){ // try{ // thread.wait(); // } catch(InterruptedException e){ // e.printStackTrace(); // } // } // } // } try{ Thread.sleep(200); } catch(InterruptedException e ) { e.printStackTrace(); } } } }
运行结果如下图:
由于敌机有多个。用到了list。开始出现的位置是顶部。x坐标随机。出现以后就向下移动。到底部后消失。再从顶部创建。碰到飞机的话会触发飞机爆炸动画。到这里就是一个简单的躲避掉落物品的游戏了
相关文章推荐
- 边做游戏边学android—5(飞机大战④子弹发射和碰撞检测)
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 08.游戏界面 碰撞检测
- 状态添加Android游戏开发十日通(4)-行走,跳跃,碰撞检测
- 【cocos2d-x-3.2】【高仿微信打飞机系列二】【敌机 碰撞检测 爆炸管理】
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 04.游戏界面 主角飞机的添加
- unity之飞机大战和碰撞检测、射线
- unity学习之飞机大战制作 关键点(六) 子弹与敌机之间的碰撞
- 跑酷类游戏------quick-Cocos2d-X lua添加瓦片地图、瓦片地图的移动以及碰撞检测
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 07.游戏界面 敌机的产生与移动
- pygame编写飞机大战(8)-碰撞检测
- Android游戏开发之碰撞检测(矩形碰撞、圆形碰撞、像素碰撞)
- Cocos2d-x碰撞检测原理与英雄要打死怪物--之游戏开发《赵云要格斗》(7)
- 游戏开发中常见的几种碰撞检测
- HTML5吃豆豆游戏开发实战(四)2d碰撞检测、重构-第二篇
- 飞机大战开发04子弹与敌机的碰撞检测
- Android游戏开发基础part7--碰撞检测
- [源码解读]Silverlight 4 中对不规则对象进行碰撞检测(在游戏中常使用的是否碰撞怪物边界等原理)
- unity3D游戏开发实战原创视频讲座系列3之全民飞机大战游戏开发
- unity初识之2d游戏中做碰撞检测
- Android游戏中的碰撞检测