1

I am trying to create a basic snake game using the arduino UNO along with the MAX72XX module, the LCD module and the analog joystick module. I am wanting to program my program using object orientated programming. I have no experience with C++ so I am struggling quite a bit to understand what the following error is trying to say to me: request for member 'following_direction' in 'snake[0]', which is of non-class type 'int'.Here is the full code:

#include "LedControl.h"
#include <LiquidCrystal.h>
LedControl lc=LedControl(12,10,11,1);
LiquidCrystal lcd(8, 13, 3, 5, 6, 7);


/*lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);*/

class Snake_body
{
  //private:




  public:

    int following_direction;
    int body_length[64];
    int x;
    int y;
    int previous_x;
    int previous_y;

    Snake_body(int x_value, int y_value) 
    {
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;
    }
    /*void configure(int x_value, int y_value)
    {
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;  
    }*/

};


class Power_up
{
  private:

  public:
    virtual void init() 
    {
      this->x = 0;
      this->y = 0;
    }

    int x;
    int y;
};

int snake[64];
int snakes_length;


Snake_body *snake_body;
Power_up *power_up;



void setup() {

  lcd.begin(16, 1);

  snakes_length = 1;

  for(int z = 1; z < 64; z++)
  {
  snake[z] = 0;
  }

  snake[0] = new Snake_body(0,0);
  //Snake_body snake[0] = new Snake_body();
  //snake[0].configure(0,0);
  // put your setup code here, to run once:
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);

  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  Serial.begin(9600);
  power_up->x = random(0, 8);
  power_up->y = random(0, 8);
}

int snake_direction;


void loop() 
{
  int x_axis = analogRead(A0);
  int y_axis = analogRead(A1);
  int switcher = digitalRead(2);


   if(x_axis <= 400) //&& x < 7)
   {
     snake[0].following_direction = 0;
     //x += 1;
     //Serial.println("right");
   }


   else if(x_axis >= 600) //&& x > 0)
   {
     snake[0].following_direction = 1;
     //x -= 1;
     //Serial.println("left");
   }


   else if(y_axis <= 400) //&& y > 0)
   {
     snake[0].following_direction = 2;
     //y -= 1;
     //Serial.println("up");
   }


   else if(y_axis >= 600) //&& y < 7)
   {
     snake[0].following_direction = 3;
     //y += 1;
     //Serial.println("down");
   }





  if(snake[0].following_direction == 0 && snake[0].x < 7)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x += 1;
  }

  else if(snake[0].following_direction == 1 && snake[0].x > 0)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x -= 1;
  }

  else if(snake[0].following_direction == 2 && snake[0].y > 0)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y -= 1;
  }

  else if(snake[0].following_direction == 3 && snake[0].y < 7)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y += 1;
  }


  for(int a = 1; a < 64; a++)
  {
    if(snake[i] != 0)
    {

      snake[i].x = snake[i-1].previous_x;
      snake[i].y = snake[i-1].previous_y;    
    }
  }


  lc.setLed(0,power_up->x,power_up->y,true);
  for(int w = 0; w < 64; w++)
  {
    if(snake[w] != 0)
    {
      lc.setLed(0,snake[w].x,snake[w].y,true);  
    }
  }

  if(snake[0].x == power_up->x && snake[0].y == power_up->y)
  {
    power_up->x = random(0, 8);
    power_up->y = random(0, 8);


    for(int i = 0; i < 64; i++)
    {
      if(snake[i] == 0)
      {
         Snake_body *snake[i];

         //next_snake_body = new Snake_body();
         snake[i] = new Snake_body(snake[i-1].previous_x, snake[i-1].previous_y);
         //Snake_body next_snake_body;
         //snake[i].configure(snake[i-1].previous_x, snake[i-1].previous_y);
      }
    }
  }
  /*Serial.print("x: ");
  Serial.print(snake.x);
  Serial.println();

  Serial.print("y: ");
  Serial.print(snake.y);
  Serial.println();*/

  if(switcher == 0)
  {
    delay(50); 
  }
  else
  {
    delay(100); 
  }
  lc.clearDisplay(0);
}

the error is coming from this line here:

else if(snake[0].following_direction == 3 && snake[0].y < 7)

but I think it is also happening at all other lines of which are similar to this one.

I have tried replacing the . in snake[0].following_direction with a -> instead but that just caused the error: base operand of '-> ' is not a pointer. I have experience programming in java python and a few others but not arduino C++.

1

The problem is indeed in that line:

else if(snake[0].following_direction == 3 && snake[0].y < 7)

And specifically in:

snake[0].following_direction

snake is defined as

int snake[64]; 

Thus snake[0] is of type int. And you cannot request the member following_direction from type. Since you make new instances with:

snake[0] = new Snake_body(0,0)

I assume snake should be an array of Snake_body.

So I suggest (at least) change the type to:

Snake_body snake[64]; 

Just as a side note, I think one instance of Snake_body can take up like 16 bytes, and you make 64 of them, this results in 1,024 bytes which is half the memory of an Arduino Uno. Also, sometimes a library takes up some space, so keep this in mind if you are intending to add more libraries/global variables or stack space.

Update

Also the following line seems wrong:

snake[0] = new Snake_body(0,0);

You already created 64 instances of Snake_body (and assigned to snake) after you do my change. So you do not need to do this yourself; actually you cannot, unless you create an array of POINTERS to Snake_body.

10
  • 1
    That is now so obvious that I see that. This was a silly mistake I did not see. Also you are 100% right in thinking that snake is an array of snake_body. I probably did it because I am used to doing arrays of integers. You are such a legend! Cheers! – The_Indestructible_Cat May 25 '20 at 10:41
  • One problem though, when I have done the line Snake_body snake[64];, it thinks it is a function – The_Indestructible_Cat May 25 '20 at 10:42
  • Can you add the line and exact error you get (it thinks it is a function, that's not enough for me to see what isi wrong) – Michel Keijzers May 25 '20 at 10:44
  • 1
    I have already created a mechanism to see how many elements are in the list. That is where int snakes_length; comes in. – The_Indestructible_Cat May 25 '20 at 15:17
  • 2
    Problem is now solved but the program is still having some work done on it. Its up to the point of what my question is about. – The_Indestructible_Cat May 25 '20 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.