# Snake Game Code

**URL:** <https://discourse.processing.org/t/snake-game-code/1725>\
**Category:** Coding Questions\
**Created:** [July 12, 2018, 7:20pm UTC](https://discourse.processing.org/t/snake-game-code/1725 "2018-07-12T19:20:53Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 3, 2020, 3:08am UTC](https://discourse.processing.org/t/snake-game-code/1725/23 "2020-04-03T03:08:41Z")

</div>

You’re two years too late. Oh well.

Also, that is not too much code, but it’s not exactly understandable.

Consider this snake code I once wrote:

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

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [June 9, 2020, 9:29pm UTC](https://discourse.processing.org/t/snake-game-code/1725/24 "2020-06-09T21:29:14Z")

</div>

Hey look! nice polished snake game! 😀  
but not quite understandable code, but i liked 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 9, 2022, 1:13am UTC](https://discourse.processing.org/t/snake-game-code/1725/25 "2022-12-09T01:13:30Z")

</div>

do you have a splash screen for your cods and a game over screen??

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 9, 2022, 3:39am UTC](https://discourse.processing.org/t/snake-game-code/1725/26 "2022-12-09T03:39:21Z")

</div>

No, but you can certainly add one!

---

<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 9, 2022, 11:14am UTC](https://discourse.processing.org/t/snake-game-code/1725/27 "2022-12-09T11:14:35Z")

</div>

class Score {  
int score;

Score() {  
score = 0;  
}

void increase() {  
score ++;  
}

void show() {  
fill(255);  
textAlign(RIGHT);  
textSize(30);  
text("Score: " + String.valueOf(score), width-5, 15);  
}  
}`Preformatted text`

---

<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 9, 2022, 11:15am UTC](https://discourse.processing.org/t/snake-game-code/1725/28 "2022-12-09T11:15:14Z")

</div>

how can I make my score from +1 To +5

[Previous page](https://discourse.processing.org/t/snake-game-code/1725.md?page=1)
