首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

自个儿编的安卓游戏

2013-03-14 
自己编的安卓游戏学院:烟台大学计算机学院项目名称:dogrun版本号:v1.0感言:自己这么做着玩,就像老虎啃天。

自己编的安卓游戏

学院:烟台大学计算机学院

项目名称:dogrun

版本号:v1.0

感言:自己这么做着玩,就像老虎啃天。能有个人带着做做项目就好了

主类:

package com.example.dogrun;import android.os.Bundle;import android.app.Activity;import android.content.pm.ActivityInfo;import android.view.Menu;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);this.requestWindowFeature(Window.FEATURE_NO_TITLE);this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(new MysurfaceView(this));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
package com.example.dogrun;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class MysurfaceView extends SurfaceView implements Callback,Runnable{SurfaceHolder hold;Canvas canvas;Thread thread;public static int screenH,screenW;boolean flag=false;Paint paint;final static int GAME_MENU=0;final static int GAME_BEGIN=1;final static int GAME_PAUSE=2;final static int GAME_EXIT=3;public static int gamestage=GAME_MENU;Bitmap background;Bitmap gamebegin;Bitmap gameexit;Bitmap meunback;Bitmap origle;Bitmap[] dogs=new Bitmap[5];Bitmap handle;GameMu Gamemu;GameBg Gamebg;Origle origles;Dog dog;public MysurfaceView(Context context) {super(context);// TODO Auto-generated constructor stubhold=this.getHolder();hold.addCallback(this);paint=new Paint();paint.setColor(Color.BLUE);setFocusable(true);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubswitch(gamestage){case GAME_MENU:Gamemu.onTouchEvent(event) ;;break;case GAME_BEGIN:dog.onTouchEvent(event);break;case GAME_PAUSE:;break;case GAME_EXIT:;break;default:;break;}return true;}@Overridepublic void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stubscreenH=this.getHeight();screenW=this.getWidth();initgame();flag=true;thread=new Thread(this);thread.start();}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stubflag=false;}public void myDraw(){canvas=hold.lockCanvas();try {if(canvas!=null){switch(gamestage){case GAME_MENU:Gamemu.draw(canvas, paint) ;break;case GAME_BEGIN:{Gamebg.draw(canvas, paint);dog.draw(canvas, paint);origles.myDraw(canvas, paint);};break;case GAME_PAUSE:;break;case GAME_EXIT:;break;default:;break;}}} catch (Exception e) {// TODO: handle exception}finally{if(canvas!=null){hold.unlockCanvasAndPost(canvas);}}}public void logic(){switch(gamestage){case GAME_MENU:;break;case GAME_BEGIN:{Gamebg.login();dog.logic();origles.logic();};break;case GAME_PAUSE:;break;case GAME_EXIT:;break;default:;break;}}public void initgame(){gamebegin=BitmapFactory.decodeResource(getResources(), R.drawable.startgame);gameexit=BitmapFactory.decodeResource(getResources(), R.drawable.exit);background=BitmapFactory.decodeResource(getResources(), R.drawable.background);meunback=BitmapFactory.decodeResource(getResources(), R.drawable.menuback);for(int i=0;i<5;i++){dogs[i]=BitmapFactory.decodeResource(getResources(), R.drawable.dog0+i);}handle=BitmapFactory.decodeResource(getResources(), R.drawable.handle);origle=BitmapFactory.decodeResource(getResources(), R.drawable.orie);Gamemu=new GameMu(gamebegin, gameexit, meunback);Gamebg=new GameBg(background, background);dog=new Dog(dogs,handle);origles=new Origle(origle);}@Overridepublic void run() {// TODO Auto-generated method stubwhile(flag){long start=System.currentTimeMillis();myDraw();logic();long end=System.currentTimeMillis();if((end-start)<50){try {Thread.sleep(50-(end-start));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

package com.example.dogrun;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;public class GameBg {Bitmap back1,back2;int bx1,by1,bx2,by2;int speed=6;public GameBg(Bitmap back1, Bitmap back2) {super();this.back1 = back1;this.back2 = back2;bx1=-back1.getWidth()+MysurfaceView.screenW;by1=-back1.getHeight()+MysurfaceView.screenH;bx1=-back2.getWidth()+MysurfaceView.screenW+100;by2=-back2.getHeight()+MysurfaceView.screenH;}public void draw(Canvas canvas,Paint paint){canvas.drawBitmap(back1, bx1, by1, paint);canvas.drawBitmap(back2, bx2, by2, paint);}public void login() {// TODO Auto-generated method stubbx1+=speed;bx2+=speed;if(bx1>MysurfaceView.screenW){bx1=bx2-back1.getWidth()+100;}if(bx2>MysurfaceView.screenW){bx2=bx1-back2.getWidth()+100;}}}


 

package com.example.dogrun;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.view.MotionEvent;import android.view.View;public class GameMu {Bitmap gamebigen,gameexit,beginback;int bx,by,ex,ey;public GameMu(Bitmap gamebigen, Bitmap gameexit, Bitmap beginback) {super();this.gamebigen = gamebigen;this.gameexit = gameexit;this.beginback = beginback;bx=MysurfaceView.screenW/2-gamebigen.getWidth()/2;by=MysurfaceView.screenH/2-gamebigen.getHeight()/2;ex=MysurfaceView.screenW/2-gameexit.getWidth()/2;ey=4*MysurfaceView.screenW/7-gameexit.getWidth()/2;}public void draw(Canvas canvas,Paint paint) {// TODO Auto-generated method stubcanvas.drawBitmap(beginback, 0, 0, paint);canvas.drawBitmap(gamebigen, bx, by, paint);canvas.drawBitmap(gameexit, ex, ey, paint);}public boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubint x=(int) event.getX();int y=(int) event.getY();System.out.println(x+"....."+y);if(x>=bx&&y>=by&&x<=(bx+gamebigen.getWidth())&&by<=(by+gamebigen.getHeight())){MysurfaceView.gamestage=MysurfaceView.GAME_BEGIN;Origle.leftlife=10;}if(x>=ex&&y>=ey&&x<=(ex+gameexit.getWidth())&&ey<=(ey+gameexit.getHeight())){MysurfaceView.gamestage=MysurfaceView.GAME_EXIT;System.exit(0);}return true;}}


 

package com.example.dogrun;import java.util.Random;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.view.MotionEvent;public class Origle {Bitmap origle;int number=10;boolean death=false;int rx1,ry1=0,rx2,ry2=70,rx3,ry3=140,rx4,ry4=210,rx5,ry5=280,ry6=350;int dom1,dom2,dom3,dom4,dom5;Random random,random1,random2,random3,random4,random5;int score=0;public static int leftlife=10;public Origle(Bitmap origle) {super();this.origle = origle;random=new Random();random1=new Random();random2=new Random();random3=new Random();random4=new Random();random5=new Random();dom1=1;dom2=1;dom3=2;dom4=3;dom5=4;}public void myDraw(Canvas canvas,Paint paint){paint.setTextSize(36);canvas.drawText("生命:"+leftlife, MysurfaceView.screenW/5, MysurfaceView.screenH-40, paint);canvas.drawText("score:"+score, MysurfaceView.screenW/5, MysurfaceView.screenH-80, paint);canvas.drawText("起跳高度:"+(400-Dog.bmpH), MysurfaceView.screenW/2, MysurfaceView.screenH-40, paint);canvas.save();canvas.clipRect(rx1, 0, rx1+dom1*20, 70);canvas.drawBitmap(origle, 0, 0, paint);canvas.restore();canvas.save();canvas.clipRect(rx2, 70, rx2+dom2*20, 140);canvas.drawBitmap(origle, 0, 0, paint);canvas.restore();canvas.save();canvas.clipRect(rx3, 140, rx3+dom3*20, 210);canvas.drawBitmap(origle, 0, 0, paint);canvas.restore();canvas.save();canvas.clipRect(rx4, 210, rx4+dom4*20, 280);canvas.drawBitmap(origle, 0, 0, paint);canvas.restore();canvas.save();canvas.clipRect(rx5, 280, rx5+dom5*20, 350);canvas.drawBitmap(origle, 0, 0, paint);canvas.restore();}public boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn true;}public void logic(){if(leftlife<=0){MysurfaceView.gamestage=MysurfaceView.GAME_MENU;}number+=20;if(number>=760){number=0;dom1=random1.nextInt(2);dom2=random2.nextInt(3);dom3=random3.nextInt(4);dom4=random4.nextInt(5);dom5=random5.nextInt(6);rx1=number-random1.nextInt(6)*20;rx2=number-random2.nextInt(6)*20;rx3=number-random3.nextInt(6)*20;rx4=number-random4.nextInt(6)*20;rx5=number-random5.nextInt(6)*20;}if(number>=400){death=false;}rx1+=20;rx2+=20;rx3+=20;rx4+=20;rx5+=20;isdeath();}public void isdeath(){if( (rx1+dom1*20)>=700){if(rx1>700){return;}if(Dog.bmpH<ry2 && Dog.bmpH+38>ry2){leftlife--;score-=dom1*5;}else if(Dog.bmpH<ry2){score+=5;}}if( (rx2+dom2*20)>=700){if(rx2>700){return;}if(Dog.bmpH<ry3 && Dog.bmpH+38>ry3){leftlife--;score-=dom2*4;}else if(Dog.bmpH<ry3){score+=4;}}if( (rx3+dom3*20)>=700){if(rx3>700){return;}if(Dog.bmpH<ry4 && Dog.bmpH+38>ry4){leftlife--;score-=dom3*3;}else if(Dog.bmpH<ry4){score+=3;}}if( (rx4+dom4*20)>=700){if(rx4>700){return;}if(Dog.bmpH<ry5 && Dog.bmpH+38>ry5){leftlife--;score-=dom4*2;}else if(Dog.bmpH<ry5){score+=2;}}if( (rx5+dom5*20)>=700){if(rx5>700){return;}if(Dog.bmpH<ry6 && Dog.bmpH+38>ry6){leftlife--;score-=dom5*1;}else if(Dog.bmpH<ry6){score+=1;}}}}


 

package com.example.dogrun;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.view.MotionEvent;public class Dog {Bitmap[] dogs=new Bitmap[5];Bitmap handle;    int number=4;    float bigCenterX=0,bigCenterY=MysurfaceView.screenH/2,smallCenterX=0,smallCenterY=MysurfaceView.screenH/2,bigCenterR=100,smallCenterR=30;float angle;boolean run=true;int flash=0;int flashJump=0;int a=7;int intangle;public static float bmpH;        public Dog(Bitmap[] dogs,Bitmap handle) {super();this.dogs = dogs;this.handle=handle;}public void draw(Canvas canvas,Paint paint){if(run){drawRun(canvas,paint);}else{drawJump(canvas,paint);}paint.setAlpha(0x77);canvas.drawCircle(bigCenterX, bigCenterY, bigCenterR, paint);paint.setColor(Color.GREEN);canvas.drawCircle(smallCenterX, smallCenterY, smallCenterR, paint);}public void drawRun(Canvas canvas,Paint paint){canvas.drawBitmap(dogs[flashJump], (7*MysurfaceView.screenW)/8, 5*MysurfaceView.screenH/6, paint);}public void drawJump(Canvas canvas ,Paint paint){canvas.drawText("起跳档位"+a, MysurfaceView.screenW/4, MysurfaceView.screenH/3+40, paint);canvas.drawBitmap(handle, (7*MysurfaceView.screenW)/8, bmpH, paint);}public void logic(){if(run){flashJump++;if(flashJump>=4){flashJump=0;}}else{number++;flash=20%number;if(number==20){number=0;run=true;}bmpH=(float) (0.4*a*number*number-8*a*number+400);}}public void setSmallCenterXY(float bigCenterX,float bigCenterY,float bigCenterR,double rad){smallCenterX=(float) (bigCenterR*Math.cos(rad)+bigCenterX);smallCenterY=(float) (bigCenterR*Math.sin(rad)+bigCenterY);}public float getRad(float px1,float py1,float px2,float py2){float x=px2-px1;float y=py2-py1;float line=(float) Math.sqrt(x*x+y*y);float cosAngle=x/line;float rad = (float) Math.acos(cosAngle);if(py2<py1){rad=-rad;}return rad;}public boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubif (event.getAction() == MotionEvent.ACTION_UP) {smallCenterX = bigCenterX;smallCenterY = bigCenterY;} else {int pointX = (int) event.getX();int pointY = (int) event.getY();angle= getRad(bigCenterX, bigCenterY, pointX, pointY);intangle=(int) (angle*10);a=geta(intangle);run=false;if (((pointX-bigCenterX) * (pointX-bigCenterX) + (pointY-bigCenterY) * (pointY-bigCenterY)) < bigCenterR * bigCenterR) {smallCenterX = pointX;smallCenterY = pointY;} else{setSmallCenterXY(bigCenterX, bigCenterY, bigCenterR,angle);}}return true;}public int geta(int x){if(x>=6){a=2;return a;}if(x>=3){a=3;return a;}if(x>=0){a=4;return a;}if(x>=-3){a=5;return a;}if(x>=-6){a=6;return a;}if(x>=-9){a=7;return a;}if(x>=-11){a=8;return a;}if(x>=-13){a=9;return a;}a=10;return a;}}



 自个儿编的安卓游戏自个儿编的安卓游戏

 

 

热点排行