I would like to make a point limit for my game developed on android

Asked

Viewed 224 times

2

I would like to make a point limit for my android game, example: when you reach 100 points he open another blank screen saying for example congratulations! but I’m having a hard time doing this is anyone who can help me?

This is my main class that draws the element on the screen!

public class GameView extends View implements Runnable {
private final static int INTERVAL = 25;
private boolean running = true;
private Paint paint;
private Inimigo[] inimigos;
private int pontos = 0;
private long tempo = System.currentTimeMillis();

private boolean jogoIniciado = false;

private Bitmap bmpFundo;

public GameView(Context context) {
    super(context);

    paint = new Paint();
    Thread minhaThread = new Thread(this);
    minhaThread.setPriority(Thread.MIN_PRIORITY);
    minhaThread.start();
}

public void iniciaJogo() {
    inimigos = new Inimigo[getHeight()/50];
    for (int i = 0; i < inimigos.length; i++) {
        int y = i*-50;
        int x = (int) (Math.random()*(getWidth()-25));
        inimigos[i] = new Inimigo(x, y, getResources());
    }

    bmpFundo = BitmapFactory.decodeResource(getResources(), R.drawable.fundo);
    bmpFundo = Bitmap.createScaledBitmap(bmpFundo, getWidth(), getHeight(), true);

    jogoIniciado = true;
}

public void run() {
    while (running) {
        try {
            Thread.sleep(INTERVAL);
        } catch (Exception e) {
            Log.e("Jogo", "Sleep da Thread");
        }
        update();
    }
}

private void update() {
    if (jogoIniciado==false) {
        return;
    }
    for (int i = 0; i < inimigos.length; i++) {
        inimigos[i].mexe(getHeight(), getWidth());
    }

    //invoca o método draw
    postInvalidate();

}

public void draw(Canvas canvas) {
    super.draw(canvas);
    if (jogoIniciado==false) {
        iniciaJogo();
    }

    //desenha cor de fundo
    //canvas.drawColor(Color.GREEN);
    canvas.drawBitmap(bmpFundo, 0, 0, paint);

    //define a cor do desenho
    paint.setColor(Color.RED);
    for (int i = 0; i < inimigos.length; i++) {
        inimigos[i].draw(canvas, paint);
    }

    //defino a cor do texto
    paint.setColor(Color.BLACK);
    paint.setTextSize(30);
    canvas.drawText("Corações: " + pontos, 20, 40, paint);

    /*int segundos = (int) (System.currentTimeMillis() - tempo)/1000;
    canvas.drawText("Tempo: " + segundos, 200, 30, paint);*/
}

public boolean onTouchEvent(MotionEvent event) {
    //pega o evento
    int action = event.getAction();
    //pega a posicao do dedo
    int x = (int) event.getX();
    int y = (int) event.getY();
    if (action == MotionEvent.ACTION_DOWN) {
        //afundou o dedo
        for (int i = 0; i < inimigos.length; i++) {
            if (inimigos[i].colide(x, y)) {
                inimigos[i].setX(-50);
                pontos ++;
            }
        }

    } else if (action==MotionEvent.ACTION_UP) {
        //soltou o dedo

    } else if (action==MotionEvent.ACTION_MOVE) {
        //movimentou o dedo

    }

    return super.onTouchEvent(event);
}

//termina o jogo
public void release() {
    running = false;
}
  • 2

    Hello, welcome to Sopt. You say that you "are in difficulty", but do not say what is the difficulty. You do not know how to open another screen? Can’t you compare the number of points? Can’t you accumulate points? The way the question is, you just can’t understand what your difficulty is. Please edit the question to make it clearer.

1 answer

0

You can create a method that checks the score by passing through the parameters the points the user earns during the course of the game, for example verificaPontuacao(int pontos);. Then, within the method, you make the condition to be redirecting to another Activity using Intent. With startActivity(intent), you call your classe to congratulate the player. This method you will call it where increments your score. So it would look like this:

 /**
 * Este método verifica se o jogador atingiu 100 pontos.
 * 
 * @param pontos
 */
public void verificaPontuacao(int pontos){
    if(pontos>=100){
        Intent intent = new Intent(getContext(), Main.class);
        getContext().startActivity(intent);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.