How do I "reset something in a case from switch?

So, my switch code is this one:
switch(gamestate) {
case 0 :
arduboy.drawBitmap(0, 0, titlescreen, 68, 58, WHITE);
arduboy.setCursor(70, 30);
arduboy.print("Press A ");
if(arduboy.pressed(A_BUTTON)) {
gamestate = 1;
};
break;
//TITLU
case 1:
//JOC
if (arduboy.pressed(UP_BUTTON)) {
playery = playery - 1;
};
if (arduboy.pressed(DOWN_BUTTON)) {
playery = playery + 1;
};
if (arduboy.pressed(RIGHT_BUTTON)) {
playerx = playerx + 1;
};
if (arduboy.pressed(LEFT_BUTTON)) {
playerx = playerx - 1;
};
for ( int backgroundx = 0; backgroundx < 128; backgroundx = backgroundx + 8 ) {
for ( int backgroundy = 0; backgroundy < 64; backgroundy = backgroundy +8 ){
arduboy.drawBitmap(backgroundx, backgroundy, background, 8, 8, WHITE);
};
};

arduboy.fillRect(playerx ,playery, 16, 16, BLACK);
arduboy.drawBitmap(playerx, playery, player, 16, 16, WHITE);
if (playerx == 0) {
gamestate = 4;
}
if(playerx == 127) {
gamestate = 2;
}
if(playery == 0) {
gamestate = 4;
}
if (playery == 63) {
gamestate = 4;
}
arduboy.fillRect(0, 0, 128, 2, WHITE);
arduboy.fillRect(0, 0, 2, 64, WHITE);
arduboy.fillRect(0, 62, 128, 2, WHITE);
arduboy.fillRect(126, 0, 2, 64, WHITE);
//MARGINE JOS
//MARGINE DREAPTA

break;
case 2:
//NIVEL2
break;
case 3:
//AI CASTIGAT
break;
case 4:
** arduboy.setCursor(30, 15);**
** arduboy.print(“You lost! :(”);**
** arduboy.setCursor(5, 50);**
** arduboy.print(“Press A to try again”);**
** if(arduboy.pressed(A_BUTTON)) {**
** gamestate = 1;**
** }**
** //AI PIERDUT**
break;
}
So, if i press the A button while in the 4th case, it will get me in the first case, but the problem is that by pressing the A button, it gets me in the place that I “died”. Is there anyway to like “reset” the case and make it like it was the first time i ran it? Thanks for the help anyway. If you need the whole code, let me know in the comments. I will be more than pleased to leave it in the comments.

When you reset the game in the case 4 by pressing A you should reset the variable yourself here so everything is back to “like it was the first time”.
I think you only have playery and playerx variable so change this one to the default value or the value that you want.
Just remember that in the future you might have other variables (total lifes, timer of the level, score, ect. for exemple) to change when you die and here, could be the good place for it.

There is as many different ways to do the same game so don’t take what i say as “the way to do it”, it’s just close to what i did for the game i’ve slowly developing and i’m not a dev’ with a lot of knowledge.

1 Like

Thanks for the answer and thanks for supporting the arduboy community and the noobs like me that know little about programming. Thank you sir! It really helps!

In other words: put it in a function.
Preferably one called reset.

void reset(void)
{
  playerx = 0;
  playery = 0;
  // Reset other things here
}

and call reset from case 4:

case 4:
    arduboy.setCursor(30, 15);
    arduboy.print("You lost! :(");
    arduboy.setCursor(5, 50);
    arduboy.print("Press A to try again");
    if(arduboy.pressed(A_BUTTON))
    {
      reset();
      gamestate = 1;
    }
    break;

It would make future editing much easier.

3 Likes

Yes, but because I’m such a noob, can you give me an example of a function, or how do i use this Reset ? I’m kinnda confused…

That is an example.

The first block of code defines the function.
You have to put it somewhere outside of setup and loop.

The second block is your case 4: block.

The line reset(); is calling the reset function.
So when the computer reads reset(); it will do whatever reset tells it to.

It’s just like how arduboy.setCursor puts the text cursor at the right place and arduboy.print prints text where the cursor is, except your reset function resets the game state. (arduboy.setCursor and arduboy.print are functions, as are arduboy.fillRect, arduboy.drawBitmap etc.)

1 Like

Thanks! This really made me understand how functions work! And I gotta say it again, arduboy has such a great community around it that helps noobs like me.Thanks again!

1 Like