在Android游戏中得分显示

时间:2012-04-22 20:00:21

标签: android

我刚刚完成了一个安卓游戏“Kill Them All”,现在我想添加一个高分数据库,这是游戏的代码:

package kill.them.all;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
    private Bitmap bmp;
    private SurfaceHolder holder;
    private GameLoopThread gameLoopThread;
    private List<Sprite> sprites = new ArrayList<Sprite>();
    private long lastClick;
    private Bitmap bmpBlood;
    private List<TempSprite> temps = new ArrayList<TempSprite>();
public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new Callback() {

        public void surfaceDestroyed(SurfaceHolder holder) {

        }

        public void surfaceCreated(SurfaceHolder holder) {
            createSprites();
            gameLoopThread.setRunning(true);
            gameLoopThread.start();

        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });

bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);
}
private void createSprites() {
    sprites.add(createSprite(R.drawable.bad1));


}

private Sprite createSprite(int resouce) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
    return new Sprite(this, bmp);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
      for (int i = temps.size() - 1; i >= 0; i--) {
            temps.get(i).onDraw(canvas);
      }
    for (Sprite sprite : sprites) {
        sprite.onDraw(canvas);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 300) {
        lastClick = System.currentTimeMillis();
      synchronized (getHolder()) {
          float y=event.getY();
            float x=event.getX();
    for (int i = sprites.size() - 1; i >= 0; i--) {
        Sprite sprite = sprites.get(i);
        if (sprite.isCollition(x,y)) {
            sprites.remove(sprite);

            temps.add(new TempSprite(temps, this, x, y, bmpBlood));
            break;
        }
    }
    }
        }
        return true;
    }
}

我只想在比赛开始20秒后显示游戏结果和得分(你杀了多少sprirte)!但我不知道怎么做,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

//in global
int heroKill=0;

 if (sprite.isCollition(x,y)) {
        sprites.remove(sprite);
        heroKill++;
        temps.add(new TempSprite(temps, this, x, y, bmpBlood));
        break;
    }


 //indrawable
 lblHeroKillShow.setText(""+heroKill):

 //returning from game
public void onBackPressed() {
    super.onBackPressed();
// more implementation.
 //impelement database
 mydatabase.add(herokill);
}