package com.example.fly.myapplication;
import Android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
/**
* Created by fly on 2017/5/25.
*/
public class MyPlane {
private Bitmap bmpPlane;//战斗机
private Bitmap bmpHp;//战斗机的生命值
private int hp = 3;//赋初值
private int PlaneX, PlaneY;//战斗机的横纵坐标
private boolean isOnPlane;//手在战斗机的方法
public MyPlane(Bitmap bmpPlane, Bitmap bmpHp) {
this.bmpPlane = bmpPlane;
this.bmpHp = bmpHp;
PlaneX = GameSurfaceview.screenWidth / 2 - bmpPlane.getWidth() / 2;
PlaneY = GameSurfaceview.screenHeight - bmpPlane.getHeight();
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpPlane, PlaneX, PlaneY, paint);
for (int i = 0; i < hp; i++) {
canvas.drawBitmap(bmpHp, bmpHp.getWidth() * i, GameSurfaceview.screenHeight - bmpHp.getHeight(), paint);
}
}
public void touch(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (touchX > PlaneX && touchX < PlaneX + bmpPlane.getWidth()) {
if (touchY > PlaneY && touchY < PlaneY + bmpPlane.getHeight()) {
isOnPlane = true;
}
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (isOnPlane) {
if (touchX < GameSurfaceview.screenWidth - bmpPlane.getWidth() / 2 && touchX > bmpPlane.getWidth() / 2 && touchY < GameSurfaceview.screenHeight - bmpPlane.getHeight() / 2 && touchY > bmpPlane.getHeight() / 2) {
PlaneX = (int) touchX - bmpPlane.getWidth() / 2;
PlaneY = (int) touchY - bmpPlane.getHeight() / 2;
}
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
isOnPlane = false;
}
}
}
22:07:48
�� 2017/5/25 22:07:48
package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* Created by fly on 2017/5/25.
*/
public class Boss {
private Bitmap bmpBoss;
private int BossHp;//Boss的生命值
private int Bossspeed = 5;//Boss左右移动的速度
private float BossWidth, BossHeight;//Boss的长度和宽度
private int bossX, bossY;//Boss的横纵坐标
private int countCrazy;//计数器
private boolean isCrazy = false;
private int BossCrazy = 50;//Boss疯狂的速度
public Boss(Bitmap bmpBoss) {
this.bmpBoss = bmpBoss;
BossWidth = bmpBoss.getWidth() / 10;
BossHeight = bmpBoss.getHeight();
bossX = (int) (GameSurfaceview.screenWidth / 2 - BossWidth / 2);
bossY = (int) (BossWidth / 10);
}
public void draw(Canvas canvas, Paint paint) {
canvas.clipRect(bossX, bossY, bossX + BossWidth, bossY + BossHeight);
canvas.drawBitmap(bmpBoss, bossX, bossY, paint);
}
public void logic() {
bossX += Bossspeed;
//如果Boss超出屏幕就往反方向移动
if (bossX + BossWidth >= GameSurfaceview.screenWidth) {
Bossspeed = -Bossspeed;
} else if (bossX <= 0) {
Bossspeed = -Bossspeed;
}
countCrazy++;
if (countCrazy % 200 == 0) {
isCrazy = true;
}
if (isCrazy) {
bossY += BossCrazy;
}
//如果Boss的y坐标超出屏幕就停止疯狂模式,并且以BossCrazy的速度返回
if (bossY > GameSurfaceview.screenHeight - BossHeight) {
isCrazy = false;
}
if (bossY >= 0 && isCrazy == false) {
bossY -= BossCrazy;
}
}
}
�� 2017/5/25 22:07:50
package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
/**
* Created by fly on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
private Rect rect;
private int x, y;
public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
this.bmpMenuBG = bmpMenuBG;
this.bmpMenuLogo = bmpMenuLogo;
this.bmpMenuButton = bmpMenuButton;
this.bmpbtBG = bmpbtBG;
rect = new Rect
(0, GameSurfaceview.screenHeight / 3, GameSurfaceview.screenWidth, GameSurfaceview.screenHeight / 3 + GameSurfaceview.screenHeight / 5);
}//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpMenuBG, 0, 0, paint);
canvas.drawBitmap(bmpMenuLogo, null, rect, paint);
x = GameSurfaceview.screenWidth / 2 - bmpMenuButton.getWidth() / 2;
y = GameSurfaceview.screenHeight / 3 * 2;
canvas.drawBitmap(bmpMenuButton, x, y, paint);
int m = GameSurfaceview.screenWidth / 2 - bmpbtBG.getWidth() / 2;
int n = GameSurfaceview.screenHeight / 3 * 2;
canvas.drawBitmap(bmpbtBG, m, n, paint);
}
/**
* GameMenu点击事件
* @param event
*/
public void touch(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
//第一层判断,手是否抬起动作
if (event.getAction() == MotionEvent.ACTION_UP) {
//第二层判断
if (touchX > x && touchX < x + bmpMenuButton.getWidth()) {
if (touchY > y && touchY < y + bmpMenuButton.getHeight()) {
Log.e("GameMenu", "按钮被按下");
}
}
}
}
}
�� 2017/5/25 22:07:50
package com.example.fly.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* Created by fly on 2017/5/25.
*/
public class GameBackground {
private Bitmap bmpBG1;//背景图1
private Bitmap bmpBG2;//背景图2
private int speed=5;//图片滑动速度
private int y1,y2;
public GameBackground(Bitmap bmgBG1,Bitmap bmgBG2){
this.bmpBG1=bmgBG1;
this.bmpBG2=bmgBG2;
y1 = Math.abs(GameSurfaceview.screenHeight-bmpBG1.getHeight());
y2 = y1-bmpBG1.getHeight();
}
public void draw(Canvas canvas, Paint paint){
canvas.drawBitmap(bmpBG1,0,y1,paint);//让背景图贴合屏幕下方
canvas.drawBitmap(bmpBG2,0,y2,paint);//让第二张图接在第一张图上方
}
public void logic(){
y1+=speed;
y2+=speed;
if(y1>GameSurfaceview.screenHeight){
//第一张图片超出屏幕时,移动到第二张图片的位置
y1=y2-bmpBG1.getHeight();
}
if(y2>GameSurfaceview.screenHeight){
//第二张图片超出屏幕时,移动到第一张图片的位置
y2=y1-bmpBG2.getHeight();
}
}
}
22:08:05
�� 2017/5/25 22:08:05
package com.example.fly.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by fly on 2017/5/24.
*/
public class GameSurfaceview extends SurfaceView implements SurfaceHolder.Callback {
private Canvas canvas;
private Paint paint;
private SurfaceHolder surfaceHolder;
public static int screenWidth;//手机屏幕的宽
public static int screenHeight;//手机屏幕的高
//Menu相关
private GameMenu gameMenu;
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmpMenuLogo;//菜单页面Logo
private Bitmap bmpMenuButton;//菜单页面按钮
private Bitmap bmpbtBG;//菜单页面按钮背景
//游戏背景相关
private GameBackground gameBackground;
private Bitmap bmpBG1;//背景图1
private Bitmap bmpBG2;//背景图2
//我的战斗机
private MyPlane gameMyplane;
private Bitmap bmpPlane;//战斗机
private Bitmap bmpHp;//生命值
//Boss相关
private Boss boss;
private Bitmap bmpBoss;//Boss飞机
private int BossHp;//Boss生命值
private int Bossspeed;//Boss速度
public GameSurfaceview(Context context) {
super(context);
surfaceHolder = this.getHolder();//初始化holder对象
surfaceHolder.addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenWidth = this.getWidth();
screenHeight = this.getHeight();
initBitmap();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
myDraw();
gameBackground.logic();
boss.logic();
}
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
//绘图方法
private void myDraw() {
canvas = surfaceHolder.lockCanvas();
//调用GameMenu对象画游戏菜单页面
//gameMenu.draw(canvas, paint);
//调用GameBackground对象画游戏背景
if (canvas != null) {
gameBackground.draw(canvas, paint);
gameMyplane.draw(canvas, paint);
boss.draw(canvas, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
/**
* 初始化图片方法
*/
private void initBitmap() {
//把图片转化成Bitmap
bmpMenuBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);
bmpMenuLogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
bmpMenuButton = BitmapFactory.decodeResource(this.getResources(), R.drawable.starttext);
bmpbtBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.menustart);
bmpBG1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.bk);
bmpBG2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.bk);
bmpPlane = BitmapFactory.decodeResource(this.getResources(), R.drawable.myplane);
bmpHp = BitmapFactory.decodeResource(this.getResources(), R.drawable.myhp);
bmpBoss = BitmapFactory.decodeResource(this.getResources(), R.drawable.bossplane);
//初始化对象
gameMenu = new GameMenu(bmpMenuBG, bmpMenuLogo, bmpbtBG, bmpMenuButton);
gameBackground = new GameBackground(bmpBG1, bmpBG2);
gameMyplane = new MyPlane(bmpPlane, bmpHp);
boss = new Boss(bmpBoss);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//gameMenu.touch(event);
gameMyplane.touch(event);
return true;
}
}