Snake game: start screen and two players

Heyy!!
this is my first time and I’m a beginner. I have questions about sank game in processing. I have an exam next week so I hoop to found someone to help me.
my question is how can I make a home start en a another snake cause he need two players in the game.
I needed with a simple codes on processing.
I really need a help!!:sob::sob::sob:

1 Like

you can make a variable int screen=0;

  • 0 means show Start Screen
  • 1 means Game

so in draw() say :

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
}

Nothing outside this if-clause is allowed in draw() !


And the function:

void drawForStateSplashScreen() {

  // Splash Screen as a yellow box 

  background(0);

  final int widthBox=600; 
  final float heightY=263;

  fill(246,255,55);  // yellow 
  stroke(0); 
  rect(width/2-widthBox/2, 110, 
    widthBox, heightY); 
  noFill(); 
  int innerFrameDistance=3; 
  rect(width/2-widthBox/2 +innerFrameDistance, 110+innerFrameDistance, 
    widthBox    -2*innerFrameDistance, heightY-2*innerFrameDistance);

  fill(0);
  textSize(60); 
  text("Snake Game \nTwo players", 
    width/2-widthBox/2 + 9, 270);
  textSize(30);
  // text("Text Adventure ",   width/2-widthBox/2 + 39, 333);

  // reset
  fill(255);
  textSize(21); 

  text(" Text  ", //  "Good Luck", 
    width/2-widthBox/2, 633);

  textSize(11);
}//func

on mousepressed and keypressed say screen = 1;

void keyPressed() {
     screen = 1; 
     ......
}

void mousePressed() {
     screen = 1; 
     ....
}

OKAY.

When you have one snake, you need to duplicate the variables and behavior.

So when you have int x,y; you need int x2,y2; now as well.

When you can make the functions that you have (e.g. checkApple(), checkAgainstSelf()) in a way that you can call them with a parameter like checkApple(snakeArray1); checkApple(snakeArray2); from draw(); then you don’t have to duplicate the code but just call the function twice. Nice.

You can also have an array of the snakes (which is too complicate for now but would be interesting if you had 4 snakes).

You need to have a collision check between the two snakes as well.

Show your code to get better answers.

Warm regards,

Chrisir

Hiiii, thank for your help.
and how can I over the game. Also in the snake game he want 2 players and two food (apple and Banana).
If the snake dies, it must be struck by another snake or by a stone. I want ti show the scores two.
so its very difficult for me and I don’t have enough time would you like to help me

1 Like

This is just a further value for your variable screen. So when the snake dies, say screen = 1;

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
    else if(screen==2) {
       // show a game over screen
       background(0);
       text("Game over", 111,111); 
    }
}

As I said, post your code and improve it

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
}

void draw() {

  // Splash Screen as a yellow box 

  background(0);

  final int widthBox=600; 
  final float heightY=263;

  fill(246,255,55);  // yellow 
  stroke(0); 
  rect(width/2-widthBox/2, 110, 
    widthBox, heightY); 
  noFill(); 
  int innerFrameDistance=3; 
  rect(width/2-widthBox/2 +innerFrameDistance, 110+innerFrameDistance, 
    widthBox    -2*innerFrameDistance, heightY-2*innerFrameDistance);

  fill(0);
  textSize(60); 
  text("Snake Game \nTwo players", 
    width/2-widthBox/2 + 9, 270);
  textSize(30);
  // text("Text Adventure ",   width/2-widthBox/2 + 39, 333);

  // reset
  fill(255);
  textSize(21); 

  text(" Text  ", //  "Good Luck", 
    width/2-widthBox/2, 633);

  textSize(11);
   
}//func


// Useful arrays to decode/use direction.
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
int[] dk = { RIGHT, DOWN, LEFT, UP };

class Snake {
  int x, y; // Current head position.
  ArrayList<PVector> tail; // Other segments.
  int d; // Direction of movement;
  boolean isGrowing = false;
  Snake() {
    d = 0;
    tail = new ArrayList();
  }
  void draw() {
    fill(50,200,50);
    stroke(150,200,50);
    rect(x,y,20,20);
    for( int i = 0; i < tail.size(); i++){
      rect(tail.get(i).x,tail.get(i).y,20,20);
    }
  }
  void update() {
    tail.add( new PVector(x,y,d) );
    if( isGrowing ){
      isGrowing = false;
    } else {
      tail.remove(0);
    }
    x += 20*dx[d];
    y += 20*dy[d];
    x += width;
    y += height;
    x %= width;
    y %= height;
  }
  void grow() {
    isGrowing = true;
  }
  void onKeyPressed() {
    for( int i = 0; i < dk.length; i++){
      if( key == CODED && keyCode == dk[i] ){
        d = i;
      }
    }
  }
}

class Food {
  int x, y;
  Food() {
    newPosition();
  }
  void draw() {
    fill(255,0,0);
    stroke(200,0,200);
    ellipse(x+10,y+10,20,20);
  }
  boolean wasEaten(int ox, int oy) {
    if ( x == ox && y == oy ) {
      newPosition();
      return true;
    }
    return false;
  }
  void newPosition() {
    x = 20 * int(random(width/20));
    y = 20 * int(random(height/20));
  }
}

class Timer {
  int time;
  int duration;
  Timer() {
    duration = 100;
    reset();
  }
  void reset() {
    time = millis() + duration;
  }
  boolean alarm() {
    if ( millis() > time ) {
      time = millis() + duration;
      return true;
    }
    return false;
  }
}

// --- Main driving code follows. Notice how simple its logic is to understand!

Snake snake;
Food food;
Timer timer;

void setup() {
  size(600, 400);
  snake = new Snake();
  food = new Food();
  timer = new Timer();
}

void draw() {
  background(0);
  if ( timer.alarm() ) {
    snake.update();
    if ( food.wasEaten( snake.x, snake.y ) ) {
      snake.grow();
    }
  }
  food.draw();
  snake.draw();
}

void keyPressed() {
  snake.onKeyPressed();
}
    
  void drawForStateSplashScreen(){

  // Splash Screen as a yellow box 

  background(0);

  final int widthBox=600; 
  final float heightY=263;

  fill(246,255,55);  // yellow 
  stroke(0); 
  rect(width/2-widthBox/2, 110, 
    widthBox, heightY); 
  noFill(); 
  int innerFrameDistance=3; 
  rect(width/2-widthBox/2 +innerFrameDistance, 110+innerFrameDistance, 
    widthBox    -2*innerFrameDistance, heightY-2*innerFrameDistance);

  fill(0);
  textSize(60); 
  text("Snake Game \nTwo players", 
    width/2-widthBox/2 + 9, 270);
  textSize(30);
  // text("Text Adventure ",   width/2-widthBox/2 + 39, 333);

  // reset
  fill(255);
  textSize(21); 

  text(" Text  ", //  "Good Luck", 
    width/2-widthBox/2, 633);

  textSize(11);
}//func

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
    else if(screen==2) {
       // show a game over screen
       background(0);
       text("Game over", 111,111); 
    }
}

its not working and he want to see if the snake eat a cherry the player get 5 scors how can I do it

1 Like

unfortunately I don’t have time

make a global int variable: int score = 0;

once the snake hits a cherry say score = score + 5;

please format your code

One thing to begin: the draw() function is defined multiple times in the code, which is not allowed in Processing. You should only define it once, and then use conditional statements to control which screen (start or game) is drawn each frame.

Now, it appears that you are trying to implement a complex program without fully understanding the fundamentals of the Processing language. In order to create working programs in Processing, it is important to have a solid foundation in the basics of the language, such as variables, data types, control structures, functions, and other core concepts. Without this foundation, it can be difficult to understand and troubleshoot your code when things don’t work as expected.

I encourage you to take some time to go back and learn the fundamentals of Processing. The Processing website has a great collection of tutorials and examples that can help you get started. You can also try working through some simple projects and challenges to practice your skills and build your confidence. As you learn the basics, you will be better equipped to tackle more complex programs and ideas.

Remember that learning to code is a process, and it can take time and persistence to develop your skills. Don’t be discouraged if you encounter challenges or setbacks along the way. Just keep practicing, experimenting, and asking questions, and you will eventually be able to create the programs that you want to make.

PS: I formatted your code. In the future please use code formatting to make it easier for people to help. See the example below:

Note: part of this message was generated with ChatGPT

3 Likes

oh thank u this is my first time hier

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
}

void draw() {

  // Splash Screen as a yellow box 

  background(0);

  final int widthBox=600; 
  final float heightY=263;

  fill(246,255,55);  // yellow 
  stroke(0); 
  rect(width/2-widthBox/2, 110, 
    widthBox, heightY); 
  noFill(); 
  int innerFrameDistance=3; 
  rect(width/2-widthBox/2 +innerFrameDistance, 110+innerFrameDistance, 
    widthBox    -2*innerFrameDistance, heightY-2*innerFrameDistance);

  fill(0);
  textSize(60); 
  text("Snake Game \nTwo players", 
    width/2-widthBox/2 + 9, 270);
  textSize(30);
  // text("Text Adventure ",   width/2-widthBox/2 + 39, 333);

  // reset
  fill(255);
  textSize(21); 

  text(" Text  ", //  "Good Luck", 
    width/2-widthBox/2, 633);

  textSize(11);
   
}//func


// Useful arrays to decode/use direction.
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
int[] dk = { RIGHT, DOWN, LEFT, UP };

class Snake {
  int x, y; // Current head position.
  ArrayList<PVector> tail; // Other segments.
  int d; // Direction of movement;
  boolean isGrowing = false;
  Snake() {
    d = 0;
    tail = new ArrayList();
  }
  void draw() {
    fill(50,200,50);
    stroke(150,200,50);
    rect(x,y,20,20);
    for( int i = 0; i < tail.size(); i++){
      rect(tail.get(i).x,tail.get(i).y,20,20);
    }
  }
  void update() {
    tail.add( new PVector(x,y,d) );
    if( isGrowing ){
      isGrowing = false;
    } else {
      tail.remove(0);
    }
    x += 20*dx[d];
    y += 20*dy[d];
    x += width;
    y += height;
    x %= width;
    y %= height;
  }
  void grow() {
    isGrowing = true;
  }
  void onKeyPressed() {
    for( int i = 0; i < dk.length; i++){
      if( key == CODED && keyCode == dk[i] ){
        d = I;
      }
    }
  }
}

class Food {
  int x, y;
  Food() {
    newPosition();
  }
  void draw() {
    fill(255,0,0);
    stroke(200,0,200);
    ellipse(x+10,y+10,20,20);
  }
  boolean wasEaten(int ox, int oy) {
    if ( x == ox && y == oy ) {
      newPosition();
      return true;
    }
    return false;
  }
  void newPosition() {
    x = 20 * int(random(width/20));
    y = 20 * int(random(height/20));
  }
}

class Timer {
  int time;
  int duration;
  Timer() {
    duration = 100;
    reset();
  }
  void reset() {
    time = millis() + duration;
  }
  boolean alarm() {
    if ( millis() > time ) {
      time = millis() + duration;
      return true;
    }
    return false;
  }
}

// --- Main driving code follows. Notice how simple its logic is to understand!

Snake snake;
Food food;
Timer timer;

void setup() {
  size(600, 400);
  snake = new Snake();
  food = new Food();
  timer = new Timer();
}

void draw() {
  background(0);
  if ( timer.alarm() ) {
    snake.update();
    if ( food.wasEaten( snake.x, snake.y ) ) {
      snake.grow();
    }
  }
  food.draw();
  snake.draw();
}

void keyPressed() {
  snake.onKeyPressed();
}
    
  void drawForStateSplashScreen(){

  // Splash Screen as a yellow box 

  background(0);

  final int widthBox=600; 
  final float heightY=263;

  fill(246,255,55);  // yellow 
  stroke(0); 
  rect(width/2-widthBox/2, 110, 
    widthBox, heightY); 
  noFill(); 
  int innerFrameDistance=3; 
  rect(width/2-widthBox/2 +innerFrameDistance, 110+innerFrameDistance, 
    widthBox    -2*innerFrameDistance, heightY-2*innerFrameDistance);

  fill(0);
  textSize(60); 
  text("Snake Game \nTwo players", 
    width/2-widthBox/2 + 9, 270);
  textSize(30);
  // text("Text Adventure ",   width/2-widthBox/2 + 39, 333);

  // reset
  fill(255);
  textSize(21); 

  text(" Text  ", //  "Good Luck", 
    width/2-widthBox/2, 633);

  textSize(11);
}//func

void draw() {
    if(screen==0) {
       // show start screen 
      drawForStateSplashScreen();
    }
    else if(screen==1) {
       // Game, what you have in draw() now 
    }
    else if(screen==2) {
       // show a game over screen
       background(0);
       text("Game over", 111,111); 
    }
}
type or paste code here