1. 程式人生 > >小型安卓打飛機遊戲

小型安卓打飛機遊戲

原始碼:

由於原始碼較大,所以原始碼打包,連結為:

核心程式碼:

ToolsView.java

package com.example.funplane.view;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.Timer;

import java.util.TimerTask;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.content.DialogInterface;

import android.content.res.Resources;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.view.MotionEvent;

import android.view.View;

import com.example.funplane.R;

importcom.example.funplane.activity.MainActivity;

importcom.example.funplane.entity.Background;

importcom.example.funplane.entity.BigEnBullet;

importcom.example.funplane.entity.BigEnPlane;

import com.example.funplane.entity.Boss;

import com.example.funplane.entity.BossBullet;

import com.example.funplane.entity.Bullet;

import com.example.funplane.entity.Clear;

importcom.example.funplane.entity.EnBullet;

import com.example.funplane.entity.EnPlane;

importcom.example.funplane.entity.LeftBossBullet;

import com.example.funplane.entity.Life;

import com.example.funplane.entity.Plane;

importcom.example.funplane.entity.RightBossBullet;

public class ToolsView extends View {

         privateResources res;

         privatePlane plane;

         privateint t = 0;

         privateList<Bullet> list;

         privateTimer timer;

         privateint time1 = 1;

         privateRandom random;

         privateList<Life> lifes;

         privateList<Clear> clears;

         ints = 0;

         intx;

         inty;

         inttrack = 0;

         intn = 0;

         privateBackground background;

         privateList<EnBullet> enbullets;

         privateList<BigEnBullet> bigenbullets;

         publicstatic int console;

         publicContext context;

         privateList<BossBullet> bossbullets;

         privateList<LeftBossBullet> leftbossbullets;

         privateList<RightBossBullet> rightbossbullets;

         privateint add = 0;

         publicRandom getRandom() {

                   returnrandom;

         }

         publicList<EnBullet> getEnbullets() {

                   returnenbullets;

         }

         publicvoid setEnbullets(List<EnBullet> enbullets) {

                   this.enbullets= enbullets;

         }

         publicvoid setRandom(Random random) {

                   this.random= random;

         }

         privateTimer removetimer;

         privateList<EnPlane> enplanes;

         privateList<Boss> bosses;

         privateList<BigEnPlane> bigenplanes;

         publicstatic  int  id ;

         publicTimer getRemovetimer() {

                   returnremovetimer;

         }

         publicvoid setRemovetimer(Timer removetimer) {

                   this.removetimer= removetimer;

         }

         publicResources getRes() {

                   returnres;

         }

         publicvoid setRes(Resources res) {

                   this.res= res;

         }

         publicPlane getPlane() {

                   returnplane;

         }

         publicvoid setPlane(Plane plane) {

                   this.plane= plane;

         }

         Bossboss;

         publicToolsView(Context context) {

                   super(context);

                   this.context= context;

                   //TODO Auto-generated constructor stub

                   res= getResources();

                   random= new Random();

                   lifes= new ArrayList<Life>();

                   bigenplanes= new ArrayList<BigEnPlane>();

                   bigenbullets= new ArrayList<BigEnBullet>();

                   enbullets= new ArrayList<EnBullet>();

                   clears= new ArrayList<Clear>();

                   background= new Background(res, R.drawable.background);

                   enplanes= new ArrayList<EnPlane>();

                   plane= new Plane(res,id);

                   list= new ArrayList<Bullet>();

                   removetimer= new Timer();

                   timer= new Timer();

                   bosses= new ArrayList<Boss>();

                   bossbullets= new ArrayList<BossBullet>();

                   leftbossbullets= new ArrayList<LeftBossBullet>();

                   rightbossbullets= new ArrayList<RightBossBullet>();

                   timer.schedule(newTimerTask() {

                            @Override

                            publicvoid run() {

                                     //TODO Auto-generated method stub

                                     Bulletbullet = new Bullet(res);

                                     bullet.setX(plane.getX());

                                     bullet.setY(plane.getY());

                                     list.add(bullet);

                            }

                   },0, 800);

                   removetimer.schedule(newTimerTask() {

                            @Override

                            publicvoid run() {

                                     //TODO Auto-generated method stub

                                     for(int i = 0; i < bosses.size(); i++) {

                                               Bossboss = bosses.get(i);

                                               {

                                                        if(boss.getY() >= MainActivity.y) {

                                                                 bosses.remove(boss);

                                                                 track= 0;

                                                        }

                                               }

                                     }

                                     for(int i = 0; i < list.size(); i++) {

                                               Bulletbullet = list.get(i);

                                               if(bullet.getY() <= 0 - bullet.getBitmap().getHeight()) {

                                                        list.remove(bullet);

                                               }

                                     }

                                     for(int i = 0; i < enplanes.size(); i++) {

                                               EnPlaneenplane = enplanes.get(i);

                                               if(enplane.getY() >= MainActivity.y) {

                                                        enplanes.remove(enplane);

                                               }

                                     }

                                     for(int i = 0; i < bigenplanes.size(); i++) {

                                               BigEnPlanebigenplane = bigenplanes.get(i);

                                               if(bigenplane.getY() >= MainActivity.y) {

                                                        bigenplanes.remove(bigenplane);

                                               }

                                     }

                                     for(int i = 0; i < enbullets.size(); i++) {

                                               EnBulletenbullet = enbullets.get(i);

                                               if(enbullet.getY() >= MainActivity.y) {

                                                        enbullets.remove(enbullet);

                                               }

                                     }

                                     for(int i = 0; i < bossbullets.size(); i++) {

                                               BossBulletbossbullet = bossbullets.get(i);

                                               if(bossbullet.getY() >= MainActivity.y) {

                                                        bossbullets.remove(bossbullet);

                                               }

                                     }

                                     for(int i = 0; i < leftbossbullets.size(); i++) {

                                               LeftBossBulletleftbossbullet = leftbossbullets.get(i);

                                               if(leftbossbullet.getY() >= MainActivity.y) {

                                                        leftbossbullets.remove(leftbossbullet);

                                               }

                                     }

                                     for(int i = 0; i < rightbossbullets.size(); i++) {

                                               RightBossBulletrightbossbullet = rightbossbullets.get(i);

                                               if(rightbossbullet.getY() >= MainActivity.y) {

                                                        rightbossbullets.remove(rightbossbullet);

                                               }

                                     }

                                     for(int i = 0; i < bigenbullets.size(); i++) {

                                               BigEnBulletbigenbullet = bigenbullets.get(i);

                                               if(bigenbullet.getY() >= MainActivity.y) {

                                                        bigenbullets.remove(bigenbullet);

                                               }

                                     }

                                     for(int i = 0; i < lifes.size(); i++) {

                                               Lifelife = lifes.get(i);

                                               if(life.getY() >= MainActivity.y) {

                                                        lifes.remove(life);

                                               }

                                     }

                                     for(int i = 0; i < clears.size(); i++) {

                                               Clearclear = clears.get(i);

                                               if(clear.getY() >= MainActivity.y) {

                                                        clears.remove(clear);

                                               }

                                     }

                            }

                   },2000, 30);

         }

         publicTimer getTimer() {

                   returntimer;

         }

         publicstatic int getConsole() {

                   returnconsole;

         }

         publicstatic void setConsole(int console) {

                   ToolsView.console= console;

         }

         publicvoid setTimer(Timer timer) {

                   this.timer= timer;

         }

         publicList<Bullet> getList() {

                   returnlist;

         }

         publicvoid setList(List<Bullet> list) {

                   this.list= list;

         }

         inttimes = 0;

         @Override

         protectedvoid onDraw(Canvas canvas) {

                   //TODO Auto-generated method stub

                   super.onDraw(canvas);

                   if(console != -1)

                   {

                            background.draw(canvas);

                            Stringscore = "您的成績為: " + s;

                            Stringlife_plane = "剩餘 : " + add;

                            Paintp = new Paint();

                            p.setTextSize(20);

                            canvas.drawText(score,300, 20, p);

                            canvas.drawText(life_plane,0, 20, p);

                            time1++;

                            if(time1 % 50 == 0) {

                                     EnPlaneenplane = new EnPlane(res);

                                     enplane.setX(random.nextInt(MainActivity.x

                                                        -enplane.getBitmap().getWidth()));

                                     enplanes.add(enplane);

                            }

                            if(time1 % 233 == 0) {

                                     BigEnPlanebigenplane = new BigEnPlane(res);

                                     bigenplane.setX(random.nextInt(MainActivity.x

                                                        -bigenplane.getBitmap().getWidth()));

                                     bigenplanes.add(bigenplane);

                            }

                            if(time1 % 8000== 0) {

                                     if(bosses.size() == 0) {

                                               Bossboss = new Boss(res);

                                               intposition = random.nextInt(MainActivity.x / 2

                                                                 -boss.getBitmap().getWidth() / 2);

                                               if(position <= 3) {

                                                        position= 3;

                                               }

                                               boss.setX(position);

                                               bosses.add(boss);

                                     }

                            }

                            if (n <= 2) {

                                     if(time1 % 5000 == 0) {

                                               if(lifes.size() == 0) {

                                                        Lifelife = new Life(res);

                                                        life.setX(random.nextInt(MainActivity.x

                                                                           -life.getBitmap().getWidth()));

                                                        lifes.add(life);

                                                        n++;

                                               }

                                     }

                            }

                            for(int i = 0; i < lifes.size(); i++) {

                                     Lifelife = lifes.get(i);

                                     life.draw(canvas);

                                     if(life.getRect().intersect(plane.getRect())) {

                                               add++;

                                               lifes.remove(life);

                                     }

                            }

                            if(t <= 4) {

                                      if (time1 % 3500 == 0)

                                      {

                                               if(clears.size() == 0) {

                                                        Clearclear = new Clear(res);

                                                        clear.setX(random.nextInt(MainActivity.x

                                                                           -clear.getBitmap().getWidth()));

                                                        clears.add(clear);

                                                        t++;

                                               }

                                     }

                            }

                            for(int i = 0; i < bosses.size(); i++) {

                                     Boss boss = bosses.get(i);

                                     boss.draw(canvas);

                                     if(boss.getTime() % 100 == 0) {

                                               BossBulletbossbullet = new BossBullet(res);

                                               bossbullet.setX(boss.getX());

                                               bossbullet.setY(boss.getY());

                                               bossbullets.add(bossbullet);

                                               LeftBossBulletleftbossbullet = new LeftBossBullet(res);

                                               leftbossbullet.setX(boss.getX());

                                               leftbossbullet.setY(boss.getY());

                                               leftbossbullets.add(leftbossbullet);

                                               RightBossBulletrightbossbullet = new RightBossBullet(res);

                                               rightbossbullet.setX(boss.getX());

                                               rightbossbullet.setY(boss.getY());

                                               rightbossbullets.add(rightbossbullet);

                                     }

                                     if(boss.getRect().intersect(plane.getRect())) {

                                               add--;

                                               bosses.remove(boss);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < bossbullets.size(); i++) {

                                     BossBulletbossbullet = bossbullets.get(i);

                                     bossbullet.draw(canvas);

                                     if(bossbullet.getRect().intersect(plane.getRect())) {

                                               add--;

                                               bossbullets.remove(bossbullet);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < leftbossbullets.size(); i++) {

                                     LeftBossBulletleftbossbullet = leftbossbullets.get(i);

                                     leftbossbullet.draw(canvas);

                                     if(leftbossbullet.getRect().intersect(plane.getRect())) {

                                               add--;

                                               leftbossbullets.remove(leftbossbullet);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < rightbossbullets.size(); i++) {

                                     RightBossBulletrightbossbullet = rightbossbullets.get(i);

                                     rightbossbullet.draw(canvas);

                                     if(rightbossbullet.getRect().intersect(plane.getRect())) {

                                               add--;

                                               rightbossbullets.remove(rightbossbullet);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < enplanes.size(); i++) {

                                     EnPlaneenplane = enplanes.get(i);

                                     enplane.draw(canvas);

                                     if(enplane.getTime() % 200 == 0) {

                                               EnBulletenbullet = new EnBullet(res);

                                               enbullet.setX(enplane.getX());

                                               enbullet.setY(enplane.getY());

                                               enbullets.add(enbullet);

                                     }

                                     if(enplane.getRect().intersect(plane.getRect())) {

                                               add--;

                                               enplanes.remove(enplane);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < bigenplanes.size(); i++) {

                                     BigEnPlanebigenplane = bigenplanes.get(i);

                                     bigenplane.draw(canvas);

                                     if(bigenplane.getTime() % 40 == 0) {

                                               BigEnBulletbigenbullet = new BigEnBullet(res);

                                               bigenbullet.setX(bigenplane.getX());

                                               bigenbullet.setY(bigenplane.getY());

                                               bigenbullets.add(bigenbullet);

                                     }

                                     if(bigenplane.getRect().intersect(plane.getRect())) {

                                               add--;

                                               bigenplanes.remove(bigenplane);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                                     for(int j = 0; j < list.size(); j++) {

                                               Bulletbullet = list.get(j);

                                               if(bigenplane.getRect().intersect(bullet.getRect())) {

                                                        list.remove(bullet);

                                                        bigenplanes.remove(bigenplane);

                                                        s+= 200;

                                               }

                                     }

                            }

                            for(int i = 0; i < list.size(); i++) {

                                     Bulletbullet = list.get(i);

                                     bullet.draw(canvas);

                                     for(int k = 0; k < bosses.size(); k++) {

                                               boss= bosses.get(k);

                                               if(bullet.getRect().intersect(boss.getRect())) {

                                                        track++;

                                                        list.remove(bullet);

                                                        break;

                                               }

                                                if (track < 10 && boss.getY() >MainActivity.y)

                                                         {

                                                        track= 0;

                                               }

                                     }

                                      if (track == 10)

                                      {

                                               s+= 1000;

                                              bosses.remove(boss);

                                               track= 0;

                                     }

                                     for(int j = 0; j < enplanes.size(); j++) {

                                               EnPlanee = enplanes.get(j);

                                               if(bullet.getRect().intersect(e.getRect())) {

                                                        list.remove(bullet);

                                                        enplanes.remove(e);

                                                        s+= 20;

                                               }

                                     }

                            }

                            for(int i = 0; i < enbullets.size(); i++) {

                                     EnBulletenbullet = enbullets.get(i);

                                     enbullet.draw(canvas);

                                     if(enbullet.getRect().intersect(plane.getRect())) {

                                               add--;

                                               enbullets.remove(enbullet);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < bigenbullets.size(); i++) {

                                     BigEnBulletbigenbullet = bigenbullets.get(i);

                                     bigenbullet.draw(canvas);

                                     if(bigenbullet.getRect().intersect(plane.getRect())) {

                                               add--;

                                               bigenbullets.remove(bigenbullet);

                                               if(add == -1) {

                                                        console= -1;

                                               }else {

                                                        more();

                                               }

                                     }

                            }

                            for(int i = 0; i < clears.size(); i++) {

                                     Clearclear = clears.get(i);

                                     clear.draw(canvas);

                                     if(clear.getRect().intersect(plane.getRect())) {

                                               destroy();

                                     }

                            }

                            plane.draw(canvas);

                            invalidate();

                   }else {

                            background= new Background(res, R.drawable.baozha);

                            background.draw(canvas);

                            AlertDialog.Builderbuilder = new Builder(context);

                            builder.setMessage("gameover  ! 再來一次嗎");

                            builder.setPositiveButton("繼續",

                                               newDialogInterface.OnClickListener() {

                                                        @Override

                                                        publicvoid onClick(DialogInterface arg0, int arg1) {

                                                                 //TODO Auto-generated method stub

                                                                 init();

                                                                 invalidate();

                                                        }

                                               });

                            builder.setNegativeButton("退出",

                                               newDialogInterface.OnClickListener() {

                                                        @Override

                                                        publicvoid onClick(DialogInterface arg0, int arg1) {

                                                                 //TODO Auto-generated method stub

//                                                              SharedPreferencessp=context.getSharedPreferences("遊戲",Context.MODE_PRIVATE);

//                                                              Editoredt=sp.edit();

//                                                             

//                                                              edt.putInt("分數",s);

//                                                              edt.commit();

//                                                             

//                                                              Intentintent =new Intent (context ,Name.class);

//                                                              context.startActivity(intent);

                                                                 System.exit(0);

                                                        }

                                               });

                            builder.create();

                            builder.show();

                   }

         }

         publicvoid init() {

                   timer.cancel();

                   removetimer.cancel();

                   if(lifes.size() != 0) {

                            for(int i = 0; i < lifes.size(); i++) {

                                     Lifelife = lifes.get(i);

                                     lifes.remove(life);

                            }

                   }

                   if(clears.size() != 0) {

                            for(int i = 0; i < clears.size(); i++) {

                                     Clearclear = clears.get(i);

                                     clears.remove(clear);

                            }

                   }

                   if(list.size() != 0) {

                            for (int i = 0; i < list.size();i++) {

                                     Bulletbullet = list.get(i);

                                     list.remove(bullet);

                            }

                   }

                   if(bosses.size() != 0) {

                            for(int i = 0; i < bosses.size(); i++) {

                                     Bossboss = bosses.get(i);

                                     bosses.remove(boss);

                            }

                   }

                   if(enplanes.size() != 0) {

                            for(int i = 0; i < enplanes.size(); i++) {

                                     EnPlaneenplane = enplanes.get(i);

                                     enplanes.remove(enplane);

                            }

                   }

                   if(bigenplanes.size() != 0) {

                            for(int i = 0; i < bigenplanes.size(); i++) {

                                     BigEnPlanebigenplane = bigenplanes.get(i);

                                     bigenplanes.remove(bigenplane);

                            }

                   }

                   if(enbullets.size() != 0) {

                            for(int i = 0; i < enbullets.size(); i++) {

                                     EnBulletenbullet = enbullets.get(i);

                                     enbullets.remove(enbullet);

                            }

                   }

                   if(bigenbullets.size() != 0) {

                            for(int i = 0; i < bigenbullets.size(); i++) {

                                     BigEnBulletbigenbullet = bigenbullets.get(i);

                                     bigenbullets.remove(bigenbullet);

                            }

                   }

                   if(bossbullets.size() != 0) {

                            for(int i = 0; i < bossbullets.size(); i++) {

                                     BossBulletbossbullet = bossbullets.get(i);

                                     bigenbullets.remove(bossbullet);

                            }

                   }

                   if(leftbossbullets.size() != 0) {

                            for(int i = 0; i < leftbossbullets.size(); i++) {

                                     LeftBossBulletleftbossbullet = leftbossbullets.get(i);

                                     leftbossbullets.remove(leftbossbullet);

                            }

                   }

                   if(rightbossbullets.size() != 0) {

                            for(int i = 0; i < rightbossbullets.size(); i++) {

                                     RightBossBulletrightbossbullet = rightbossbullets.get(i);

                                     rightbossbullets.remove(rightbossbullet);

                            }

                   }

                   background= new Background(res, R.drawable.background);

                   random= new Random();

                   bigenplanes= new ArrayList<BigEnPlane>();

                   bigenbullets= new ArrayList<BigEnBullet>();

                   enbullets= new ArrayList<EnBullet>();

                   bosses= new ArrayList<Boss>();

                   bossbullets= new ArrayList<BossBullet>();

                   leftbossbullets= new ArrayList<LeftBossBullet>();

                   rightbossbullets= new ArrayList<RightBossBullet>();

                   s= 0;

                   lifes= new ArrayList<Life>();

                   enplanes= new ArrayList<EnPlane>();

                   plane= new Plane(res,id);

                   list= new ArrayList<Bullet>();

                   removetimer= new Timer();

                   timer= new Timer();

                   timer.schedule(newTimerTask() {

                            @Override

                            publicvoid run() {

                                     //TODO Auto-generated method stub

                                     Bulletbullet = new Bullet(res);

                                     bullet.setX(plane.getX());

                                     bullet.setY(plane.getY());

                                     list.add(bullet);

                            }

                   },0, 800);

                   removetimer.schedule(newTimerTask() {

                            @Override

                            publicvoid run() {

                                     //TODO Auto-generated method stub

                                     for(int i = 0; i < bosses.size(); i++) {

                                               Bossboss = bosses.get(i);

                                               {

                                                        if(boss.getY() >= MainActivity.y) {

                                                                 bosses.remove(boss);

                                                                 track= 0;

                                                        }

                                               }

                                     }

                                     for(int i = 0; i < list.size(); i++) {

                                               Bulletbullet = list.get(i);

                                               if(bullet.getY() <= 0 - bullet.getBitmap().getHeight()) {

                                                        list.remove(bullet);

                                               }

                                     }

                                     for(int i = 0; i < enplanes.size(); i++) {

                                               EnPlaneenplane = enplanes.get(i);

                                               if(enplane.getY() >= MainActivity.y) {

                                                        enplanes.remove(enplane);

                                               }

                                     }

                                     for(int i = 0; i < bigenplanes.size(); i++) {

                                               BigEnPlanebigenplane = bigenplanes.get(i);

                                               if(bigenplane.getY() >= MainActivity.y) {

                                                        bigenplanes.remove(bigenplane);

                                               }

                                     }

                                     for(int i = 0; i < enbullets.size(); i++) {

                                              EnBulletenbullet = enbullets.get(i);

                                               if(enbullet.getY() >= MainActivity.y) {

                                                        enbullets.remove(enbullet);

                                               }

                                     }

                                     for(int i = 0; i < bossbullets.size(); i++) {

                                               BossBulletbossbullet = bossbullets.get(i);

                                               if(bossbullet.getY() >= MainActivity.y) {

                                                        bossbullets.remove(bossbullet);

                                               }

                                     }

                                     for(int i = 0; i < leftbossbullets.size(); i++) {

                                               LeftBossBulletleftbossbullet = leftbossbullets.get(i);

                                               if(leftbossbullet.getY() >= MainActivity.y) {

                                                        leftbossbullets.remove(leftbossbullet);

                                               }

                                     }

                                     for(int i = 0; i < rightbossbullets.size(); i++) {

                                               RightBossBulletrightbossbullet = rightbossbullets.get(i);

                                               if(rightbossbullet.getY() >= MainActivity.y) {

                                                        rightbossbullets.remove(rightbossbullet);

                                               }

                                     }

                                     for(int i = 0; i < bigenbullets.size(); i++) {

                                               BigEnBulletbigenbullet = bigenbullets.get(i);

                                               if(bigenbullet.getY() >= MainActivity.y) {

                                                        bigenbullets.remove(bigenbullet);

                                               }

                                     }

                                     for(int i = 0; i < lifes.size(); i++) {