收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > 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—5(飞机大战④子弹发射和碰撞检测)

飞机是要发射子弹的,子弹也不止一种,先创建子弹的基类。代码如下:

package com.example.object;

import android.content.res.Resources;
import android.graphics.Canvas;

public class Bullet extends GameObject {
    protected int harm;

    public Bullet(Resources resources) {
        super(resources);
        // TODO Auto-generated constructor stub
    }

    @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 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_height <= obj.getObject_y()){
            return false;
        }
        else if(obj.getObject_y() + obj.getObject_height() <=object_y){
            return false;
        }
        return true;
    }

    public int getHarm() {
        return harm;
    }

    public void setHarm(int harm) {
        this.harm = harm;
    }



}

再创建子弹类:

package com.example.object;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import com.example.planewars.R;

public class MyBullet extends Bullet {
    private Bitmap bullet;
    private int sum=3;
    private int current=0;


    public MyBullet(Resources resources) {
        super(resources);
        // TODO Auto-generated constructor stub
        initBitmap();

        this.harm =1;
    }

    public void initial(int arg0,float arg1,float arg2) {
        if(current< sum){
        isAlive = true;
        speed = 10;
        object_x = arg1 - object_width/2;
        object_y = arg2 - object_height;
        current++;
        }
    }

    public void initBitmap(){
        bullet = BitmapFactory.decodeResource(resources, R.drawable.bullet);
        object_width = bullet.getWidth();
        object_height = bullet.getHeight();
    }

    public void drawSelf(Canvas canvas){
        if(isAlive){
            canvas.save();
            canvas.clipRect(object_x, object_y, object_x + object_width, object_y + object_height);
            canvas.drawBitmap(bullet, object_x, object_y,paint);
            canvas.restore();
            logic();
        }
    }

    public void release(){
        if(!bullet.isRecycled()){
            bullet.recycle();
        }
    }

    public void logic(){
//      if(object_y >=0) {
//          object_y -= speed;
//      } else {
//          isAlive = false;
//      }
        object_y -= speed;
    }

    public boolean isCollide(GameObject obj){
        return super.isCollide(obj);
    }

}

子弹是在飞机里发射出来的,修改一下myplane的代码。修改后如下:

package com.example.object;

import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import com.example.planewars.R;
import com.example.view.MainView;

import com.example.factory.GameObjectFactory;
import com.example.interfaces.IMyPlane;

public class MyPlane extends GameObject implements IMyPlane {
    private float middle_x;
    private float middle_y;
    private Bitmap myplane;
    private Bitmap myplaneexplosion;
    private MainView mainView;
    private GameObjectFactory factory;
    private List<Bullet> bullets;
    private Bitmap bullet;

    public MyPlane(Resources resources) {
        super(resources);
        // TODO Auto-generated constructor stub
        initBitmap();
        factory = new GameObjectFactory();
        bullets = new ArrayList<Bullet>();
        //createBullet();
        this.speed = 8;
    }

    public void setMainView(MainView mainView) {
        this.mainView = mainView;
    }

    public void setScreenWH(float screen_width, float screen_height) {
        super.setScreenWH(screen_width, screen_height);
        object_x = screen_width/2 - object_width/2;
        object_y = screen_height - object_height;
        middle_x = object_x + object_width/2;
        middle_y = object_y + object_height/2;
    }

    @Override
    protected void initBitmap() {
        // TODO Auto-generated method stub
        myplane = BitmapFactory.decodeResource(resources, R.drawable.myplane);
        myplaneexplosion = BitmapFactory.decodeResource(resources, R.drawable.myplaneexplosion);
        object_width = myplane.getWidth()/2;
        object_height = myplane.getHeight();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        // TODO Auto-generated method stub
        if(isAlive){
            int x = (int) (currentFrame * object_width);
            canvas.save();
            canvas.clipRect(object_x, object_y,object_x + object_width, object_y + object_height);
            canvas.drawBitmap(myplane, object_x -x, object_y, paint);
            canvas.restore();
            currentFrame++;
            if(currentFrame >= 2){
                currentFrame = 0;
            }
        } else {
            int x = (int) (currentFrame * object_width);
            canvas.save();
            canvas.clipRect(object_x, object_y,object_x + object_width, object_y + object_height);
            canvas.drawBitmap(myplaneexplosion, object_x -x, object_y, paint);
            canvas.restore();
            currentFrame++;
            if(currentFrame >= 2){
                currentFrame = 1;
            }
        }


    }

    public void shoot(Canvas canvas,List<EnemyPlane> planes){
        for(Bullet obj:bullets){
            if(obj.isAlive()){
                for(EnemyPlane pobj:planes){
                    if(pobj.isCanCollide()){
                        if(obj.isCollide(pobj)){
                            pobj.attacked(obj.getHarm());
                        }
                    }
                }
                obj.drawSelf(canvas);
//          bullet = BitmapFactory.decodeResource(resources, R.drawable.bullet);
//          canvas.save();
//          canvas.clipRect(object_x, object_y, object_x + object_width, object_y + object_height);
//          canvas.drawBitmap(bullet, object_x, object_y,paint);
//          canvas.restore();
            }
        }
    }

    public void createBullet(){
        for(int i=0;i<5;i++){
            MyBullet bullet =(MyBullet) factory.createBullet(resources);
            bullets.add(bullet);
        }
    }

    public void initBullet(){
        for(Bullet obj:bullets){
//          if(obj.isAlive()){
                obj.initial(0, middle_x, middle_y);
//              break;
//          }
        }
    }

    public void isBulletoverTime(){
        createBullet();
    }

    @Override
    public void release() {
        // TODO Auto-generated method stub
        if(!myplane.isRecycled()){
            myplane.recycle();
        }
        if(!myplaneexplosion.isRecycled()){
            myplaneexplosion.recycle();
        }
    }

    public float getMiddle_x() {
        return middle_x;
    }

    public void setMiddle_x(float middle_x) {
        this.middle_x = middle_x;
        this.object_x =  middle_x - object_width/2;
    }

    public float getMiddle_y() {
        return middle_y;
    }

    public void setMiddle_y(float middle_y) {
        this.middle_y = middle_y;
        this.object_y = middle_y - object_height/2;
    }


}

当然也还要在mianview里初始化视图和draw里加上创建子弹的代码。修改之后的代码如下:

    public void initObject(){
        myPlane.isBulletoverTime();
        myPlane.initBullet();
        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.WHITE);
            //canvas.scale(scalex, scaley, 0, 0);
            myPlane.drawSelf(canvas);
            myPlane.shoot(canvas, enemyPlanes);
            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);
            }
    }

游戏运行截图:
查看图片

再加上游戏分数。主界面,和游戏结束界面,飞机大战就基本完成了,当然,为了使游戏内容丰富一点,也可以添加多种子弹和敌机。然后还有游戏里添加物品。

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

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

Android中SurfaceView的使用详解
通过之前介绍的如何自定义View,我们知道使用它可以做一些简单的动画效果。它通过不断循环的执行View.onDraw方法,每次执行都对内部显示的图形做一

本周排行

更新排行

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