02 Android 植物人大战僵尸-太阳花和豌豆射手卡片

1.放置太阳花和豌豆射手卡片

豌豆射手和太阳花

2.基本思路

  • 太阳花卡片的起始X位置 = 根据状态栏的X坐标 + 1个图片宽度
  • 豌豆射手卡片的起始X位置 = 根据状态栏的X坐标 + 2个图片宽度
        // 状态栏位置 + 一张图片宽度
        int statusX = (Config.screenWidth - Config.seekBank.getWidth()) / 2;
        int locationX = statusX + Config.seedFlower.getWidth();
        SeedFlower seedFlower = new SeedFlower(locationX, 0);
        gameLayout2.add(seedFlower);

        // 豌豆射手
        locationX = locationX + Config.seedPea.getWidth();
        SeedPea seedPea = new SeedPea(locationX, 0);
        gameLayout2.add(seedPea);

3.开发细节

3.1 卡片对象-基类
package com.su.botanywarzombies.model;

import android.graphics.Canvas;
import android.graphics.Paint;

public class BaseModel {

    // 对象的起始X坐标
    public int locationX;

    // 对象的起始 Y坐标
    public int locationY;

    // 是否还活着
    public boolean isLive;

    // 位置的自我绘制
    public void drawSelf(Canvas canvas, Paint paint) {

    }
}

3.1 卡片对象-接口类
package com.su.botanywarzombies.model;

public interface TouchAble {
    void onTouch();
}
3.2 太阳花卡片对象

继承基类并实现接口

package com.su.botanywarzombies.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.su.botanywarzombies.constant.Config;
import com.su.botanywarzombies.model.BaseModel;
import com.su.botanywarzombies.model.TouchAble;

public class SeedFlower extends BaseModel implements TouchAble {

    public SeedFlower(int locationX, int locationY) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.isLive = true;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isLive) {
            canvas.drawBitmap(Config.seedFlower, locationX, locationY, paint);
        }
    }

    @Override
    public void onTouch() {

    }

}
3.2 豌豆射手卡片对象
package com.su.botanywarzombies.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.su.botanywarzombies.constant.Config;
import com.su.botanywarzombies.model.BaseModel;
import com.su.botanywarzombies.model.TouchAble;

public class SeedPea extends BaseModel implements TouchAble {

    public SeedPea(int locationX, int locationY) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.isLive = true;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isLive) {
            canvas.drawBitmap(Config.seedPea, locationX, locationY, paint);
        }
    }

    @Override
    public void onTouch() {

    }

}
3.3 卡片对象的图片加载,这里需要指定宽高

除以 6 是宽度的估算

        int seedPicWidth = Config.seekBank.getWidth() / 6;
        int seedPicHeight = Config.seekBank.getHeight();
        Config.seedFlower = DeviceTools.resizeBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seed_flower), seedPicWidth, seedPicHeight);
        Config.seedPea = DeviceTools.resizeBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seed_pea), seedPicWidth, seedPicHeight);

4.demo 下载

https://github.com/sufadi/BotanyWarZombies

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值