注意:最新版本的代码我已经提交,加入了不少动画效果,优化了相关性能。
有特效版本:https://download.csdn.net/download/qq_20698983/10406847
无特效版本:https://download.csdn.net/download/qq_20698983/10383678
游戏效果图(今天更新成动图,但是看起来卡卡的,我程序很流畅的被弄成了这样):
当然你也可以在这个链接里面下载我的apk试玩
apk要求:Android version 6.0
链接:https://pan.baidu.com/s/1eTkO9mq 密码:w48l
##设计思路
一、实体类,封装一个动物头像。包含x,y坐标,图片(bitmap),一个id用于匹配,宽、高
二、布局方面一个Activity和一个自定义的View,主角当然是我们这个View。
三、在构造方法里面初始化游戏相关数据
public GameView(Context context, AttributeSet attr) {
super(context, attr);
screenHeight = DisplayUtil.getScreenHeight(context);
screenWidth = DisplayUtil.getScreenWidth(context);
// 音乐相关初始化
bgMedia = MediaPlayer.create(this.getContext(), R.raw.bg_game);
bgMedia.setOnCompletionListener(this);
bgMedia.start();
// swap = MediaPlayer.create(this.getContext(), R.raw.swap);
// int ave = screenWidth / (row + 2); // 将屏幕宽度分为 row + 2 等份
int ave = 0;
int size = (screenWidth - ave * 2) / row; // 其它两分为:舞台距离屏幕左右边的像素
ZooUtil.initZooData(size, size, this.getResources()); // 初始化动物头像数据
// 背景图片
background = BitmapFactory.decodeResource(this.getResources(), R.mipmap.game_bg);
floorBg = BitmapFactory.decodeResource(this.getResources(), R.mipmap.floor_bg);
/*
* 计算出舞台距离左边屏幕的距离
* 计算方式为:
* (屏幕总宽度 - 人物头像的宽 * 总行数) / 2
*/
int leftSpan = (screenWidth - ZooUtil.getAnimalWidth() * row) / 2;
int topSpan = (screenHeight - ZooUtil.getAnimalHeight() * col) / 3;
// 将游戏舞台的坐标、高宽保存起来
StageUtil.initStage(leftSpan, topSpan,
leftSpan + ZooUtil.getAnimalWidth() * row,
topSpan + ZooUtil.getAnimalHeight() * col);
// 实例化画笔
paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);