Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////////////////////////////////////////////////////
/////               SNEK                     //////
/////  Snek is the superior version of snake.  //////
/////  (beacuse I couldent get snake to work)  //////
/////   An Interactive Toy by Michael Arcadi   //////
/////////////////////////////////////////////////////


float playerX;//player positions
float playerY;

float playerSize=20;//standard player size

boolean goLeft=false;//moevment bools
boolean goRight=false;
boolean goUp=false;
boolean goDown=false;

int foodPosRx = int(random(0, 20));//this line and the one below it randomize the food X coordinate
int foodPosX = foodPosRx * 20;
int foodPosPx = foodPosRx;// previous position of food X coordinate, used to make sure that it dosent spawn in the same place twice

int foodPosRy = int(random(0, 20)); //this line and the one below it randomize the food Y coordinate
int foodPosY = foodPosRx * 20; 
int foodPosPy = foodPosRy; //previous position of food Y coondinate

int scan = -20;//for drawing the scan lines ane making them move
int scanFill= 0;

boolean about = false;//menu variables
boolean play = false;
boolean back = false;

color snek = color(86, 204, 0);//snek colour, for making it flash

//PFont digi;//I tried using a custom font. It didnt work. 

void setup() {
  size(400, 400);
  frameRate(20); //gotta go slow
  playerX=width/2;
  playerY=height/2;
  //I think it didnt work beacsue processing 2 has a limited selection of file formats that it can read fonts from and the font I was trying to use wasn't the right format. Or something. 
  //digi = createFont("AlphaSmart3000", 32);
}


void draw() {
  background(28, 66, 0);
  noStroke();

  //boot up menu
  if (about == false) {

    //clear();
    background(28, 66, 0);
    scanLines();
    dampen();
    title();
  } 
  //opens the about screen
  if (about == true) {
    //clear();
    background(28, 66, 0);
    scanLines();
    dampen();
    callAbout();
  } 
  //starts the game
  if (play ==  true) {
    //clear();
    background(28, 66, 0);
    snake();
    food();
    scanLines();
    dampen();
  }
}


void keyPressed() {
  //key presses. pretty self explanatory
  if (key == 'w') {   
    goUp=true;
    goDown=false;
    goLeft=false;
    goRight=false;
  } else if (key == 'a') {
    goLeft=true;
    goRight = false;
    goUp = false;
    goDown = false;
  } else if (key == 's') {
    goDown = true;
    goUp = false;
    goLeft = false;
    goRight = false;
  } else if (key == 'd') {
    goRight=true;
    goUp = false;
    goDown = false;
    goLeft = false;
  }
}

void snake() {

  //draws the snek
  fill(snek);
  rect(playerX, playerY, playerSize, playerSize);

  //movement in 4 directions
  if (goLeft == true) {
    playerX = playerX - playerSize;
    //if player reaches edge of screen loop to other side
    if (playerX < 0) {
      playerX = 380;
    }
  } else if (goRight == true) {
    playerX = playerX + playerSize;
    //if player reaches edge of screen loop to other side
    if (playerX > 380) {
      playerX = 0;
    }
  } else if (goUp == true) {
    playerY = playerY - playerSize;
    //if player reaches edge of screen loop to other side
    if (playerY < 0) {
      playerY = 380;
    }
  } else if (goDown == true) {
    //if player reaches edge of screen loop to other side
    playerY = playerY + playerSize;
    if (playerY > 380) {
      playerY = 0;
    }
  }
}

void food() {
  //draws the food
  fill(86, 204, 0);  
  rect(foodPosX, foodPosY, 20, 20);

  //re-locates the food once the player hits it
  if (playerX == foodPosX && playerY == foodPosY) {
    foodPosRx = int(random(0, 20));
    foodPosX = foodPosRx * 20;
    foodPosRy = int(random(0, 20));
    foodPosY = foodPosRy * 20;   
    // if the food is in the same place as it was last time re-randomize its location
    if (foodPosRx == foodPosPx && foodPosRy == foodPosPy) {
      foodPosRx = int(random(0, 20));
      foodPosX = foodPosRx * 20;
      foodPosRy = int(random(0, 20));
      foodPosY = foodPosRy * 20;
    }
    foodPosPx = foodPosRx;
    foodPosPy = foodPosRy;
    //makes the snek flash when it gets the food
    snek = color(255, 255, 255);
    //the "flash of light" the snek emits whenever it gets food
    fill(255, 100);
    ellipseMode(CORNER);
    ellipse(playerX-10, playerY, 40, 40);
    ellipseMode(CENTER);
  } else {
    // keeps the snake its normal color
    snek = color(86, 204, 0);
  }
}

void scanLines() {
  stroke(129, 255, 33, 40); 
  strokeWeight(2);
  // fills the screen w/ scan lines
  while (scanFill < 450) {
    line(0, scan+scanFill, 400, scan+scanFill);
    scanFill = scanFill + 20;
  }
  scanFill = 0;
  //moves the scan lines down
  scan++;
  //reset the scanline position every time a line is moved off screen, giving the illusion of movement
  if (scan >= 20) {
    scan = -20;
  }
}

void dampen() {
  //I was working on this part seperately, and it dosent like it if the variables are outside the function so thats why they're here.
  int alpha = 0;
  int size = 360;
  int ring = 0;
  strokeWeight(10);
  //this is the thing that gives us that cool effect around the screen 
  while (alpha < 300) {
    noFill();
    stroke(0, alpha);
    ellipse(width/2, height/2, size, size*2);
    alpha = alpha + 3;
    size = size + 5;
    ring = ring + 10;
  }
}

void title() {

  noStroke();
  //draw the s
  fill(86, 204, 0);
  rect(65, 150, 15, 15);
  rect(80, 150, 15, 15);
  rect(95, 135, 15, 15);
  rect(65, 120, 15, 15);
  rect(80, 120, 15, 15);
  rect(50, 105, 15, 15);
  rect(65, 90, 15, 15);
  rect(80, 90, 15, 15);

  //draw the n
  rect(130, 150, 15, 15);
  rect(130, 135, 15, 15);
  rect(130, 120, 15, 15);
  rect(130, 105, 15, 15);
  rect(130, 90, 15, 15);
  rect(145, 105, 15, 15);
  rect(145, 120, 15, 15);
  rect(160, 135, 15, 15);
  rect(175, 150, 15, 15);
  rect(175, 135, 15, 15);
  rect(175, 120, 15, 15);
  rect(175, 105, 15, 15);
  rect(175, 90, 15, 15);

  //draw the e
  rect(210, 150, 15, 15);
  rect(210, 135, 15, 15);
  rect(210, 120, 15, 15);
  rect(210, 105, 15, 15);
  rect(210, 90, 15, 15);
  rect(225, 90, 15, 15);
  rect(240, 90, 15, 15);
  rect(255, 90, 15, 15);
  rect(210, 120, 15, 15);
  rect(225, 120, 15, 15);
  rect(240, 120, 15, 15);
  rect(255, 120, 15, 15);
  rect(210, 150, 15, 15);
  rect(225, 150, 15, 15);
  rect(240, 150, 15, 15);
  rect(255, 150, 15, 15);

  //draw the k
  rect(290, 150, 15, 15);
  rect(290, 135, 15, 15);
  rect(290, 120, 15, 15);
  rect(290, 105, 15, 15);
  rect(290, 90, 15, 15);
  rect(305, 120, 15, 15);
  rect(320, 105, 15, 15);
  rect(320, 135, 15, 15);
  rect(335, 90, 15, 15);
  rect(335, 150, 15, 15);


  //title screen buttons
  stroke(86, 204, 0);
  strokeWeight(1);
  fill(0, 50);
  //play button
  if (mouseX > 100 && mouseX < 300 && mouseY > 220 && mouseY < 270) {
    fill(50, 50);
  }
  rect(100, 220, 200, 50);
  fill(86, 204, 0);
  textSize(30);
  text("PLAY", 165, 255);

  //about button
  fill(0, 50);
  if (mouseX > 100 && mouseX < 300 && mouseY > 300 && mouseY < 350) {
    fill(50, 50);
  }
  rect(100, 300, 200, 50);
  fill(86, 204, 0);
  text("ABOUT", 150, 335);
}

void mouseClicked() {
  //title screen shenanigans, this one makes the about page appear
  if (mouseX > 100 && mouseX < 300 && mouseY > 300 && mouseY < 350) {
    about = true;
    back = false;
  }
  //this one makes the game start
  if (mouseX > 100 && mouseX < 300 && mouseY > 220 && mouseY < 270) {
    play = true;
  }
  //this one takes you back to the main menu from the about page
  if (mouseX > 10 && mouseX < 60 && mouseY > 340 && mouseY < 390) {
    back = true;
    about = false;
  }
}

void callAbout() {
  fill(86, 204, 0);
  //about text for Snek
  textSize(18);
  text("Snek is like Snake,", 120, 20);
  text("but Snek is much smarter than Snake.", 35, 40);
  text("Instead of wasting valuable nutrients on", 25, 60);
  text("growing in size, Snek instead converts all", 19, 80);
  text("those nutrients into energy which it then", 23, 100);
  text("lets off in a flash of light.", 90, 120);
  text("As a result Snek never has to worry about", 15, 140);
  text("biting its own tail and killing itself.", 45, 160);
  text("Because Snek doesn't have to worry about", 15, 180);
  text("killing itself, it is effectively immortal and", 20, 200);
  text("can spend its days running around and ", 30, 220);
  text("eating food for all eternity.", 80, 240);

  //draw the back button
  fill(0, 50);
  if (mouseX > 10 && mouseX < 60 && mouseY > 340 && mouseY < 390) {
    fill(50, 50);
  }
  stroke(86, 204, 0);
  strokeWeight(1);
  rect(10, 340, 50, 50);
  fill(86, 204, 0);
  rect(35, 355, 20, 20);
  triangle(35, 385, 35, 345, 12, 365);
}