项目下载地址 http://download.csdn.net/detail/u010184245/9004137
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameSurface surface=new GameSurface(MainActivity.this);
setContentView(surface);
}
}
GameSurface.java
public class GameSurface extends SurfaceView implements Callback{
public static boolean flag=true;
public static boolean isFlag() {
return flag;
}
public static void setFlag(boolean flag) {
GameSurface.flag = flag;
}
SurfaceHolder holder;
private Paint paint;
private boolean isRunning;
Canvas canvas;
private Background background;
private Bird bird;
Context context;
private Bound bound;
private int count1=0;
private Bitmap bitmap;
public GameSurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder=getHolder();
holder.addCallback(this);
paint = new Paint();
bird = new Bird(context);
background = new Background(context);
this.context=context;
bound=new Bound(context);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
bitmap = new BitmapFactory().decodeResource(context.getResources(), R.drawable.faill);
isRunning=true;
flag=true;
new startThread().start();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
isRunning=false;
flag=true;
}
class startThread extends Thread{
@Override
public void run() {
while(isRunning){
canvas = holder.lockCanvas();
if (canvas == null)
continue;
canvas.drawColor(0xFFFFFFFF);
draw(canvas);
faill(canvas, paint, flag);
logic();
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
try {
sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void draw(Canvas canvas){
background.draw(canvas, paint);
bird.draw(canvas, paint,count1);
count1++;
bound.draw(canvas, paint);
if(bound.isCollection(bird)){
bird.setFlag(false);
background.setFlag(false);
bound.setFlag(false);
flag=false;
}
}
public void logic(){
background.logic();
bird.logic();
bound.logic();
}
public boolean onTouchEvent(MotionEvent event) {
bird.move();
return true;
}
public void faill(Canvas canvas,Paint paint,Boolean flag){
if(flag){
}
else{
canvas.drawBitmap(bitmap, 200-bitmap.getWidth()/2, 300-bitmap.getHeight()/2, paint);
}
}
}
Bound.java
public class Bound {
private int x=830;
private int y_down=380,y_up=-200;
private int speed=10;
private Bitmap bound_down;
private Bitmap bound_up;
private boolean flag=true;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Bound(Context context) {
super();
bound_down = BitmapFactory.decodeResource(context.getResources(),R.drawable.bound_down);
bound_up = BitmapFactory.decodeResource(context.getResources(),R.drawable.bound_up);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public void draw(Canvas canvas,Paint paint){
canvas.drawBitmap(bound_down, x, y_down, paint);
canvas.drawBitmap(bound_up, x, y_up, paint);
}
public void setY(int y_down) {
this.y_down = 800-y_down;
y_up=180-y_down;
}
public void logic(){
if(flag){
x-=speed;
if(x<-68){
x=480;
while(true){
int height=(int)(Math.random()*400);
if(height>180)
{
setY(height);
break;
}
}
}
}
else{
}
}
//碰撞检测
public boolean isCollection(Bird bird){
int left=200;
int right=200+bird.getBitmap().getWidth();
int top=bird.getY();
int bottom=bird.getY()+bird.getBitmap().getHeight();
//左边
if(rightreturn false;
//右边
if(x+bound_down.getWidth()return false;
//上边
if(bottomy_up+420)
return false;
return true;
}
}
Bird.java
public class Bird {
private int x=200,y=400,speed=0;
Context context;
private Bitmap bitmap,bitmap2,bitmap3;
private boolean flag=true;
public Bird(Context context) {
super();
this.context=context;
bitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.bird_blue_0);
bitmap2= BitmapFactory.decodeResource(context.getResources(), R.drawable.bird_blue_1);
bitmap3= BitmapFactory.decodeResource(context.getResources(), R.drawable.bird_blue_2);
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public void draw(Canvas canvas,Paint paint,int count1){
if(speed>0)
canvas.drawBitmap(bitmap, 200, y, paint);
else{
switch(count1%3){
case 0:
canvas.drawBitmap(bitmap, 200, y, paint);
break;
case 1:
canvas.drawBitmap(bitmap2, 200, y, paint);
break;
case 2:
canvas.drawBitmap(bitmap3, 200, y, paint);
}
}
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void logic(){
if(flag){
y+=speed;
speed++;
if(y<0)
y=0;
if(y>700){
GameSurface.flag=false;
}
}
else{
speed=20;
y+=speed;
speed++;
}
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void move(){
speed=-13;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
Backgroud.java
public class Background {
private int x = 0, x2 = 400;
private int speed;
Context context;
private Bitmap bitmap;
private boolean flag = true;
public Background(Context context) {
super();
this.context = context;
speed = 10;
bitmap = new BitmapFactory().decodeResource(context.getResources(),
R.drawable.background);
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bitmap, x, 0, paint);
canvas.drawBitmap(bitmap, x2, 0, paint);
}
public void logic() {
if (flag) {
x -= speed;
x2 -= speed;
if (x < -400)
x = 400;
if (x2 < -400)
x2 = 400;
} else {
}
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}