Need help adding two player to our snake project

me and my friend have a assignment to edit a code to make it different in our own way. we decided to edit a code made by the coding train to add two player.

the idea is that two snakes fight it out by trying to get the other snake go into it’s tail. we have one snake already (thanks to the coding train) but we can’t find out how to make a second snake that can interact with the same things as the first one and also that they can interact with each other.

main code:

// Code by: https://youtu.be/AaGK-fj-BAM

`Preformatted text`Snake s;
int scl = 20;

PVector food;

void setup() {
  size(600, 600);
  s = new Snake();
  frameRate(8);
  pickLocation();
}

void pickLocation() {
  int cols = width/scl;
  int rows = height/scl;
  food = new PVector(floor(random(cols)), floor(random(rows)));
  food.mult(scl);
}

void mousePressed() {
  s.total++;
}

void draw() {
  background(51);

  if (s.eat(food)) {
    pickLocation();
  }
  s.death();
  s.update();
  s.show();


  fill(255, 0, 100);
  rect(food.x, food.y, scl, scl);
}





void keyPressed() {
  if (keyCode == UP) {
    s.dir(0, -1);
  } else if (keyCode == DOWN) {
    s.dir(0, 1);
  } else if (keyCode == RIGHT) {
    s.dir(1, 0);
  } else if (keyCode == LEFT) {
    s.dir(-1, 0);
  }
}

snake class:

class Snake

{
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  ArrayList<PVector> tail = new ArrayList<PVector>();

  Snake() {
  }

  boolean eat(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      total++;
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i < tail.size(); i++) {
      PVector pos = tail.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        println("starting over");
        total = 0;
        tail.clear();
      }
    }
  }

  void update() {
    //println(total + " " + tail.size());
    if (total > 0) {
      if (total == tail.size() && !tail.isEmpty()) {
        tail.remove(0);
      }
      tail.add(new PVector(x, y));
    }

    x = x + xspeed*scl;
    y = y + yspeed*scl;

    x = constrain(x, 0, width-scl);
    y = constrain(y, 0, height-scl);
  }

  void show() {
    fill(255);
    for (PVector v : tail) {
      rect(v.x, v.y, scl, scl);
    }
    rect(x, y, scl, scl);
  }
}

thanks

Can a snake collide with itself?

Welcome to the forum! First please use </> button to format the code. Also add homework tag to the topic category if it’s an assignment.

The code seems straight from coding train as you mentioned. According to the homework policy of the forum we can’t give you a complete answer especially given that you are simply sharing the assignment. What do you think is the steps to have the second snake? If you have no idea what to start with, did your teacher give any hint on how to begin with?

1 Like

yes the snake can collide with itself

sorry i am new to the forum and forgot to do those things, fixed it now.

as someone who is extremely new to processing and text programming in general my first guess might be just replicating the code that makes the snake a second time and change a few things like color, where the snake spawns and controls maybe?

If you are extremely new to programming, this task may be a bit too ambitious to begin with because you need to be aware of several components like class and scope, which may get confusing and frustrating.

You mentioned changing colors or where it’s spawned - this may be a good starting point to get familiarized with code. Do you think you can manage to change these parameters? (forget about the second snake for now)