I am having a few troubles with collsission

OK, so i have a game where there is a switch and 3 different gamemodes.
1.Title screen
2.The game
3.Death screen
well i was getting into the meat of the game, when i added collision to a rectangle so that when the player hits it, they would die. unfortunately once i added the
if (arduboy.collide(player, block2)) {
gamestate = 2;}
it works, but instead of sending me to gamestate 2, it sadly just turns the screen black. and i am no longer able to do anything until i turn off and back on. any help would be much apreciated. :slight_smile: cheers

the whole thing. it is not very long

#include <Arduboy2.h>
Arduboy2 arduboy;

int gamestate = 0;
int playerwidth = 6;
int playerheight = 6;
int playerx = 60;
int playery = 35;
int enemyx = 150;
int enemyy = 27;
int enemy2x = 150;
int enemy2y = 48;
int alwaystrue = 1;
static bool spawnenemys;
static bool scenery;
void setup() {
  arduboy.begin();
  
  scenery = true;
  spawnenemys = true;
  
  arduboy.setFrameRate(30);
}


void loop() {
  if (!(arduboy.nextFrame()))
    return;  
  arduboy.clear();
  arduboy.pollButtons();
  switch (gamestate){
    case 0:
      arduboy.setCursor(0, 0);
      arduboy.print("Welcome to");
      arduboy.setCursor(0, 10);
      arduboy.print("the title screen!");
      arduboy.setCursor(0, 20);
      arduboy.print("Press B to start");
      if (arduboy.justPressed(B_BUTTON)){
        gamestate = 1;}
      break;
    case 1:
      Rect player = Rect(playerx, playery, playerwidth, playerheight);
      Rect block1 = Rect(enemyx, enemyy, 5, 12);
      Rect block2 = Rect(enemy2x, enemy2y, 5, 12);
      if (scenery = true){
        arduboy.drawLine(0, 25, 127, 25, WHITE);
        arduboy.drawRect(3, 16, 12, 10, WHITE);
        arduboy.drawRect(30, 13, 8, 13, WHITE);
        arduboy.drawRect(40, 8, 15, 18, WHITE);
        arduboy.drawRect(80, 21, 8, 5, WHITE);
        arduboy.drawRect(103, 23, 17, 3, WHITE);
      }
      arduboy.drawRect(playerx, playery, playerwidth, playerheight, WHITE);
      if (arduboy.pressed(LEFT_BUTTON) && playerx > 0){
        playerx = playerx - 2;}
      if (arduboy.pressed(RIGHT_BUTTON) && playerx < 122){
        playerx = playerx + 2;}
      if (arduboy.pressed(UP_BUTTON) && playery > 25){
        playery = playery - 1;}
      if (arduboy.pressed(DOWN_BUTTON) && playery < 58){
        playery = playery + 1;}
      arduboy.fillRect(enemyx, enemyy, 4, 14, WHITE);
      arduboy.fillRect(enemy2x, enemy2y, 4, 14, WHITE);
      if (spawnenemys = true){
        enemyx = enemyx - 1;
        enemy2x = enemy2x - 1.5;
        if (enemyx == -20){
          enemyx = 150;}
        if (enemy2x == -20){
          enemy2x = 150;}}
      if (arduboy.collide(player, block1)) {
       arduboy.setCursor (playerx - 20, playery);
       arduboy.print("AAAAAAAh");
       gamestate = 2;}
      if (arduboy.collide(player, block2)) {
       arduboy.setCursor (playerx - 20, playery);
       arduboy.print("AAAAAAAh");
       gamestate = 2;}
      break;
    case 2:
      arduboy.setCursor(0, 0);
      arduboy.print("This is death.");
      arduboy.setCursor(0, 10);
      arduboy.print("Press B to");
      arduboy.setCursor(0, 20);
      arduboy.print("reincarnate");
      if (arduboy.justPressed(B_BUTTON)){
        gamestate = 0;}
      break;
    }
  arduboy.display();
}

I’ve moved the code into the forum post so it’s easier for people to see.
For such short code there’s no need to rely on a third party host.


Your problem is that you are using variables in the case areas.
To be able to use variables in cases you need to add extra braces to protect the variables from conflicting with each other, like this:

switch(variable)
{
	case 0:
	{
		// Variables and other code
		break;
	}
	case 1:
	{
		// Variables and other code
		break;
	}
	case 2:
	{
		// Variables and other code
		break;
	}
	// Et cetera
}

(There’s a silly and boring technical reason for this.)


You have a few other oddities as well by the way…

spawnenemys and scenery don’t need to be static.

= is the assignment operator, so when you do if (scenery = true) you’re actually assigning the value true to scenery, not testing whether scenery is true, so the condition will always be true.
This is one of the reasons why it’s generally considered bad practice to attempt to write if (scenery == true).
Instead you should just write if (scenery).

That being said, you set scenery and spawnenemys to true in setup and you never set them to false so actually they’re not doing anything and you could get rid of them.

And although it’s not a problem at the moment, you should use the F macro around your strings so they get stored in progmem instead of RAM.

Taking those things into account your code would look more like this:

#include <Arduboy2.h>
Arduboy2 arduboy;

int gamestate = 0;
int playerwidth = 6;
int playerheight = 6;
int playerx = 60;
int playery = 35;
int enemyx = 150;
int enemyy = 27;
int enemy2x = 150;
int enemy2y = 48;
int alwaystrue = 1;

void setup()
{
  arduboy.begin();  
  arduboy.setFrameRate(30);
}


void loop()
{
  if (!arduboy.nextFrame())
    return;

  arduboy.clear();
  arduboy.pollButtons();

  switch (gamestate)
  {
    case 0:
    {
      arduboy.setCursor(0, 0);
      arduboy.print(F("Welcome to"));
      arduboy.setCursor(0, 10);
      arduboy.print(F("the title screen!"));
      arduboy.setCursor(0, 20);
      arduboy.print(F("Press B to start"));
      
      if (arduboy.justPressed(B_BUTTON))
      {
        gamestate = 1;
      }
      
      break;
    }
    case 1:
    {
      Rect player = Rect(playerx, playery, playerwidth, playerheight);
      Rect block1 = Rect(enemyx, enemyy, 5, 12);
      Rect block2 = Rect(enemy2x, enemy2y, 5, 12);

      arduboy.drawLine(0, 25, 127, 25, WHITE);
      arduboy.drawRect(3, 16, 12, 10, WHITE);
      arduboy.drawRect(30, 13, 8, 13, WHITE);
      arduboy.drawRect(40, 8, 15, 18, WHITE);
      arduboy.drawRect(80, 21, 8, 5, WHITE);
      arduboy.drawRect(103, 23, 17, 3, WHITE);

      arduboy.drawRect(playerx, playery, playerwidth, playerheight, WHITE);
      
      if (arduboy.pressed(LEFT_BUTTON) && playerx > 0)
      {
        playerx = playerx - 2;
      }
      
      if (arduboy.pressed(RIGHT_BUTTON) && playerx < 122)
      {
        playerx = playerx + 2;
      }
      
      if (arduboy.pressed(UP_BUTTON) && playery > 25)
      {
        playery = playery - 1;
      }
      
      if (arduboy.pressed(DOWN_BUTTON) && playery < 58)
      {
        playery = playery + 1;
      }
      
      arduboy.fillRect(enemyx, enemyy, 4, 14, WHITE);
      arduboy.fillRect(enemy2x, enemy2y, 4, 14, WHITE);
      
      enemyx = enemyx - 1;
      enemy2x = enemy2x - 1.5;
      
      if (enemyx == -20)
      {
        enemyx = 150;
      }
      
      if (enemy2x == -20)
      {
        enemy2x = 150;
      }

      if (arduboy.collide(player, block1))
      {
        arduboy.setCursor (playerx - 20, playery);
        arduboy.print(F("AAAAAAAh"));
        gamestate = 2;
      }

      if (arduboy.collide(player, block2))
      {
        arduboy.setCursor (playerx - 20, playery);
        arduboy.print(F("AAAAAAAh"));
        gamestate = 2;
      }
      break;
    }
    case 2:
    {
      arduboy.setCursor(0, 0);
      arduboy.print(F("This is death."));
      arduboy.setCursor(0, 10);
      arduboy.print(F("Press B to"));
      arduboy.setCursor(0, 20);
      arduboy.print(F("reincarnate"));
      
      if (arduboy.justPressed(B_BUTTON))
      {
        gamestate = 0;
      }
      
      break;
    }
  }

  arduboy.display();
}

Your code then has another bug which should become apparent when you run it, but I don’t know whether you want me to reveal what’s wrong or whether you want to figure it out on your own.

1 Like

Thank you so much, this is my first ever game in arduboy let alon c++ thanks you v much!

1 Like

I would have thought that would fail to compile with a ‘cross initialization’ error. Is this another -permissive trick?

I couldn’t say for definite without running some tests, but I believe so.
Visual Studio certainly doesn’t allow this kind of thing,
so I would assume (or at least hope) that GCC wouldn’t allow it under normal circumstances.
Unfortunately there’s no comprehensive list of what kind of horrors -fpermissive allows.