您的位置:首页 > 移动开发 > Android开发

边做游戏边学android—4(飞机大战③添加敌机和碰撞检测)

2015-05-16 23:26 411 查看
敌机的不会只有1个,而且也不会只有一种敌机。所以敌机也可以建一个继承与gameobject的父类。命名为EnemyPlane,代码如下:

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坐标随机。出现以后就向下移动。到底部后消失。再从顶部创建。碰到飞机的话会触发飞机爆炸动画。到这里就是一个简单的躲避掉落物品的游戏了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐