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