Snake Game Midterm Progress

IDEA:

 

One of my favorite games I played back in my childhood was the snake game. I thought recreating the snake game would be a nice aim for the midterm. However, since I do not want to replicate it but make my own version of it, I am going to get creative with it. I am still not set with the creative part; however, I am thinking of shifting to a new mode after reaching a certain amount of points.

 

 

 

For now, I have almost made the classic snake game. After I figure out all the features I would like to add for my new mode, I will add it as an if statement when the score reaches a certain amount.

 

 

https://youtu.be/v1hmxYABemE

 

Code:

ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();

int w=30, h=30, blocks=20, direction=2, foodX=15, foodY=15, speed = 8, fc1 = 255, fc2 = 255, fc3 = 255; PFont f; 

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


boolean gameover=false;

void setup() { 
  size(600, 600); 
  f = createFont("Georgia", 35);
  x.add(0); y.add(15); //snake starting position 
}   

void draw() {  
  background(0);
  fill(3, 252, 169); //snakes color
  for (int i = 0; i < x.size(); i++) rect(x.get(i)*blocks, y.get(i)*blocks, blocks, blocks); 
  if (!gameover) {  
    fill(fc1, fc2, fc3); //food color red
    ellipse(foodX*blocks+10, foodY*blocks+10, blocks, blocks); //food
    textAlign(LEFT); //score
    textFont(f);
    textSize(25);
    fill(255);
    text("score : " + x.size(), 30, 30, width - 20, 50);
    //makes the snake longer
    if (frameCount%speed==0) { 
      x.add(0, x.get(0) + Xdirection[direction]); 
      y.add(0, y.get(0) + Ydirection[direction]);
      
      if (x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) gameover = true; 
      for (int i=1; i<x.size(); i++) 
        if (x.get(0)==x.get(i)&&y.get(0)==y.get(i)) gameover=true; 
      if (x.get(0)==foodX && y.get(0)==foodY) { //new food
         if (x.size() %5==0 && speed>=2) speed-=1;  // every 5 points speed increases
        foodX = (int)random(0, w); //new food
        foodY = (int)random(0, h);
 
      } else { 
        x.remove(x.size()-1); 
        y.remove(y.size()-1);
      }
    }
  } else {
    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);
    if (keyCode == ENTER) { 
      x.clear(); 
      y.clear(); 
      x.add(0);  
      y.add(15);
      direction = 2;
      speed = 8;
      gameover = false;
    }
  }
}
void keyPressed() { 
  int newdir=keyCode == DOWN? 0:(keyCode == UP?1:(keyCode == RIGHT?2:(keyCode == LEFT?3:-1)));
  if (newdir != -1) direction = newdir;
}

 

 

 

 

Leave a Reply