I am creating a game, how to implement a pause menu?

0

I am creating a game in Android studio using the SurfaceView class that is instantiated in MainActivity and a thread that is instantiated in SurfaceView, the problem is when I turn the screen on and it goes back to the initial state, ie it does not save the positions / state, in real, wanted to know how to implement a pause menu ...

IMAGES

Before pressing the off screen button (I tapped the screen to jump the [circle / bird])

afterpressingtheonscreenbutton(the[circle/bird]returnstothefallposition)

Source code below

MainActivity

public class MainActivity extends Activity {

private Game game;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FrameLayout container = findViewById(R.id.container);
    game = new Game(this);
    container.addView(game);
}
@Override
protected void onPause() {
    super.onPause();
    game.cancela();
}

@Override
protected void onResume() {
    super.onResume();
    game.inicia();
    new Thread(game).start();
      }
   }

Game Class

public class Game extends SurfaceView implements Runnable,View.OnTouchListener{
private final SurfaceHolder holder = getHolder();
private boolean isRunning = true;
private Player passaro;
private Bitmap background;
private Tela tela;


public Game(Context context) {
    super(context);
    setOnTouchListener(this);
    inicializaElementos();
}

private void inicializaElementos() {
    tela = new Tela(getContext());
    this.passaro = new Player();
    Bitmap back = BitmapFactory.decodeResource(getResources(),
            R.drawable.background);
    this.background = Bitmap.createScaledBitmap(back,
            tela.getLargura(), tela.getAltura(), false);

}

public void update() {
    passaro.update();
}

@Override
public void run() {
    while (isRunning) {
        if (!holder.getSurface().isValid()) continue;
        //Neste loop vamos gerenciar os elementos do Jumper.
        //
        Canvas canvas = holder.lockCanvas();
        update();
        //
        //Aqui vamos desenhar os elementos do jogo!
        canvas.drawBitmap(background, 0, 0, null);
        passaro.desenhaNo(canvas);
        //
        holder.unlockCanvasAndPost(canvas);
    }
}

public void cancela() {
    this.isRunning = false;
}

public void inicia() {
    this.isRunning = true;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    passaro.pula();
    return false;
     }
  }

Player class (bird)

public class Player {
private static final Paint vermelho = Cores.getCorDoPassaro();
private int x;
private static final int RAIO = 50;
private int y, velY = 0;
private int gravity = 1;
//private float fric = 0.3f;
//private boolean jump = false;

public Player() {
    this.y = 0;
    this.x = 100;
}

public void pula() {
    this.velY = -30;
    cai();
}

public void update() {
    if (chao()) {
        this.y = 1080 - RAIO;
        this.velY = 0;//(int) (-this.velY*this.fric);
    } else {
        cai();
    }
}


private boolean chao() {
    if (this.y >= 1080 - RAIO) {
        return true;
    }
    return false;
}

private void cai() {
    this.y += this.velY;
    this.velY += this.gravity;
}


public void desenhaNo(Canvas canvas) {
    canvas.drawCircle(x, y, RAIO, vermelho);
   }
}

Screen Class

public class Tela {
private DisplayMetrics metrics;

public Tela(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(
            Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    metrics = new DisplayMetrics();
    display.getMetrics(metrics);
}

public int getAltura() {
    return metrics.heightPixels;
}

public int getLargura() {
    return metrics.widthPixels;
   }
}

Colors Class

public class Cores {
public static Paint getCorDoPassaro() {
    Paint vermelho = new Paint();
    vermelho.setColor(0xFFFF0000);
    return vermelho;
   }
}
    
asked by anonymous 05.01.2018 / 18:11

1 answer

1

To pause in the game a simple way would be to create a variable and check inside the loop of your game.

Ex:

@Override
public void run() {
    while (isRunning) {
        if(isPaused) continue; // Aqui checa de o jogo esta pausado ou não;
        if (!holder.getSurface().isValid()) continue;
        //Neste loop vamos gerenciar os elementos do Jumper.
        //
        Canvas canvas = holder.lockCanvas();
        update();
        //
        //Aqui vamos desenhar os elementos do jogo!
        canvas.drawBitmap(background, 0, 0, null);
        passaro.desenhaNo(canvas);
        //
        holder.unlockCanvasAndPost(canvas);
    }
}

Now to pause the game and exit it and then back you need to save its information beforehand. You can use onSaveInstanceState to save and then within onCreate you reload them.

static final String Tag_intQualquer = "tint";
static final String Tag_stringQualquer = "tstring";

int intQualquer;
String stringQualquer;

@Override
public void onSaveInstanceState(Bundle savedInstanceState) 
{
    savedInstanceState.putInt(Tag_intQualquer, intQualquer);
    savedInstanceState.putString(Tag_stringQualquer, stringQualquer);

    super.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // verifica se existe algo salvo para ser recarregado
    if (savedInstanceState != null) 
    {
        carrega os dados do jogo anterior
        intQualquer = savedInstanceState.getInt(Tag_intQualquer);
        stringQualquer = savedInstanceState.getInt(Tag_stringQualquer);
        // com os dados carregados você gera um Player e manda para para a classe Game
    } 
    else 
    {
        // recomeçar o jogo do inicio           
    }
}

Then just make some modifications to the constructor of your Game class so that it can receive an object Player

public Game(Context context, Player old) 
{
    super(context);
    setOnTouchListener(this);
    inicializaElementos(old);
}

private void inicializaElementos(Player old)
{
    tela = new Tela(getContext());
    if(old == null)
    {
        this.passaro = new Player();
    }
    else
    {
        this.passaro = old;
    }
    Bitmap back = BitmapFactory.decodeResource(getResources(),
            R.drawable.background);
    this.background = Bitmap.createScaledBitmap(back,
            tela.getLargura(), tela.getAltura(), false);
}
    
05.01.2018 / 19:47