将飞机的素材,放在res/drawable-mdpi文件夹下,
创建物品对象的类:
package com.example.object;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
abstract public class GameObject {
protected int currentFrame;
protected int speed;
protected float object_x;
protected float object_y;
protected float object_height;
protected float object_width;
protected float screen_width;
protected float screen_height;
protected boolean isAlive;
protected Paint paint;
protected Resources resources;
public GameObject(Resources resources) {
this.resources = resources;
this.paint = new Paint();
}
public void setScreenWH(float screen_width, float screen_height){
this.screen_width = screen_width;
this.screen_height = screen_height;
}
public void initial(int arg0, float arg1, float arg2){
}
protected abstract void initBitmap();
public abstract void drawSelf(Canvas canvas);
public abstract void release();
public boolean isCollide(GameObject obj) {
return true;
}
public void logic(){
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public float getObject_x() {
return object_x;
}
public void setObject_x(float object_x) {
this.object_x = object_x;
}
public float getObject_y() {
return object_y;
}
public void setObject_y(float object_y) {
this.object_y = object_y;
}
public float getObject_height() {
return object_height;
}
public void setObject_height(float object_height) {
this.object_height = object_height;
}
public float getObject_width() {
return object_width;
}
public void setObject_width(float object_width) {
this.object_width = object_width;
}
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
}
创建飞机类。继承物品类:
package com.example.object;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import com.example.planewars.R;
import com.example.view.MainView;
import com.example.interfaces.IMyPlane;
public class MyPlane extends GameObject {
private float middle_x;
private float middle_y;
private Bitmap myplane;
private Bitmap myplaneexplosion;
private MainView mainView;
public MyPlane(Resources resources) {
super(resources);
// TODO Auto-generated constructor stub
initBitmap();
}
public void setMainView(MainView mainView) {
this.mainView = mainView;
}
public void setScreenWH(float screen_width, float screen_height) {
super.setScreenWH(screen_width, screen_height);
object_x = screen_width/2 - object_width/2;
object_y = screen_height - object_height;
middle_x = object_x + object_width/2;
middle_y = object_y + object_height/2;
}
@Override
protected void initBitmap() {
// TODO Auto-generated method stub
myplane = BitmapFactory.decodeResource(resources, R.drawable.myplane);
myplaneexplosion = BitmapFactory.decodeResource(resources, R.drawable.myplaneexplosion);
object_width = myplane.getWidth()/2;
object_height = myplane.getHeight();
}
@Override
public void drawSelf(Canvas canvas) {
canvas.save();
canvas.clipRect(object_x, object_y,object_x + object_width, object_y + object_height);
canvas.drawBitmap(myplane, object_x , object_y, paint);
canvas.restore();
}
@Override
public void release() {
// TODO Auto-generated method stub
if(!myplane.isRecycled()){
myplane.recycle();
}
if(!myplaneexplosion.isRecycled()){
myplaneexplosion.recycle();
}
}
public float getMiddle_x() {
return middle_x;
}
public void setMiddle_x(float middle_x) {
this.middle_x = middle_x;
}
public float getMiddle_y() {
return middle_y;
}
public void setMiddle_y(float middle_y) {
this.middle_y = middle_y;
}
}
创建工厂类,用于创建游戏中的各种物品:
package com.example.factory;
import android.content.res.Resources;
import com.example.object.GameObject;
import com.example.object.MyPlane;
public class GameObjectFactory {
public GameObject createMyPlane(Resources resources){
return new MyPlane(resources);
}
}
创建显示的类。
package com.example.view;
import com.example.planewars.MainActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BaseView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
protected int currentFrame;
protected float scalex;
protected float scaley;
protected float screen_width;
protected float screen_height;
protected boolean threadFlag;
protected Paint paint;
protected Canvas canvas;
protected Thread thread;
protected SurfaceHolder sfh;
//protected GameSoundPool sounds;
protected MainActivity mainActivity;
public BaseView(Context context) {
super(context);
this.mainActivity = (MainActivity) context;
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1,int arg2, int arg3){
}
public void surfaceCreated(SurfaceHolder arg0) {
screen_width = this.getWidth();
screen_height = this.getHeight();
threadFlag = true;
}
public void surfaceDestroyed(SurfaceHolder arg0){
threadFlag = false;
}
public void initBitmap(){
}
public void release(){
}
public void drawSelf(){
}
public void run(){
}
public void setThreadFlag(boolean threadFlag){
this.threadFlag = threadFlag;
}
}
创建主界面,继承显示类:
package com.example.view;
import android.content.Context;
import android.graphics.Color;
import android.os.Message;
import android.view.SurfaceHolder;
import com.example.object.MyPlane;
import com.example.factory.GameObjectFactory;
public class MainView extends BaseView{
private MyPlane myPlane;
private GameObjectFactory factory;
private boolean isPlay;
public MainView(Context context) {
super(context);
// TODO Auto-generated constructor stub
factory = new GameObjectFactory();
myPlane = (MyPlane) factory.createMyPlane(getResources());
myPlane.setMainView(this);
thread = new Thread(this);
}
public void surfaceCreated(SurfaceHolder arg0){
super.surfaceCreated(arg0);
initBitmap();
myPlane.setScreenWH(screen_width, screen_height);
myPlane.setAlive(true);
if(thread.isAlive()){
thread.start();
} else {
thread = new Thread(this);
thread.start();
}
}
public void surfaceDestoryed(SurfaceHolder arg0){
super.surfaceDestroyed(arg0);
release();
}
public void initBitmap(){
}
public void initObject(){
}
public void drawSelf(){
try{
canvas = sfh.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.save();
myPlane.drawSelf(canvas);
} finally{
if(canvas != null);
sfh.unlockCanvasAndPost(canvas);
}
}
public void viewLogic(){
}
public void run(){
while(threadFlag){
long startTime = System.currentTimeMillis();
initObject();
drawSelf();
viewLogic();
long endTime = System.currentTimeMillis();
if(!isPlay){
synchronized(thread){
try{
thread.wait();
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
try{
if(endTime - startTime <100){
Thread.sleep(100-(endTime - startTime));
}
} catch(InterruptedException e ){
e.printStackTrace();
}
}
try{
Thread.sleep(1000);
} catch(InterruptedException e ) {
e.printStackTrace();
}
}
}
运行结果如下图:
这里用surfaceView 。bitmap、canvas。创建飞机使用的是工厂模式。