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