收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > Android > Android游戏 > 正文

首页 - PHP - 数据库 - 操作系统 - 游戏开发 - JS - Android - MySql - Redis - MongoDB - Win8 - Shell编程 - DOS命令 - jQuery - CSS样式 - Python - Perl

Access - Oracle - DB2 - SQLServer - MsSql2008 - MsSql2005 - Sqlite - PostgreSQL - node.js - extjs - JavaScript vbs - Powershell - Ruby

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

敌机的不会只有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坐标随机。出现以后就向下移动。到底部后消失。再从顶部创建。碰到飞机的话会触发飞机爆炸动画。到这里就是一个简单的躲避掉落物品的游戏了

边做游戏边学android—5(飞机大战④子弹发射和碰撞检测)
飞机是要发射子弹的,子弹也不止一种,先创建子弹的基类。代码如下:packagecom.example.object;importandroid.content.res.Resources;importandroid.graphics.Canvas;publicclassB

总结了近百个Android优秀开源项目,覆盖Android开发的每个角落
Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用。其中也有许许多多的开发者提供了应用开源项

Android游戏框架的搭建
通常情况下,游戏开发的基本框架中,一般包括以下模块:窗口管理(Windowmanagement):该模块负责在Android平台上创建、运行、暂停、恢复游戏界面等功能

本周排行

更新排行

强悍的草根IT技术社区,这里应该有您想要的! 友情链接:b2b电子商务
Copyright © 2010 Gimoo.Net. All Rights Rreserved  京ICP备05050695号