The Snake Game – Midterm

IDEA:

One of my favorite games I played back in my childhood was the snake game. I used to play it on the old Nokia phones which was a very basic version of it. I thought recreating the snake game would be a nice aim for the midterm. However, since I did not want to replicate it, but make my own version of it so, I got creative with it and added my own final touches on it to -in a way- make it my own.

^The form I tried replicating^

 

PROCESS:

I started with creating the main menu

 

and wrote an array list that will store the x and y values of the snake then, making the snake’s body that is generated through a for loop. Changed the frame count too, to make the snake faster after each 5 points.

 

In order to grow the snake’s body, I wrote a bunch of code that makes up the fruit of the snake and randomly generates it across the window. I also added a sound effect to play when the snake takes a bit from the food.

 

After that, a user would like to view their score instead of counting the squares that make up the snake’s body, so I added a text function that displays the score which is the size of the snake.

 

 

In order for this game to end, the snake must die. So, I created an if statement that checks of the snake’s position is on the window’s parameter. Then a for loop to check if the snake bit itself. Then an option to play again if the user wanted to.

 

 

WHY IT IS BETTER THAN THE ORIGINAL + FEATURES:

1)Different visuals and sounds

 

2)You can read your score

 

3)You can play over and over again

 

4)Night mode – in a way

 

5) snake speeds up every 5 points making it more challenging

 

DEMO:

 

 

CODE:

// main menu variables
String gameState;


// to make the snake's body
ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();

//image variable
PImage fruit;

//global variables
int w=30, h=30, blockSize=20, dir=2, foodX=15, foodY=15, speedNum = 8, fc = 255, life = 30, score;

//font variable
PFont f; 

//coordinates for x and y
int[] Xdir={0, 0, 1, -1};
int[] Ydir={1, -1, 0, 0}; 

//game condition 
boolean gameOver=false;

  
import processing.sound.*;
SoundFile foodSound;
SoundFile mainMenuSound;





void setup(){

  size(600, 600);
  gameState = "START";
  // font initializer
  f = createFont("Georgia", 35);
  //snake's starting position 
  x.add(0); y.add(15); 

  foodSound = new SoundFile(this, "0.aif");
  
  
  fruit = loadImage("apple.png");





}


void draw() {
  background(0);
  if (gameState == "START") {
    startGame();
  } else if (gameState == "PLAY") {
    playGame();
  } else if (gameState == "LOSE") {
    loseGame();
  }
}


void startGame(){
  
  textAlign(CENTER);
  textSize(18);
  textFont(f);
  fill(255);
  text("Click Anywhere to Start the Game!", width/2, height/2 -30);
  textSize(14);
  text("Use the Arrows to move\n Eat the fruits for a higher score \n Do NOT eat yourself or escape the walls of the game\n You have 1 life", width/2, height/2);
  //look for the click
  if (mousePressed == true) {
    gameState = "PLAY";
  }
}




void playGame(){
  
  
  
  
   //snake's body color
  fill(3, 252, 169); 
  
  //snake's body
  for (int i = 0; i < x.size(); i++){
    
    rect(x.get(i)*blockSize, y.get(i)*blockSize, blockSize, blockSize);
    }
    
    
    if (!gameOver) { 
    // food
    imageMode(CENTER);
    image(fruit, foodX*blockSize+10, foodY*blockSize+10, fruit.width*0.03, fruit.height*0.03);
    // score on player screen
    textAlign(LEFT); 
    textFont(f);
    textSize(25);
    fill(255);
    text("score : " + x.size(), 30, 30, width - 20, 50);
    
    //add more blocks to the snake
    if (frameCount%speedNum==0) { 
      x.add(0, x.get(0) + Xdir[dir]); 
      y.add(0, y.get(0) + Ydir[dir]);
    
    
      // snake die if it touches the window
      if (x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) {
        
        gameOver = true;
    }
    //  snake die if it touches itself
    for (int i=1; i<x.size(); i++) 
        if (x.get(0)==x.get(i)&&y.get(0)==y.get(i)) gameOver = true; 
        
        // eating food 
        if (x.get(0)==foodX && y.get(0)==foodY) { 
         if (x.size() %5==0 && speedNum>=2) speedNum-=1;  // speed increases every 5 points 
          foodSound.play();
         // generates new food position
        foodX = (int)random(0, w);
        foodY = (int)random(0, h);
       
 
      } else {
        // so that the snake size doesnt become infinite
        x.remove(x.size()-1); 
        y.remove(y.size()-1);
      }
    }
  }
  
  
  else {
   
    loseGame();
  }
 

}


void loseGame(){

  // game is over screen
    fill(255); 
    textSize(30); 
    textFont(f);
    textAlign(CENTER); 
    text("GAME OVER \n Your Score is: "+ x.size() +"\n Press ENTER", width/2, height/3+30);
    
    keyPressed();
  
}


void keyPressed() { 
  int newDir=keyCode == DOWN? 0:(keyCode == UP?1:(keyCode == RIGHT?2:(keyCode == LEFT?3:-1)));
  if (newDir != -1) dir = newDir;
  
  if (keyCode == ENTER && gameOver) { 
    x.clear(); 
    y.clear(); 
    x.add(0);  
    y.add(15);
    dir = 2;
    speedNum = 8;
    gameOver = false;
  }
}

The link to download the zip folder with the code:

https://github.com/ShaikhaAlshehhi/Midterm-SnakeGameFinal

CONCLUSION:

I really had fun experimenting and testing this project. It did add to my coding career and inspired me. As a next step for this project, I would like to implement a life system where the snake would get injured instead of dying and could heal from it.

Leave a Reply