android小游戏 飞机躲子弹
最近android老师让每人写一个小东西,因为之前学awt时写过一个java版的飞机躲子弹,所以这次想写成android版的。
文件直接导入就行https://files.cnblogs.com/files/lolybj/PlaneGame.rar
项目比较简单。主要用了SurfaceView,
只放出主要的类,
1 public class GameView extends SurfaceView implements SurfaceHolder.Callback{ 2 3 4 private boolean threadFlag; 5 private int view_width; 6 private int view_height; 7 private int plane_x; 8 private int plane_y; 9 private boolean isLive; 10 private boolean isBao; 11 private int speed; 12 private int time; 13 private int time1; 14 private Date startTime; 15 private Date endTime; 16 private SurfaceHolder surfaceHolder; 17 private Canvas canvas; 18 private String TAG = "提示"; 19 private Bitmap bgBitmap ; 20 private Bitmap planeBitmap; 21 private Bitmap[] baoBitmaps; 22 private Bullet bullet; 23 private Plane plane; 24 private Context context; 25 private SharedPreferences preferences; 26 private SharedPreferences.Editor editor; 27 28 ArrayList<Bullet> bullets = new ArrayList<Bullet>(); 29 30 31 public GameView(Context context,int speed) { 32 super(context); 33 surfaceHolder = this.getHolder(); 34 surfaceHolder.addCallback(this); 35 this.context = context; 36 this.speed = speed; 37 38 //System.out.println(getWidth()); 39 //System.out.println(getHeight()); 40 41 42 // TODO Auto-generated constructor stub 43 } 44 45 public void init(){ 46 bgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg); 47 planeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.plane); 48 baoBitmaps = new Bitmap[20]; 49 baoBitmaps[0] = BitmapFactory.decodeResource(getResources(), R.drawable.e1); 50 for(int i=0;i<9;i++){ 51 baoBitmaps[i+1] = BitmapFactory.decodeResource(getResources(), R.drawable.e2+i); 52 } 53 for(int i=0;i<7;i++){ 54 baoBitmaps[i+9] = BitmapFactory.decodeResource(getResources(), R.drawable.e10+i); 55 } 56 Constant.SetView(getWidth(), getHeight()); 57 view_width = getWidth(); 58 view_height = getHeight(); 59 plane = new Plane(view_width/2-planeBitmap.getWidth()/2,view_height-planeBitmap.getHeight()-150,planeBitmap.getWidth(),planeBitmap.getHeight()); 60 startTime = new Date(); 61 isLive = true; 62 isBao = false; 63 threadFlag = true; 64 for(int i=0;i<20;i++) 65 { 66 bullet = new Bullet(view_width/2, view_height/2, speed); 67 bullets.add(bullet); 68 } 69 preferences = context.getSharedPreferences("test",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); 70 editor = preferences.edit(); 71 time = preferences.getInt("one", 0); 72 73 /*editor.putInt("one", 0); 74 editor.putInt("two", 0); 75 editor.putInt("three", 0); 76 editor.commit();*/ 77 } 78 79 80 private Thread mThread = new Thread(new Runnable() { 81 82 @Override 83 public void run() { 84 while(threadFlag){ 85 86 canvas = surfaceHolder.lockCanvas(); 87 88 Paint paint = new Paint(); 89 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 90 canvas.drawPaint(paint); 91 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 92 93 canvas.drawBitmap(bgBitmap, 0, 0, null); 94 95 for(int i=0;i<bullets.size();i++){ 96 bullet = bullets.get(i); 97 bullet.drawSelf(canvas); 98 if(bullet.getRect().intersect(plane.getRect())){ 99 100 isLive = false; 101 } 102 103 } 104 if(isLive){ 105 canvas.drawBitmap(planeBitmap,plane.getPlane_x(), plane.getPlane_y(), null); 106 } 107 else{ 108 if(!isBao){ 109 for(int i=0;i<16;i++){ 110 111 canvas.drawBitmap(baoBitmaps[i], plane.getPlane_x(), plane.getPlane_y(), null); 112 } 113 isBao = true; 114 endTime = new Date(); 115 116 time1 = (int) ((endTime.getTime()-startTime.getTime())/1000); 117 118 119 } 120 paint.setColor(Color.RED); 121 paint.setTextSize(80); 122 canvas.drawText("GAME_OVER!", Constant.getGAME_WIDTH()/2-200, Constant.getGAME_HIGHT()/2-100, paint); 123 paint.setTextSize(30); 124 canvas.drawText("你存活了"+(endTime.getTime()-startTime.getTime())/1000+"秒", Constant.getGAME_WIDTH()/2-100, Constant.getGAME_HIGHT()/2, paint); 125 if(time<time1){ 126 editor.putInt("one", time1); 127 editor.commit(); 128 } 129 paint.setTextSize(25); 130 paint.setColor(Color.YELLOW); 131 canvas.drawText("最高记录秒"+preferences.getInt("one", 5), Constant.getGAME_WIDTH()/2-70, Constant.getGAME_HIGHT()/2+100, paint); 132 133 //Log.i(TAG, "GAME_OVER!"); 134 135 136 } 137 138 if(canvas !=null){ 139 surfaceHolder.unlockCanvasAndPost(canvas); 140 141 } 142 143 try { 144 Thread.sleep(40); 145 } catch (InterruptedException e) { 146 e.printStackTrace(); 147 } 148 } 149 } 150 }); 151 152 153 @Override 154 public void surfaceCreated(SurfaceHolder holder) { 155 156 init(); 157 158 mThread.start(); 159 160 //canvas.drawBitmap(imageBitmap, 0, 0, null); 161 } 162 163 164 @Override 165 public boolean onTouchEvent(MotionEvent event) { 166 // TODO Auto-generated method stub 167 //System.out.println("监听到了"); 168 switch(event.getAction()){ 169 case MotionEvent.ACTION_DOWN: 170 if((int)event.getX()>0&& 171 (int)event.getX()<view_width-plane.getPlane_width()&& 172 (int)event.getY()>0&& 173 (int)event.getY()<view_height-plane.getPlane_height()){ 174 plane.setPlane_x((int)event.getX()); 175 plane.setPlane_y((int)event.getY()); 176 } 177 178 break; 179 case MotionEvent.ACTION_MOVE: 180 if((int)event.getX()>0&& 181 (int)event.getX()<view_width-plane.getPlane_width()&& 182 (int)event.getY()>0&& 183 (int)event.getY()<view_height-plane.getPlane_height()){ 184 plane.setPlane_x((int)event.getX()); 185 plane.setPlane_y((int)event.getY()); 186 } 187 break; 188 case MotionEvent.ACTION_UP: 189 if((int)event.getX()>0&& 190 (int)event.getX()<view_width-plane.getPlane_width()&& 191 (int)event.getY()>0&& 192 (int)event.getY()<view_height-plane.getPlane_height()){ 193 plane.setPlane_x((int)event.getX()); 194 plane.setPlane_y((int)event.getY()); 195 } 196 break; 197 } 198 199 //return super.onTouchEvent(event); 200 return true; 201 } 202 @Override 203 public void surfaceChanged(SurfaceHolder holder, int format, int width, 204 int height) { 205 // TODO Auto-generated method stub 206 207 } 208 @Override 209 public void surfaceDestroyed(SurfaceHolder holder) { 210 // TODO Auto-generated method stub 211 212 } 213 }
主要的方法,重写surfaceCreated方法,本类的入口。init()方法数据初始化。thread线程方法,绘制坐标移动后的画面。重新onTouchEvent()监听鼠标坐标变化。