整体思路
先背景
绘制玩家飞机,玩家飞机随触摸点移动
绘制boss飞机,自动移动
绘制子弹,分别从玩家飞机和boss飞机发射
绘制爆装效果,由子弹触发
绘制血量,玩家血量为0触发死亡画面,boss血量为零触发通过
绘制开始界面,点击开机进入游戏
1.如何绘制滚动的背景图片
第一张图y坐标往下递减实现滚动,
第二张图的底部坐标等于第一张图的顶部坐标,当第一张图片的顶部坐标大于屏幕,令其底部坐标等于第二张图的顶部坐标实现循环滚动。
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
class BackGround {
private int y1;
private int y2;
private Bitmap bitmap;
public BackGround(Bitmap bitmap){
this.bitmap = bitmap;
y1=0;
y2=y1-bitmap.getHeight();
}
public void draw(Canvas canvas,Paint paint){
logic();
canvas.drawBitmap(bitmap,0,y1,paint);
canvas.drawBitmap(bitmap,0,y2,paint);
}
public void logic() {
y1+=10;
y2+=10;
if (y1>=MySurfaceView.height){
y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
}
if (y2>=MySurfaceView.height){
y2=y1-bitmap.getHeight();
}
}
2.如何绘制飞机
用canvas调用drawBitmap
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Myplane {
private Bitmap bitmap;
private Bitmap bitmapHp;
private int x,y;
private int width,height;
private boolean noCollision;
private int noCollisionCount;
private int hp = 3;
public Myplane(Bitmap bitmap, Bitmap bitmapHp){
this.bitmap = bitmap;
this.bitmapHp = bitmapHp;
x = MySurfaceView.width/2-bitmap.getWidth()/2;
y = MySurfaceView.height-bitmap.getHeight();
width = bitmap.getWidth();
height = bitmap.getHeight();
}
public void draw(Canvas canvas,Paint paint){
if (noCollision){
noCollisionCount++;
if (noCollisionCount%10==0){
canvas.drawBitmap(bitmap,x,y,paint);//飞机闪烁
}
if (noCollisionCount>100){//无敌时间
noCollision = false;
noCollisionCount = 0;
}
}else {
//非无敌状态
canvas.drawBitmap(bitmap,x,y,paint);
}
for (int i = 0; ix&&exy&&eyMySurfaceView.height){
y=MySurfaceView.height-height;
}
}
}
}
public boolean isCollision(Bullet bullet){
if (noCollision){
return false;
}else{
if (bullet.getX()>x&&bullet.getX()
3.如何绘制子弹
用canvas调用drawBitmap
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
public class Bullet {
private Bitmap bitmap;
private int x, y;
private int speed = 10;
private boolean isDead;
private int type;
public Bullet(Bitmap bitmap, int x, int y,int type) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
this.type = type;
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bitmap, x, y, paint);
logic();
}
public void logic() {
switch (type){
//玩家子弹
case 0:
y -= speed;
if (y < 0) {
isDead = true;
}
break;
//Boss子弹
case 1:
y += speed;
if (y < 0) {
isDead = true;
}
break;
}
}
public boolean isDead() {
return isDead;
}
public Bitmap getBitmap(){
return bitmap;
}
public int getX(){
return x;
}
public int getY() {
return y;
}
}
4.如何判断碰撞
子弹的左右x坐标在飞机左右x坐标里面
public boolean isCollision(Bullet bullet) {
if (noCollision) {
return false;
} else {
if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {
noCollision = true;
if (hp >= 0) {
hp--;
}
return true;
}
}
return false;
}
5.如何绘制爆装效果
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
public class Boom {
private Bitmap bitmap;
private int x,y;
private int totalFrame;
private int currentFrame;//当前显示的第几幅画面
private int frameH,frameW;
private boolean isEnd;
public Boom(Bitmap bitmap,int x,int y,int totalFrame){
this.bitmap = bitmap;
this.x = x;
this.y = y;
this.totalFrame = totalFrame;
frameH = bitmap.getHeight();
frameW = bitmap.getWidth()/totalFrame;
}
public void draw(Canvas canvas, Paint paint){
canvas.save();
canvas.clipRect(x,y,x+frameW,y+frameH);
canvas.drawBitmap(bitmap,x-currentFrame*frameW,y,paint);
canvas.restore();
logic();
}
public void logic(){
if (currentFrame
6.如何添加音效
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class GameSoundPool {
private SoundPool soundPool;
private int s1;
private int s2;
public GameSoundPool(Context context){
this.soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
s1 = soundPool.load(context,R.raw.shoot,1);
s2 = soundPool.load(context,R.raw.explosion2,1);
}
public void playSound(int s){
switch (s){
case 1:
soundPool.play(s1,1,1,1,0,1.0f);
break;
case 2:
soundPool.play(s2,1,1,1,0,1.0f);
break;
}
}
}
定义变量时常常用到封装。
需要用某个类的功能但又想扩展一些功能,可以继承那个类。
public class MySurfaceView extends SurfaceView
实现接口
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable
implements SurfaceHolder.Callback,Runnable