Uhm ok, i‘ll get on my PC and try to adjust THe code a bit, because i just can‘t imagine that Snake can only run with 4 frames ( a Game that is older than the Internet). Should take around 30 min, Max an Hour if i cant find anything. I‘ll Update this Reply once i got it^^
First of all, i made some changes to your code to make it easier to read and use, so here it is:
final int GridSize = 50; // sets the size of the Grid (50*50)
final int size = 15; // sets the size of the snake and the grid (you could also
//make it into a PVector to have the size of each grid be width 10 height 20 for example and same for GridSize
final color RED = color(255, 0, 0);
final color GREEN = color(0, 255, 0);
final color BLUE = color(0, 0, 255);
//i removed the xD from Snake so just let both start by going upwards. Should save a lot of trouble
Snake snake1 = new Snake(195, 345, RED);
Snake snake2 = new Snake(555, 345, BLUE);
boolean[][] occupied = new boolean[50][50];
void setup() {
size(750, 750);
background(0);
createGrid();
frameRate(4);
}
void draw() {
snake1.drawSnake();
snake2.drawSnake();
snake1.moveSnakeForward();
snake2.moveSnakeForward();
if (snake1.testCollision()) {
win(2);
}
if (snake2.testCollision()) {
win(1);
}
occupied[(int)snake1.pos.x/size][(int)snake1.pos.y/size] = true;
occupied[(int)snake2.pos.x/size][(int)snake2.pos.y/size] = true;
}
void win(int i) {
createGrid();
fill(0);
noStroke();
rect(200, 350, 352, 60);
fill(i == 1 ? RED : i == 2 ? BLUE : 0);
textSize(48);
textAlign(CENTER, CENTER);
text("Snake " + i + " win", width/2, height/2);
if (i == 1) {
snake1.dir = new PVector(0, 0);
} else if (i == 2) {
snake2.dir = new PVector(0, 0);
}
noLoop();
}
void keyPressed() {
if (key == 'z') {
snake1.turnUp();
}
if (key == 's') {
snake1.turnDown();
}
if (key == 'q') {
snake1.turnLeft();
}
if (key == 'd') {
snake1.turnRight();
}
if (keyCode == UP) {
snake2.turnUp();
}
if (keyCode == DOWN) {
snake2.turnDown();
}
if (keyCode == LEFT) {
snake2.turnLeft();
}
if (keyCode == RIGHT) {
snake2.turnRight();
}
}
void temporisation() {
try {
Thread.sleep(30);
}
catch (Exception e) {
}
}
void createGrid() {
for (int x = 0; x < GridSize; x++) { // if you changed GridSize to PVector, just use GridSize.x
for (int y = 0; y < GridSize; y++) {//and here GridSize.y
stroke(255);
fill(0);
rect(x*size, y*size, size, size);
}
}
}
class Snake {
PVector pos;
PVector dir;
color c;
Snake(int x, int y, color col) {
pos = new PVector(x, y);
dir = new PVector(0, -size);
c = col;
}
void drawSnake() {
noStroke();
fill(c);
rect(pos.x, pos.y, size, size);
}
void moveSnakeForward() {
pos = new PVector(pos.x + dir.x, pos.y + dir.y);
temporisation();
}
void turnUp() {
dir = new PVector(0,-size);
}
void turnDown() {
dir = new PVector(0, size);
}
void turnLeft() {
dir = new PVector(-size, 0);
}
void turnRight() {
dir = new PVector(size, 0);
}
boolean testCollision() {
if (pos.x <= 0 || pos.x > 750 || pos.y <= 0 || pos.y > 750) {
return true;
}
if (occupied[(int)pos.x/size][(int)pos.y/size]) {
return true;
}
return false;
}
}
I’ll also send you one version where the movement is per pixel, so that it looks better :
final int GridSize = 50; // sets the size of the Grid (50*50)
final int size = 15; // sets the size of the snake and the grid (you could also
//make it into a PVector to have the size of each grid be width 10 height 20 for example and same for GridSize
final color RED = color(255, 0, 0);
final color GREEN = color(0, 255, 0);
final color BLUE = color(0, 0, 255);
//i removed the xD from Snake so just let both start by going upwards. Should save a lot of trouble
Snake snake1 = new Snake(195, 345, RED);
Snake snake2 = new Snake(555, 345, BLUE);
boolean[][] occupied = new boolean[50][50];
void setup() {
size(750, 750);
background(0);
createGrid();
frameRate(60);
}
void draw() {
snake1.drawSnake();
snake2.drawSnake();
snake1.moveSnakeForward();
snake2.moveSnakeForward();
if (frameCount % 15 == 0) {
snake1.updateNextDir();
snake2.updateNextDir();
if (snake1.testCollision()) {
win(2);
}
if (snake2.testCollision()) {
win(1);
}
occupied[(int)snake1.pos.x/size][(int)snake1.pos.y/size] = true;
occupied[(int)snake2.pos.x/size][(int)snake2.pos.y/size] = true;
}
}
void win(int i) {
createGrid();
fill(0);
noStroke();
rect(200, 350, 352, 60);
fill(i == 1 ? RED : i == 2 ? BLUE : 0);
textSize(48);
textAlign(CENTER, CENTER);
text("Snake " + i + " win", width/2, height/2);
if (i == 1) {
snake1.dir = new PVector(0, 0);
} else if (i == 2) {
snake2.dir = new PVector(0, 0);
}
noLoop();
}
void keyPressed() {
if (key == 'z') {
snake1.turnUp();
}
if (key == 's') {
snake1.turnDown();
}
if (key == 'q') {
snake1.turnLeft();
}
if (key == 'd') {
snake1.turnRight();
}
if (keyCode == UP) {
snake2.turnUp();
}
if (keyCode == DOWN) {
snake2.turnDown();
}
if (keyCode == LEFT) {
snake2.turnLeft();
}
if (keyCode == RIGHT) {
snake2.turnRight();
}
}
void temporisation() {
try {
Thread.sleep(30);
}
catch (Exception e) {
}
}
void createGrid() {
for (int x = 0; x < GridSize; x++) { // if you changed GridSize to PVector, just use GridSize.x
for (int y = 0; y < GridSize; y++) {//and here GridSize.y
stroke(255);
fill(0);
rect(x*size, y*size, size, size);
}
}
}
class Snake {
PVector pos;
PVector dir;
PVector nextDir;
color c;
Snake(int x, int y, color col) {
pos = new PVector(x, y);
dir = new PVector(0, -1);
c = col;
}
void drawSnake() {
noStroke();
fill(c);
rect(pos.x, pos.y, size, size);
}
void moveSnakeForward() {
pos = new PVector(pos.x + dir.x, pos.y + dir.y);
temporisation();
}
void turnUp() {
if (frameCount % 15 == 0) {
dir = new PVector(0, -1);
} else {
nextDir = new PVector(0, -1);
}
}
void turnDown() {
if (frameCount % 15 == 0) {
dir = new PVector(0, 1);
} else {
nextDir = new PVector(0, 1);
}
}
void turnLeft() {
if (frameCount % 15 == 0) {
dir = new PVector(-1, 0);
} else {
nextDir = new PVector(-1, 0);
}
}
void turnRight() {
if (frameCount % 15 == 0) {
dir = new PVector(1, 0);
} else {
nextDir = new PVector(1, 0);
}
}
void updateNextDir() {
if (nextDir != null) {
dir = nextDir;
nextDir = null;
}
}
boolean testCollision() {
if (pos.x <= 0 || pos.x > 750 || pos.y <= 0 || pos.y > 750) {
return true;
}
if (occupied[(int)pos.x/size][(int)pos.y/size]) {
return true;
}
return false;
}
}
The changes are in the new nextDir PVector, the turnUp/Down and so on methods, the new updateNextDir() method changing where size was before into 1 and the frameRate(60) and frameCount % 15 == 0 addition. Took a bit longer than expected
Btw, if you cant find the differences, just use an online difference checker. Just search for “find difference between two texts online”. Used it to find what i changed
Hope it helps