Advertisement
Tsskyx

Snake game (made with p5.js)

May 20th, 2016
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //this uses the p5 library; download it, or the editor here: http://p5js.org/download
  2.  
  3. function Body(pos_) {
  4.   this.pos = createVector(pos_.x, pos_.y);
  5.   this.rect1 = createVector(cellsize / 2 + 2 * cellsize * this.pos.x, cellsize / 2 + 2 * cellsize * this.pos.y);
  6.   this.rect2 = createVector(this.rect1.x - cellsize * nextpos.x, this.rect1.y - cellsize * nextpos.y);
  7.  
  8.   this.display = function() {
  9.     rect(this.rect1.x, this.rect1.y, cellsize + 1, cellsize + 1);
  10.     if (snake[0] != this) {
  11.       rect(this.rect2.x, this.rect2.y, cellsize + 1, cellsize + 1);
  12.     }
  13.   }
  14. }
  15.  
  16. function Food(index) {
  17.   this.pos = createVector(emptycells[index].x, emptycells[index].y);
  18.  
  19.   this.display = function() {
  20.     this.fpos = createVector(cellsize / 2 + 2 * cellsize * this.pos.x, cellsize / 2 + 2 * cellsize * this.pos.y);
  21.     ellipse(this.fpos.x, this.fpos.y, cellsize, cellsize);
  22.   }
  23. }
  24.  
  25. var fps = 6;
  26. var gridsize = 21;
  27. var gamemode = "MENU";
  28. var cellsize, emptycells, snake, headpos, nextpos, foodIndex, food, direction, lock, space;
  29. var grid = new Array(gridsize);
  30.  
  31. function setup() {
  32.   createCanvas(600, 600);
  33.   cellsize = width / gridsize / 2;
  34.   headpos = createVector(0, 0);
  35.   nextpos = createVector(0, 0);
  36.   for (var i = 0; i < grid.length; i++) {
  37.     grid[i] = new Array(grid);
  38.   }
  39.   resetFormat();
  40. }
  41.  
  42. function resetFormat() {
  43.   ellipseMode(CORNER);
  44.   rectMode(CORNER);
  45.   textAlign(CENTER, CENTER);
  46.   textSize(30);
  47.   fill(255);
  48.   noStroke();
  49. }
  50.  
  51. function draw() {
  52.   switch (gamemode) {
  53.     case "MENU":
  54.       loadMenu();
  55.       break;
  56.     case "SETUP":
  57.       loadGame();
  58.       break;
  59.     case "PLAY":
  60.       launchGame();
  61.       break;
  62.     case "END":
  63.       die();
  64.   }
  65. }
  66.  
  67. function keyPressed() {
  68.   if (gamemode == "SETUP" && keyCode > 36 && keyCode < 41) {
  69.     gamemode = "PLAY";
  70.     loop();
  71.   }
  72.  
  73.   if (gamemode == "PLAY" && !lock) {
  74.     lock = true;
  75.  
  76.     if (keyCode == 37 && direction != "R") {
  77.       nextpos.set(-1, 0);
  78.       direction = "L";
  79.     }
  80.    
  81.     if (keyCode == 38 && direction != "D") {
  82.       nextpos.set(0, -1);
  83.       direction = "U";
  84.     }
  85.    
  86.     if (keyCode == 39 && direction != "L") {
  87.       nextpos.set(1, 0);
  88.       direction = "R";
  89.     }
  90.  
  91.     if (keyCode == 40 && direction != "U") {
  92.       nextpos.set(0, 1);
  93.       direction = "D";
  94.     }
  95.   }
  96. }
  97.  
  98. function loadMenu() {
  99.   background(0);
  100.   fill(100);
  101.   rectMode(CENTER);
  102.   rect(width / 2, height / 2 - 5, 300, 80);
  103.   fill(255);
  104.   text("Welcome to Snake!", width / 2, height / 2 - 15);
  105.   textSize(20);
  106.   text("(press ENTER to start)", width / 2, height / 2 + 15);
  107.   text("Made by Tsskyx", width / 2, height - 30);
  108.   resetFormat();
  109.   if (keyIsPressed && keyCode == ENTER) {
  110.     gamemode = "SETUP";
  111.   }
  112. }
  113.  
  114. function loadGame() {
  115.   lock = false;
  116.   showtail = false;
  117.   direction = null;
  118.   emptycells = [];
  119.   snake = [];
  120.   headpos.set(floor(gridsize / 2), floor(gridsize / 2));
  121.   nextpos.set(0, 0);
  122.   snake.push(new Body(headpos));
  123.   for (var i = 0; i < grid.length; i++) {
  124.     for (var j = 0; j < grid.length; j++) {
  125.       grid[i][j] = false;
  126.       grid[snake[0].pos.x][snake[0].pos.y] = true;
  127.       if (!grid[i][j]) {
  128.         emptycells.push(createVector(i, j));
  129.       }
  130.     }
  131.   }
  132.   foodIndex = floor(random(emptycells.length));
  133.   food = new Food(foodIndex);
  134.  
  135.   background(0);
  136.   textSize(20);
  137.   text("press any arrow key to begin", width / 2, height - 30);
  138.   snake[0].display();
  139.   food.display();
  140.   resetFormat();
  141.   noLoop();
  142. }
  143.  
  144. function launchGame() {
  145.   lock = false;
  146.   headpos.add(nextpos);
  147.   snake.push(new Body(headpos));
  148.   if (headpos.x == food.pos.x && headpos.y == food.pos.y) {
  149.     emptycells = [];
  150.     for (var i = 0; i < grid.length; i++) {
  151.       for (var j = 0; j < grid.length; j++) {
  152.         grid[i][j] = false;
  153.         for (var k = 0; k < snake.length; k++) {
  154.           if (!offscreen()) {
  155.             grid[snake[k].pos.x][snake[k].pos.y] = true;
  156.           }
  157.         }
  158.         if (!grid[i][j]) {
  159.           emptycells.push(createVector(i, j));
  160.         }
  161.       }
  162.     }
  163.     if (emptycells.length !== 0) {
  164.       foodIndex = floor(random(emptycells.length));
  165.       food.pos.set(emptycells[foodIndex].x, emptycells[foodIndex].y);
  166.     }
  167.   } else {
  168.     snake.shift();
  169.   }
  170.  
  171.   background(0);
  172.   if (!space && snake.length > 4) {
  173.     textSize(20);
  174.     fill(255);
  175.     text("Hint: Press and hold SPACEBAR for a speed boost.", width / 2, height - 30);
  176.     resetFormat();
  177.   }
  178.  
  179.   if (keyIsPressed && key == " ") {
  180.     frameRate(3 * fps);
  181.     space = true;
  182.   } else {
  183.     frameRate(fps);
  184.   }
  185.   for (i = 0; i < snake.length; i++) {
  186.     snake[i].display();
  187.   }
  188.   food.display();
  189.  
  190.   for (k = 0; k < max(1, snake.length - 1); k++) {
  191.     if (headpos.x == snake[k].pos.x && headpos.y == snake[k].pos.y && snake.length > 4 || offscreen() || emptycells.length === 0) {
  192.       gamemode = "END";
  193.       die();
  194.     }
  195.   }
  196. }
  197.  
  198. function offscreen() {
  199.   return headpos.x < 0 || headpos.x > gridsize - 1 || headpos.y < 0 || headpos.y > gridsize - 1;
  200. }
  201.  
  202. function die() {
  203.   frameRate(60);
  204.   fill(100);
  205.   rectMode(CENTER);
  206.   rect(width / 2, height / 2, 200, 80);
  207.   fill(255);
  208.   text("You died!", width / 2, height / 2 - 13);
  209.   textSize(20);
  210.   text("Final score: " + (snake.length - 1), width / 2, height / 2 + 17);
  211.   fill(0);
  212.   rect(width / 2, height - 30, 300, 30);
  213.   fill(255);
  214.   text("(press ENTER to restart)", width / 2, height - 30);
  215.   resetFormat();
  216.  
  217.   if (keyIsPressed && keyCode == ENTER) {
  218.     gamemode = "SETUP";
  219.   }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement