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期末大作业 - 运行中有什么问题可以私聊博主,本人高级安卓工程师,主页置顶有常见爆红解决的方法,以及更多代码项目 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
资源推荐
资源详情
资源评论
收起资源包目录
FlappyBird2Android-master.zip (69个子文件)
FlappyBird2Android-master
src
androidTest
java
com
zhixin
flappybird
ExampleInstrumentedTest.java 752B
test
java
com
zhixin
flappybird
ExampleUnitTest.java 398B
main
java
com
zhixin
flappybird
adapter
Grade_item.java 1KB
sql
FactorySql.java 6KB
tool
MyEnum.java 207B
GlobeVar.java 239B
Util.java 496B
AboutActivity.java 349B
objects
Brid.java 2KB
Pipe.java 3KB
Floor.java 804B
view
GameView.java 11KB
Grade.java 6KB
entity
User.java 754B
lombok.config 567B
GameActicity.java 3KB
server
MyService.java 2KB
MainActivity.java 6KB
res
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
mipmap-hdpi
ground.png 4KB
gameover.png 11KB
rank.png 12KB
g2.png 2KB
last.png 19KB
rate.png 7KB
ic_launcher_round.png 5KB
sky.png 16KB
title.png 27KB
bg.png 25KB
top.png 18KB
begin.png 10KB
g3.png 2KB
ic_launcher.png 3KB
g1.png 2KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 276B
ic_launcher_round.xml 276B
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
values
colors.xml 300B
strings.xml 76B
styles.xml 394B
layout
grade_items.xml 766B
activity_game.xml 1KB
activity_main.xml 3KB
main_dialog.xml 708B
activity_grade.xml 4KB
activity_about.xml 2KB
drawable
ic_launcher_background.xml 6KB
n0.png 3KB
n1.png 3KB
n2.png 3KB
n4.png 3KB
n3.png 3KB
n7.png 3KB
n6.png 3KB
n9.png 3KB
n8.png 3KB
n5.png 3KB
raw
game.mp3 1.16MB
AndroidManifest.xml 1KB
proguard-rules.pro 772B
build.gradle 1KB
app.iml 16KB
共 69 条
- 1
资源评论
.Android安卓科研室.
- 粉丝: 4419
- 资源: 2453
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功