Rewrite the snake example in an object-oriented fashion with a Snake class. Can you make snakes with slightly different looks (different shapes, colors, sizes)?

Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 9-7: Rewrite the snake example in an object-oriented fashion with a Snake class. 
// Can you make snakes with slightly different looks (different shapes, colors, sizes)? 
// (For an advanced problem, create a Point class that stores an x and y coordinate as part of the sketch. 
// Each snake object will have an array of Point objects, instead of two separate arrays of x and y values. 
// This involves arrays of objects, covered in the next section.)

// Two snake objects (this would be better as an array!)
Snake s0;
Snake s1;

void setup() {
  size(400,200);
  smooth();
  // Initialize
  s0 = new Snake(50);
  s1 = new Snake(25);
}

void draw() {
  background(255);
  
  // Update and display
  s0.update(mouseX-30,mouseY);
  s0.display();

  // Update and display
  s1.update(mouseX+30,mouseY);
  s1.display();  
}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 9-7: Snake Class

class Snake {
  // x and y positions
  int[] xpos;
  int[] ypos;

  // The constructor determines the length of the snake
  Snake(int n) {
    xpos = new int[n];
    ypos = new int[n];
  }

  void update(int newX, int newY) {
    // Shift all elements down one spot. 
    // xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
    for (int i = 0; i < xpos.length-1; i ++ ) {
      xpos[i] = xpos[i+1]; 
      ypos[i] = ypos[i+1];
    }

    // Update the last spot in the array with the mouse location.
    xpos[xpos.length-1] = newX; 
    ypos[ypos.length-1] = newY;
  }

  void display() {
    // Draw everything
    for (int i = 0; i < xpos.length; i ++ ) {
      // Draw an ellipse for each element in the arrays. 
      // Color and size are tied to the loop's counter: i.
      stroke(0);
      fill(255-i*5);
      ellipse(xpos[i],ypos[i],i,i); 
    }

  }

}
  • marisaurus

    In the book, you have an addition to this exercise, concerning the use of a Pt class that would be called into the Snake class. Is it possible to post how to do this?

    thanks.

  • http://www.learningprocessing.com Daniel Shiffman

    First you need a simple Point class, which could look like so:

    class Point {
      float x;
      float y;
     
       Point(float x_, float y_) {
         x = x_;
         y = y_;
      }
    }
    

    Once you have this then your Snake class only needs one array of “Point” objects, rather than two separate x and y arrays.

     class Snake {
      // x and y positions
      Point[] points;
    
      // etc.
    
  • Johan Hess

    In the advanced problem how do you draw the ‘ellipses’ with the new array of Point objects?

    The ‘xpos’ and ‘ypos’ arrays can be directly used as arguments for the ellipse: ellipse(xpos[i],ypos[i],i,i),
    but with the Point object i cant see how this is possible, as ellipse obviously dosnt work with: ellipse(points[i],points[i],i,i).

    do you need to decipher the x and y value from the points object so that you are able to use them seperately within the ellipse object, and if so, how is that done?
    or is some larger change needed to make it work?

  • Johan Hess

    ok I have just read chapter 10, where i found the solution: ellipse(points[i].x, points[i].y,i,i)

    thank you regardless :)

  • Maggie

    could you please provide the full example?
    thanks!

  • Maggie

    Oh nevermind!  I just realized there’s two different tabs

  • donald clark

    conceptually i wonder why we would be adding i = n+1 where i starts from 0 moving to 49 .

    if we are shifting down would we not want to move from the new location of the mouse[49] to [0] therfore saying n= n-1