(游戏)打飞机01:前言 传送门
(游戏)打飞机02:游戏背景滚动 传送门
(游戏)打飞机03:控制玩家飞机 传送门
(游戏)打飞机04:绘画敌机、添加子弹 传送门
(游戏)打飞机05:处理子弹,击中敌机,添加计分板 传送门
(游戏)打飞机06:后续 传送门
游戏背景滚动效果
packagecom.example.administrator.myapplication;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;importandroid.view.WindowManager;importjava.util.ArrayList;importjava.util.List;/*** Created by Administrator on 2018/8/9.*/
public class dafeijiGameView extends SurfaceView implementsSurfaceHolder.Callback,Runnable{private Bitmap my;//自己
private Bitmap baozha;//爆炸
private Bitmap bg;//背景
private Bitmap diren;//敌人
private Bitmap zidan;//子弹
private Bitmap erjihuancun;//二级缓存
private WindowManager windowManager;//获得界面长宽高
private int display_w; //界面的宽
private int display_h; //界面的高
private List gameImage = newArrayList();publicdafeijiGameView(Context context) {super(context);
getHolder().addCallback(this);
}private voidinit(){//加载照片
my=BitmapFactory.decodeResource(getResources(),R.drawable.my);
baozha=BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
bg=BitmapFactory.decodeResource(getResources(),R.drawable.bg);
diren=BitmapFactory.decodeResource(getResources(),R.drawable.diren);
zidan=BitmapFactory.decodeResource(getResources(),R.drawable.zidan);
erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
gameImage.add(new BeijingImage(bg)); //先加入背景照片
}private interfaceGameImage{publicBitmap getBitmap();public intgetX();public intgetY();
}//负责背景照片的处理
private class BeijingImage implementsGameImage{privateBitmap bg;privateBeijingImage(Bitmap bg){this.bg=bg;
newBitmap=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
}private Bitmap newBitmap = null;private int height = 0;publicBitmap getBitmap(){
Paint p= newPaint();
Canvas canvas= newCanvas(newBitmap);
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,height,display_w,display_h+height),p);
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,-display_h+height,display_w,height),p);
height++;if(height==display_h){
height=0;
}returnnewBitmap;
}public intgetX(){return 0;
}public intgetY(){return 0;
}
}private boolean state = false;privateSurfaceHolder holder;//绘画中心
public voidrun() {
Paint p1= newPaint();try{while(true){
Canvas newCanvas= newCanvas(erjihuancun);for(GameImage image:gameImage){
newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
}
Canvas canvas=holder.lockCanvas();
canvas.drawBitmap(erjihuancun,0,0,p1);
holder.unlockCanvasAndPost(canvas);
Thread.sleep(10);
}
}catch(Exception e){
}
}
@Overridepublic voidsurfaceCreated(SurfaceHolder holder) {
}
@Overridepublic voidsurfaceDestroyed(SurfaceHolder holder) {
state= false;
}
@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, intheight) {//得到屏幕的宽和高
display_w=width;
display_h=height;
init();this.holder=holder;
state= true;new Thread(this).start();
}
}
dafeijiView.java
packagecom.example.administrator.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;public class MainActivity extendsAppCompatActivity{
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//去掉标题//requestWindowFeature(Window.FEATURE_NO_TITLE);
dafeijiGameView view = new dafeijiGameView(this);
setContentView(view);
}
}
MainActivity.java
实现过程
使用线程绘画游戏
public voidrun() {
Paint p1= newPaint();try{while(true){
Canvas newCanvas= newCanvas(erjihuancun);for(GameImage image:gameImage){
newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
}
Canvas canvas=holder.lockCanvas();
canvas.drawBitmap(erjihuancun,0,0,p1);
holder.unlockCanvasAndPost(canvas);
Thread.sleep(10);
}
}catch(Exception e){
}
}
绘画移动的背景图片
publicBitmap getBitmap(){
Paint p= newPaint();
Canvas canvas= newCanvas(newBitmap);
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,height,display_w,display_h+height),p);
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,-display_h+height,display_w,height),p);
height++;if(height==display_h){
height=0;
}returnnewBitmap;
}
surfaceCreated:创建时初始化图片并开启线程绘画游戏
@Overridepublic voidsurfaceCreated(SurfaceHolder holder) {
}
@Overridepublic voidsurfaceDestroyed(SurfaceHolder holder) {
state= false;
}
@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, intheight) {//得到屏幕的宽和高
display_w=width;
display_h=height;
init();this.holder=holder;
state= true;new Thread(this).start();
}
初始化函数。加载图片
private voidinit(){//加载照片
my=BitmapFactory.decodeResource(getResources(),R.drawable.my);
baozha=BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
bg=BitmapFactory.decodeResource(getResources(),R.drawable.bg);
diren=BitmapFactory.decodeResource(getResources(),R.drawable.diren);
zidan=BitmapFactory.decodeResource(getResources(),R.drawable.zidan);
erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
gameImage.add(new BeijingImage(bg)); //先加入背景照片
}
线程中使用二级缓存绘画背景,每隔10ms绘画一次
Canvas canvas =holder.lockCanvas();
canvas.drawBitmap(erjihuancun,0,0,p1);
holder.unlockCanvasAndPost(canvas);
Thread.sleep(10);
图片坐标的移动
Paint p = newPaint();
Canvas canvas= newCanvas(newBitmap);
//第一张图片
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,height,display_w,display_h+height),p);
//第二张图片
canvas.drawBitmap(bg,new Rect(0,0,bg.getWidth(),bg.getHeight()),new Rect(0,-display_h+height,display_w,height),p);
height++;
宽度不发生改变,长度发生变化,两张图片的高不断升高(height++)
canvas.drawBitmap(bitmap, srcRect, dstRect, paint) :在图片上绘制图片 传送门
1参:要绘制的图片
2参: 是对图片进行裁截,可以传null,传null则表示显示整个图片
3参:目标区域,即把原区域改变参数,得到自己想要的图片及位置,是图片在Canvas画布中显示的区域
4参:paint画笔,可以为null
Rect(left,top,right,bottom) :用于表示坐标系中的一块矩形区域
left:左
top:上
right:右
bottom:下
if(height==display_h){
height=0;
}
其实只是两张照片拼接而成,两张图片不断的向上移动。开始时,第一张在手机屏幕中间,第二张在手机屏幕下方,第一张图片超出手机最上方的范围时候拼接到第一张图片下面,下一张图片不断往上运动形成新的第一张图片在手机屏幕中间,两张背景图片就这样不断的进行循环
在MainActivity.java中添加这一句可以去掉标题
requestWindowFeature(Window.FEATURE_NO_TITLE);