package com.zhixin.flappybird.view;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import com.zhixin.flappybird.GameActicity;
import com.zhixin.flappybird.MainActivity;
import com.zhixin.flappybird.R;
import com.zhixin.flappybird.objects.Brid;
import com.zhixin.flappybird.objects.Floor;
import com.zhixin.flappybird.objects.Pipe;
import com.zhixin.flappybird.tool.GlobeVar;
import com.zhixin.flappybird.tool.MyEnum;
import com.zhixin.flappybird.tool.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private GameActicity gameActicity;
private Paint paint;
private Canvas mc;
private GameStatus status = GameStatus.RUNNING;
private Thread gameThread;
private Brid brid;
private Bitmap[] bridBitmap; // 小鸟图片
private Bitmap[] mNumBitmap; // 数字数组
private int mSingleGradeHeight;
private int mSingleGradeWidth;
private List<Pipe> pipes = new ArrayList<>(); // 地图管道的集合,也是障碍物
private final int WIDTH = Util.dp2px(getContext(), 60); // 管道的宽度
private final int PIPSPACE = Util.dp2px(getContext(), 200); // 管道相互之间的距离
private int SPEED = Util.dp2px(getContext(), 3); // 管道和地面向左移动的速度
private List<Floor> floors = new ArrayList<>(); // 地面版图的集合
private Boolean aSpeed = false; // 运行加速的标志
private final int meijifenjiasu = 5;
private final int xiaoniaoshangtiao = 32;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i(MyEnum.tip.getName(), "onSizeChanged");
for (int i = 0; i < 3; i++) {
Floor f = new Floor(loadImageByResId(R.mipmap.ground), getWidth() * i);
floors.add(f);
}
brid = new Brid(getContext(), getHeight(), bridBitmap);
Pipe p = new Pipe(loadImageByResId(R.mipmap.top), loadImageByResId(R.mipmap.last), getWidth() / 2 - WIDTH / 2, getHeight());
pipes.add(p);
for (int i = 0; i < 5; i++) {
int x = pipes.get(pipes.size() - 1).getX();
Pipe p1 = new Pipe(loadImageByResId(R.mipmap.top), loadImageByResId(R.mipmap.last), x + PIPSPACE, getHeight());
pipes.add(p1);
}
mSingleGradeHeight = (int) (h * 1 / 15f);
mSingleGradeWidth = (int) (mSingleGradeHeight * 1.0f / mNumBitmap[0].getHeight() * mNumBitmap[0].getWidth());
}
private void initBitmap() {
bridBitmap = initBridBitmap();
mNumBitmap = initNumBitmap();
}
private Bitmap[] initNumBitmap() {
Bitmap[] b = new Bitmap[10];
b[0] = (loadImageByResId(R.drawable.n0));
b[1] = (loadImageByResId(R.drawable.n1));
b[2] = (loadImageByResId(R.drawable.n2));
b[3] = (loadImageByResId(R.drawable.n3));
b[4] = (loadImageByResId(R.drawable.n4));
b[5] = (loadImageByResId(R.drawable.n5));
b[6] = (loadImageByResId(R.drawable.n6));
b[7] = (loadImageByResId(R.drawable.n7));
b[8] = (loadImageByResId(R.drawable.n8));
b[9] = (loadImageByResId(R.drawable.n9));
return b;
}
private Bitmap[] initBridBitmap() {
Bitmap[] b = new Bitmap[3];
b[0] = (loadImageByResId(R.mipmap.g1));
b[1] = (loadImageByResId(R.mipmap.g2));
b[2] = (loadImageByResId(R.mipmap.g3));
return b;
}
/**
* 绘制分数
*/
private void drawGrades(long grade) {
String gradeString = grade + "";
mc.save();
mc.translate(getWidth() / 2 - gradeString.length() * mSingleGradeWidth / 2,
1f / 8 * getHeight());
Rect r = new Rect(0, 0, mSingleGradeWidth, mSingleGradeHeight);
for (int i = 0; i < gradeString.length(); i++) {
String numStr = gradeString.substring(i, i + 1);
int num = Integer.valueOf(numStr);
mc.drawBitmap(mNumBitmap[num], null, r, null);
mc.translate(mSingleGradeWidth, 0);
}
mc.restore();
}
/**
* 游戏状态
*/
private enum GameStatus {
PAUSE, RUNNING, STOP
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
this.gameActicity = (GameActicity) context;
paint = new Paint();
paint.setAntiAlias(true); // 抗锯齿
paint.setDither(true); // 防抖动
getHolder().addCallback(this);
this.setKeepScreenOn(true); // 保持屏幕常亮
Log.i(MyEnum.tip.getName(), "GameView");
initBitmap();
}
private void drawPipe() {
movePip();
for (Pipe p : pipes) { // 画管道
p.draw(mc, WIDTH, getHeight());
}
removePipe();
addPipe();
}
/**
* 地图使用三张图片链接,当第一张图片过去屏幕左边立马移除加到最后
*/
private void drawFloor() {
moveFloor();
for (int i = 0; i < floors.size(); i++) {
Floor floor = floors.get(i);
floor.draw(mc, Util.dp2px(getContext(), 200));
if (floor.getX() + mc.getWidth() < 0) {
floors.remove(floor);
Floor f = new Floor(loadImageByResId(R.mipmap.ground), floors.get(floors.size() - 1).getX() + mc.getWidth());
floors.add(f);
}
}
}
/**
* 移动地板
*/
private void moveFloor() {
for (Floor f : floors) {
f.setX(f.getX() - SPEED);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(MyEnum.tip.getName(), "surfaceDestroyed");
status = GameStatus.STOP;
}
public void draw() {
mc = getHolder().lockCanvas(); // 锁住画布
mc.save();
drawBack(); // 画背景
drawPipe(); // 画管道
brid.draw(mc, Util.dp2px(getContext(), 200)); // 画鸟
drawFloor(); // 画地表
mc.restore();
drawGrades(GlobeVar.grade); // 画分数
getHolder().unlockCanvasAndPost(mc); // 解锁画布
}
private void drawBack() {
Bitmap bitmap = loadImageByResId(R.mipmap.bg);
Rect r = new Rect();
r.set(0, 0, getWidth(), getHeight());
mc.drawBitmap(bitmap, null, r, null);
}
/**
* 移除管道,如果管道已经在左边屏幕外边就移除,并且计分
*/
private void removePipe() {
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
if (pipe.getX() + WIDTH < 0) {
GlobeVar.grade++;
pipes.remove(pipe);
aSpeed = true;
}
}
}
/**
* 添加管道,如果管道的数量少于屏幕宽度/管道宽度加间隙加上3,就添加一个管道
*/
private void addPipe() {
int i = getWidth() / (WIDTH + PI
.Android安卓科研室.
- 粉丝: 4655
- 资源: 2238
最新资源
- 燃气表摇杆上料压装机sw18全套技术开发资料100%好用.zip
- 美容品、电脑、书检测17-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- Runtime Editor v4.4.6
- 考虑需求响应的微网优化调度matlab 程序采用粒子群算法,风力发电机、光伏发电机、储能装置、燃气轮机、柴油机组等主体,考虑负荷需求响应、soc约束等,程序模块化编程,注释清楚,有对应资料
- STM32F103C8T6实现用2.4G模块NRF2401传输室内温湿度并显示OLED 源程序
- GENEX-Probe-3.5软件及GENEX-Assistant-3.5软件使用及报告输出指导书
- , 免费,局域网,IP电话, SIP, VOIP, 视频通话,可与手机互通,手机上也安装 yealink
- 西门子S7-200PLC和MCGS组态的燃油锅炉控制系统
- Peer-to-peer LAN chat and file transfer, 局域网点对点消息传输和文件 传输,类似飞秋
- FreeModbus从机基于STM32平台移植源码
- 基于高频方波电压注入的永磁同步电机无感FOC 1.采用方波电压注入,减少了提取dq轴基频电流时LPF的使用,提高了系统的动态性能; 2.可实现带载起动和突加负载运行; 提供算法对应的参考文献和仿真模型
- 免费,局域网,IP电话, SIP, VOIP, 视频通话,可与手机互通,需要安装 手机版本
- 旅游景点数据集.zip
- 基于龙贝格观测器的永磁同步电机无感FOC 1.采用龙伯格观测器提取电机反电势,使用PLL从反电势中获得转子位置和速度信息 2.提供算法对应的参考文献和仿真模型,支持技术解答 仿真模型纯手工搭建
- 中国汽车基础软件发展白皮书4.0
- 背包、炸弹、瓶子、椅子、时钟、键盘、老鼠、人检测1-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈