1、整体实现思路
(1)打开页面,背景开始走动;
(2)游戏开始,飞机开始不断发射子弹,敌人随机出现在上方;
(3)当敌人碰到子弹,敌人扣除一定血量,敌人消失;
(4)当敌人和飞机相遇,飞机扣除血量,飞机血量为零时,死亡,结束游戏;
2、如何绘制循环滚动的背景图片
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
public 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){
logic();
Paint paint = new Paint();
canvas.drawBitmap(bitmap,0,y1,paint);
canvas.drawBitmap(bitmap,0,y2,paint);
}
public void logic(){
y1+=5;
y2+=5;
if(y1>=MySurfaceView.Height){
y1=y2-bitmap.getHeight();
}
if(y2>=MySurfaceView.Height){
y2=y1-bitmap.getHeight();
}
}
}
3、如何绘制飞机
绘制敌机:
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import java.util.function.BooleanSupplier;
public class BossPlane {
private Bitmap bitmap;
private int x,y;
private int frameW,frameH;
public BossPlane(Bitmap bitmap){
this.bitmap = bitmap;
this.frameW = bitmap.getWidth()/10;
this.frameH = bitmap.getHeight();
x=MySurfaceView.width/2-frameW/2;
}
public void draw(Canvas canvas, Paint paint){
canvas.clipRect(x,y,x+frameW,y+frameH);
canvas.drawBitmap(bitmap,x,y,paint);
}
}
绘制自己的飞机:
public class MyPlane {
private final Bitmap bitmap;
private int y;
private int x;
private int width;
private int height;
public MyPlane(Bitmap bitmap) {
this.bitmap = bitmap;
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){
canvas.drawBitmap(bitmap, x, y, paint);
}
public void touchEvent(MotionEvent event){
if (event.getAction()==MotionEvent.ACTION_MOVE){
float ex = (int) event.getX();
float ey = (int) event.getY();
if (ex>x&&ex<x+width&&ey>y&&ey<y+height){
x = (int) ex-width/2;
y = (int) ey-height/2;
}
}
}
}
4、如何绘制子弹
package com.example.lenovo.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
public static int height;
public static int width;
private SurfaceHolder surfaceHolder;
private Canvas canvas;//绘制图形的画布
private boolean isDrawing = true;//标定位
private MyPlane plane;
private Vector<Bullet>bulletVector = new Vector<>();//玩家子弹数组
private Vector<Bullet>bossBullterVector = new Vector<>();//Boss子弹数组
private int count;
public MySurfaceView(Context context) {
super(context);
init();
}
/**
* 初始化操作
*
* @param
*/
private void init() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);//添加回调时间监听
setFocusable(true);//设置可聚焦
setKeepScreenOn(true);//设置屏幕常亮
setFocusableInTouchMode(true);//设置接触模式
height = getHeight();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();//启动子线程
height = getHeight();
width = getWidth();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isDrawing = false;
}
@Override
public void run() {
Paint paint = new Paint();
BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.bk));
plane = new MyPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.myplane));
BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.bossplane));
while (isDrawing) {
count++;
try {
canvas = surfaceHolder.lockCanvas();//锁定画布
canvas.drawColor(Color.WHITE);
backGround.draw(canvas, paint);
plane.draw(canvas, paint);
bossPlane.draw(canvas, paint);
if (count % 20 == 0) {
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet), plane.getX, plane.getY,0);
Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet), plane.getX() + plane.getWidth(), plane.getY,0);
bulletVector.add(bullet);
bulletVector.add(bullet1);
}
//移除消失的子弹
for (int i = 0; i < bulletVector.size(); i++) {
if (bulletVector.elementAt(i).isDead()) {
bulletVector.remove(i);
}
}
//绘制玩家子弹
for (int i = 0; i < bulletVector.size(); i++) {
bulletVector.elementAt(i).draw(canvas, paint);
}
if (count % 20 == 0) {
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet), bossPlane.getX(), bossPlane.getY()+bossPlane.getFramH(),1);
Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet), bossPlane.getX() + bossPlane.getFramW(), bossPlane.getY+bossPlane.getFramH(),1);
bossBullterVector.add(bullet);
bossBullterVector.add(bullet1);
}
//移除消失boss的子弹
for (int i = 0; i < bossBullterVector.size(); i++) {
if (bossBullterVector.elementAt(i).isDead()) {
bossBullterVector.remove(i);
}
}
//绘制boss子弹
for (int i = 0; i < bossBullterVector.size(); i++) {
bossBullterVector.elementAt(i).draw(canvas, paint);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
plane.touchEvent(event);
return true;//永远监听屏幕触摸事件
}
private class MyPlane {
public int getX;
public int getY;
public MyPlane getWidth;
private int x;
private int width;
public MyPlane(Bitmap bitmap) {
}
public void draw(Canvas canvas, Paint paint) {
}
public void touchEvent(MotionEvent event) {
}
public int getX() {
return x;
}
public int getWidth() {
return width;
}
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* 2018/05/25
*/
public 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);
canvas.drawBitmap(bitmap,0,bitmap.getHeight(),paint);
}
public void logic(){
y1+=10;
y2-=10;
if (y1>=MySurfaceView.height){
y1=y2-bitmap.getHeight();//移动到第二张顶部
}
if (y2>=MySurfaceView.height){
y2=y1-bitmap.getHeight();
}
}
public void draw(Canvas canvas) {
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* 2018/5/29
*/
public class Bullet {
private Bitmap bitmap;
private int x,y;
private int speed = 20;
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;
case 1:
//Boss子弹
y += speed+5;
break;
default:
break;
}
y-=speed;
if (y<0){
isDead = true;
}
}
public boolean isDead() {
return isDead;
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import java.util.function.BooleanSupplier;
public class BossPlane {
private Bitmap bitmap;
private int x,y;
private int frameW,frameH;
private int speed=5;
private int crazySpeed=50;
private int count;//计数器
private int time=200;//疯狂模式间隔时间
private boolean isCrazy;
private int framH;
public int getY;
private int framW;
public BossPlane(Bitmap bitmap){
this.bitmap = bitmap;
this.frameW = bitmap.getWidth()/10;
this.frameH = bitmap.getHeight();
x=MySurfaceView.width/2-frameW/2;
}
public void draw(Canvas canvas, Paint paint){
canvas.save();
canvas.clipRect(x,y,x+frameW,y+frameH);
canvas.drawBitmap(bitmap,x,y,paint);
canvas.restore();
logic();
}
public void logic(){
count++;
if (isCrazy){
//疯狂模式
y = y+crazySpeed;
crazySpeed--;
if(y==0){
isCrazy = false;
crazySpeed = 50;
}
}else{
if(count%time==0){
isCrazy = true;
}
x = x+speed;
if(x>MySurfaceView.width-frameW){
speed = -speed;
}
if (x<0){
speed = -speed;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getFramH() {
return framH;
}
public int getFramW() {
return framW;
}
}
5、如何判断碰撞(子弹与飞机碰撞、飞机与飞机碰撞)
package com.example.lenovo.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MySurfaceView(this));
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import java.util.function.BooleanSupplier;
public class BossPlane {
private Bitmap bitmap;
private int x,y;
private int frameW,frameH;
private int speed=5;
private int crazySpeed=50;
private int count;//计数器
private int time=200;//疯狂模式间隔时间
private boolean isCrazy;
private int framH;
public int getY;
private int framW;
public BossPlane(Bitmap bitmap){
this.bitmap = bitmap;
this.frameW = bitmap.getWidth()/10;
this.frameH = bitmap.getHeight();
x=MySurfaceView.width/2-frameW/2;
}
public void draw(Canvas canvas, Paint paint){
canvas.save();
canvas.clipRect(x,y,x+frameW,y+frameH);
canvas.drawBitmap(bitmap,x,y,paint);
canvas.restore();
logic();
}
public void logic(){
count++;
if (isCrazy){
//疯狂模式
y = y+crazySpeed;
crazySpeed--;
if(y==0){
isCrazy = false;
crazySpeed = 50;
}
}else{
if(count%time==0){
isCrazy = true;
}
x = x+speed;
if(x>MySurfaceView.width-frameW){
speed = -speed;
}
if (x<0){
speed = -speed;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getFramH() {
return framH;
}
public int getFramW() {
return framW;
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* 2018/05/25
*/
public 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);
canvas.drawBitmap(bitmap,0,bitmap.getHeight(),paint);
}
public void logic(){
y1+=10;
y2+=10;
if (y1>=MySurfaceView.height){
y1=y2-bitmap.getHeight();//移动到第二张顶部
}
if (y2>=MySurfaceView.height){
y2=y1-bitmap.getHeight();
}
}
public void draw(Canvas canvas) {
}
}
package com.example.lenovo.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class MyPlane {
private final Bitmap bitmap;
private int y;
private int x;
private int width;
private int height;
private boolean noCollision;
private int noCollisionCount;
public MyPlane(Bitmap bitmap) {
this.bitmap = bitmap;
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);
}
}
public void touchEvent (MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_MOVE) {
float ex = (int) event.getX();
float ey = (int) event.getY();
if (ex > x && ex < x + width && ey > y && ey < y + height) {
x = (int) ex - width / 2;
y = (int) ey - height / 2;
}
}
}
public boolean isCollision(Bullet bullet){
if (bullet.getX()>x&&bullet.getX()<x+width&&bullet.getY()>y&&bullet.getY()<y+height){
noCollision = true;
return true;
}
return false;
}
public int getY() {
return y;
}
public int getX() {
return x;
}
public int getWidth() {
return width;
}
}
package com.example.lenovo.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
public static int height;
public static int width;
private SurfaceHolder surfaceHolder;
private Canvas canvas;//绘制图形的画布
private boolean isDrawing = true;//标定位
private MyPlane plane;
private Vector<Bullet> bulletVector = new Vector<>();//玩家子弹数组
private Vector<Bullet> bossBullterVector = new Vector<>();//Boss子弹数组
private int count;
public MySurfaceView(Context context) {
super(context);
init();
}
/**
* 初始化操作
*
* @param
*/
private void init() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);//添加回调时间监听
setFocusable(true);//设置可聚焦
setKeepScreenOn(true);//设置屏幕常亮
setFocusableInTouchMode(true);//设置接触模式
height = getHeight();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();//启动子线程
height = getHeight();
width = getWidth();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isDrawing = false;
}
@Override
public void run() {
Paint paint = new Paint();
BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.bk));
plane = new MyPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.myplane));
BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.bossplane));
while (isDrawing) {
count++;
try {
canvas = surfaceHolder.lockCanvas();//锁定画布
canvas.drawColor(Color.WHITE);
backGround.draw(canvas, paint);
plane.draw(canvas, paint);
bossPlane.draw(canvas, paint);
if (count % 20 == 0) {
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet), plane.getX(), plane.getY(), 0);
Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet), plane.getX() + plane.getWidth(), plane.getY(), 0);
bulletVector.add(bullet);
bulletVector.add(bullet1);
}
//移除消失的子弹
for (int i = 0; i < bulletVector.size(); i++) {
if (bulletVector.elementAt(i).isDead()) {
bulletVector.remove(i);
}
}
//绘制玩家子弹
for (int i = 0; i < bulletVector.size(); i++) {
bulletVector.elementAt(i).draw(canvas, paint);
}
if (count % 20 == 0) {
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet), bossPlane.getX(), bossPlane.getY() + bossPlane.getFramH(), 1);
Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet), bossPlane.getX() + bossPlane.getFramW(), bossPlane.getY + bossPlane.getFramH(), 1);
bossBullterVector.add(bullet);
bossBullterVector.add(bullet1);
}
//移除消失boss的子弹
for (int i = 0; i < bossBullterVector.size(); i++) {
if (bossBullterVector.elementAt(i).isDead()) {
bossBullterVector.remove(i);
}
}
//绘制boss子弹
for (int i = 0; i < bossBullterVector.size(); i++) {
bossBullterVector.elementAt(i).draw(canvas, paint);
plane.isCollision(bossBullterVector.elementAt(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
plane.touchEvent(event);
return true;//永远监听屏幕触摸事件
}
}
package com.example.lenovo.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
/**
* 2018/5/29
*/
public class Bullet {
private Bitmap bitmap;
private int x,y;
private int speed = 20;
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;
case 1:
//Boss子弹
y += speed+20;
break;
default:
break;
}
// y-=speed;
// if (y<0){
// isDead = true;
// }
}
public boolean isDead() {
return isDead;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeed() {
return speed;
}
}
8、哪些地方用到了封装、继承、多态、方法重载、接口等
封装:
public class Bullet {
private Bitmap bitmap;
private int x, y;
private int speed = 10;
private boolean isDead;
private int type;
继承:
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
public static int GAME_STATE = 0;
public class Boom extends Bullet {
private Bitmap bitmap;
private int x,y;
private int totalFrame;
多态:
public class Boom extends Bullet {
private Bitmap bitmap;
private int x,y;
private int totalFrame;
private int currentFrame;
private int frameW,frameH;
private boolean isEnd;
public Boom(Bitmap bitmap, int x, int y, int totalFrame) {
super();
this.bitmap = bitmap;
this.x = x;
this.y = y;
this.totalFrame = totalFrame;
frameW = bitmap.getWidth()/totalFrame;
frameH = bitmap.getHeight();
方法重写:
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();//启动子线程
height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
width = getWidth();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
9、我的收获与感悟
储备实力很重要,手里一定要有几颗炸弹。工作中学习提升就是在储备能力,要有几样别人办不到的独门功夫,这就是所谓的核心竞争力。
人生的每一步旅途中,总有着一道无形的栏杆,每一次跨越需要很多的勇气,也不是每一次跨越都能够成功,失败是不可避免的,主要的是要敢于承认失败,面对失败,努力去做,解决它,有这个决心,我想人生会成功的,至少可以无憾!