# Snake game: start screen and two players

**URL:** https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962
**Category:** Coding Questions
**Tags:** homework
**Created:** [December 1, 2022, 9:11am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962 "2022-12-01T09:11:42Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 1, 2022, 9:11am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/1 "2022-12-01T09:11:42Z")

</div>

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!!😭😭😭

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 1, 2022, 10:16am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/2 "2022-12-01T10:16:49Z")

</div>

> [@ALAL](#):
>
> how can I make a home start

you can make a variable `int screen=0;`

- 0 means show Start Screen
- 1 means Game

* * *

**so in draw() say :**

```auto
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:**

```auto
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;**

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

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

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 1, 2022, 4:19pm UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/3 "2022-12-01T16:19:12Z")

</div>

> [@ALAL](#):
>
> another snake cause he need two players in the game.

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

---

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 2, 2022, 8:50am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/4 "2022-12-02T08:50:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 2, 2022, 11:32am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/5 "2022-12-02T11:32:30Z")

</div>

> [@ALAL](#):
>
> how can I over the game.

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

```auto
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

---

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 6, 2022, 9:51am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/6 "2022-12-06T09:51:37Z")

</div>

```auto
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); 
    }
}

```

---

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 6, 2022, 9:53am UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/7 "2022-12-06T09:53:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 6, 2022, 12:11pm UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/8 "2022-12-06T12:11:11Z")

</div>

unfortunately I don’t have time

> [@ALAL](#):
>
> the player get 5 scors how can I do it

make a global int variable: `int score = 0;`

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

please format your code

---

<div class="post-metadata">

### Author: ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)
#### Post date: [December 6, 2022, 2:07pm UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/9 "2022-12-06T14:07:11Z")

</div>

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](https://processing.org/tutorials/) and [examples](https://processing.org/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:

[![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/34521338de6902479873291389bf53a11d12237d.gif) ](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/34521338de6902479873291389bf53a11d12237d.gif)

_Note: part of this message was generated with ChatGPT_

---

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 7, 2022, 1:05pm UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/10 "2022-12-07T13:05:28Z")

</div>

oh thank u this is my first time hier

---

<div class="post-metadata">

### Author: ![ALAL](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@ALAL](https://discourse.processing.org/u/ALAL)
#### Post date: [December 7, 2022, 1:06pm UTC](https://discourse.processing.org/t/snake-game-start-screen-and-two-players/39962/11 "2022-12-07T13:06:38Z")

</div>

```auto
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); 
    }
}

```

```auto
type or paste code here

```
