飞机大战源代码(一)
注:红线报错并非代码错误,各类代码需配合一起使用。
1.主程序:
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.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.Vector;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
public static int GAME_STATE = 0;
private SurfaceHolder surfaceHolder;
private Canvas canvas;//绘制图形的画布
private boolean isDrawing = true;//标志位
public static int height;
public static int width;
private Myplane plane;
private Vector<Bullet>bulletVector = new Vector <>();
private Vector<Bullet>bossBulletVector = new Vector <>();
private Vector<Bullet>boomVector = new Vector <>();
private int count;
private GameSoundPool gameSoundPool;
public MySurfaceView(Context context) {
super(context);
gameSoundPool = new GameSoundPool(context);
init();
}
/*
初始化操作
*/
private void init() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);//添加回调事件监听
setFocusable(true);
setKeepScreenOn(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();//启动子线程
height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
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.img_bg_level_3));
plane = new Myplane(BitmapFactory.decodeResource(getResources(), R.mipmap.pl2),BitmapFactory.decodeResource(getResources(), R.mipmap.myhp));
BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), R.mipmap.bossplane));
while (isDrawing) {
count++;
try {
canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
canvas.drawColor(Color.WHITE);
switch (GAME_STATE){
case 0:
backGround.draw(canvas, paint);
plane.draw(canvas, paint);
bossPlane.draw(canvas,paint);
if(count%10==0){
gameSoundPool.playSound(1);
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet2),plane.getX(),plane.getY(),0);
Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.mybullet2),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 (bossPlane.isCollision(bulletVector.elementAt(i))){
gameSoundPool.playSound(2);
Boom boom = new Boom(BitmapFactory.decodeResource(getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/4,bossPlane.getY(),7);
boomVector.add(boom);
}
}
for (int i=0;i<boomVector.size();i++){
if (boomVector.elementAt(i).isDead()){
boomVector.remove(i);
}else{
boomVector.elementAt(i).draw(canvas,paint);
}
}
if(count%30==0){
Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY()+bossPlane.getFrameH(),1);
//Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW(),bossPlane.getY()+bossPlane.getFrameH(),1);
bossBulletVector.add(bullet);
// bossBulletVector.add(bullet1);
}
for (int i = 0;i < bossBulletVector.size(); i++) {
if (bossBulletVector.elementAt(i).isDead()) {
bossBulletVector.remove(i);
}
}
for (int i = 0;i < bossBulletVector.size(); i++){
bossBulletVector.elementAt(i).draw(canvas,paint);
plane.isCollision(bossBulletVector.elementAt(i));
}
plane.isCollision(bossPlane);
break;
case 1:
RectF rectF = new RectF(0,0,getWidth(),getHeight());
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.gamewin),null,rectF,paint);
break;
case 2:
RectF rectF1 = new RectF(0,0,getWidth(),getHeight());
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.over),null,rectF1,paint);
break;
case 3:
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
plane.touchEvent(event);
return true;//永远监听屏幕触摸事件
}
}
2.辅程序代码:
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();
}
}
}
3.玩家飞机类:
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(hp<=0){
MySurfaceView.GAME_STATE = 2;
}
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; i<hp; i++){
canvas.drawBitmap(bitmapHp,i*bitmapHp.getWidth(),MySurfaceView.height-bitmapHp.getHeight(),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;
if(y<0){
y=0;
}
if(y+height>MySurfaceView.height){
y=MySurfaceView.height-height;
}
}
}
}
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;
}
public boolean isCollision(BossPlane bossPlane) {
if (noCollision){
return false;
}else{
if(bossPlane.getY()+bossPlane.getFrameH()>y&&bossPlane.getY()+bossPlane.getFrameH()<y+height){
if(x<bossPlane.getX()&&x+width>bossPlane.getX()){
noCollision = true;
if (hp>0){
hp--;
}
return true;
}
if (x>bossPlane.getX()&&x+width<bossPlane.getX()+bossPlane.getFrameW()){
noCollision = true;
if (hp>0){
hp--;
}
return true;
}
if (x>bossPlane.getX()&&+x+width>bossPlane.getX()+bossPlane.getFrameW()){
noCollision = true;
if (hp>0){
hp--;
}
return true;
}
}
}
return false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
}
4.BOSS飞机类:
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
public class BossPlane {
private Bitmap bitmap;
private int x,y;
private int frameW,frameH;
private int speed=5;//boos飞机的速度
private int crazySpeed=50;//疯狂速度
private int count;//计数器
private int time=500;//疯狂模式间隔
private boolean isCrazy;
private int bossHp = 100;
public BossPlane(Bitmap bitmap) {
this.bitmap = bitmap;
this.frameW = bitmap.getWidth()/10;
this.frameH = bitmap.getHeight();
x=MySurfaceView.width/2-frameH/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-frameH){
speed = -speed;
}
if (x<0){
speed = -speed;
}
}
}
public boolean isCollision(Bullet bullet){
if(bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()<x+frameW&&bullet.getY()>y&&bullet.getY()<y+frameH){
bossHp--;
bullet.setDead(true);
if (bossHp<0){
MySurfaceView.GAME_STATE = 1;
}
return true;
}
return false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getFrameW() {
return frameW;
}
public int getFrameH() {
return frameH;
}
}
5.子弹类:
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 Bullet() {
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bitmap, x, y, paint);
logic();
}
public void logic() {
switch (type){
//玩家子弹
case 0:
y -= speed+5;
if (y < 0) {
isDead = true;
}
break;
//Boss子弹
case 1:
y += speed+8;
if (y < 0) {
isDead = true;
}
break;
default:
break;
}
}
public boolean isDead() {
return isDead;
}
public Bitmap getBitmap(){
return bitmap;
}
public int getX(){
return x;
}
public int getY() {
return y;
}
public void setDead(boolean dead) {
isDead = dead;
}
}