/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://p5js.sketchpad.cc/sp/pad/view/ro.watRHAjNiEj/rev.2833
*
* authors:
* Javier Lopez
*
*
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/* Had to switch between Sketchpad.cc and a the P5 Editor where
the Sprite images where referenced differently
suhImage = loadImage("/static/uploaded_resources/p.21053/s_unit_hz.png");
sImage = loadImage("/static/uploaded_resources/p.21053/snake.png");
fImage = loadImage("/static/uploaded_resources/p.21053/food.png");
nImage= loadImage("/static/uploaded_resources/p.21053/newgame.png");
//Loaded images from sketch folder "assets"
suhImage = loadImage("assets/s_unit_hz.png");
sImage = loadImage("assets/snake.png");
fImage = loadImage("assets/food.png");
nImage= loadImage("assets/newgame.png");
*/
//----------------------------------------------------Program Start----------------------------------------------------------------
//Image Variables to later be loaded by Sprites
var suhImage,sImage,fImage,nImage;
var len; //length of snake
var fCtr=0; //Counter for food consumed by Snake
var i,j,k,l,m,n,q;//Indexing Variables for Looping
var units; //Variable to be made into Sprite Group
//Individual Sprite Variables: Snake, Food and Unit (tail)
var snake, unit, food;
var dir; //Snake Direction (Up, Down, Left, Right)
var sxPos,syPos,fxPos,fyPos,sPos,fPos;
var eaten=false; //Boolean for if food was consumed
var gameOver; //Boolean for status of game
//End Globals
function setup() {
createCanvas(1280,640); //1280 x 640 Window Size
gameOver=true;
//Loaded images from sketch folder "assets"
suhImage = loadImage("/static/uploaded_resources/p.21053/s_unit_hz.png");
sImage = loadImage("/static/uploaded_resources/p.21053/snake.png");
fImage = loadImage("/static/uploaded_resources/p.21053/food.png");
nImage= loadImage("/static/uploaded_resources/p.21053/newgame.png");
snake = createSprite(width/2,height/2, 20, 20);
snake.addImage(sImage);
snake.setCollider("rectangle", 0, 0, 10, 10);
snake.rotation=0;
title = createSprite(width/2,10);
title.addImage(nImage);
units = new Group();
units.add(snake);
len = units.length;
createFood();
} //End Setup
function draw() {
frameRate(18);
if(gameOver && keyWentDown("x")) {
newGame(); //If game over, press "x" to start a New Game
}
if(!gameOver) { //During gameplay...
background(255,255,255); //White Background
text("Score: "+fCtr,10,height-10); //Score Counter
text("Units: "+len,10,height-20); //Snake Length
sxPos=snake.position.x;
syPos=snake.position.y;
setDir(); //Set Direction of Snake according to Dir variable
travel(); //Movement function for all Sprites
if (len>2) {
//If the head of snake collides with the tail then game over
for(j = 1; j<len; j++){
if (snake.overlap(units[j])) {
die();
}
}
}
if(snake.overlap(food)) {
len++;
eaten=true;
removeSprite(food);//Remove sprite after being eaten
createFood(); //Make a new food Sprite if the last one was eaten
addUnit(); //Add additional unit to Snake
fCtr++; //Add one to counter everytime food is eaten
}
//Restricts the snake to staying within the window borders
switch(true) {
case (snake.position.y <= 0):
die();
break;
case (snake.position.x < 0):
die();
break;
case (snake.position.y >= height):
die();
break;
case (snake.position.x > width):
die();
break;
case (snake.position.x === width):
die();
break;
case (snake.position.x === 0):
die();
break;
}
}
//Draw all sprites to sketch.
drawSprite(title);
drawSprite(food);
drawSprites(units);
}//End Draw
function createFood() {
food = createSprite(random(20, width-20), random(20, height-20), 20, 20);
food.addImage(fImage);
food.setCollider("rectangle", 0, 0, 10, 10);
eaten=false;
}
function addUnit() {
unit = createSprite(0, 0, 20, 20);
units.add(unit);
unit.addImage(suhImage);
unit.rotation=(units[len - 1].rotation);
unit.setCollider("rectangle", 0, 0, 10, 10);
units[len - 1].position.x = (units[len - 2].position.x)+snake.width;
units[len - 1].position.y = (units[len - 2].position.y)+snake.height;
updateSprites(true);
}
function travel() { //Travel function for all blocks
//if (units.length>1) { //Travel for tail sprites
for(i = len - 1; i>0; i-=1) { //moves positions backwards
units[i].position.x = units[i-1].position.x;
units[i].position.y = units[i-1].position.y;
}
//}
switch(dir) { //Travel for Snake head
case "up":
snake.position.y-=40;
snake.rotation=-90;
break;
case "down":
snake.position.y+=40;
snake.rotation=90;
break;
case "left":
snake.position.x-=40;
snake.rotation=180;
break;
case "right":
snake.position.x+=40;
snake.rotation=0;
break;
}
snake.position.x = ((snake.position.x + width) % width);
snake.position.y = ((snake.position.y + height) % height);
}
function setDir() { //Sets Direction for Snake
if(keyIsPressed) { //Once a key is pressed
switch(keyCode) { //Checks keycode JS std
case UP_ARROW: //Code#: 38
dir = "up";
break;
case DOWN_ARROW: //Code#: 40
dir = "down";
break;
case LEFT_ARROW: //Code#: 37
dir = "left";
break;
case RIGHT_ARROW: //Code#: 39
dir = "right";
break;
}
}
}
function die() {
len = 1;
var fScore=fCtr; //For final score
removeSprite(food); //Remove previous food
if (units.length>1) {
for (k=units.length - 1; k>0; k--) {
units[k].remove(); //Remove snake's tail units
}
}
updateSprites(true);
background(255,255,255); //White Background
text("He's Dead Jim, Press X to Start Again", width/2-100,height/2);
text("Your Final Score Was: " + fScore, width/2-60,height/1.8);
textAlign(CENTER); //Center text listed above
gameOver = true; //Game is over
}
function newGame() { //When a new game is started
fCtr=0; //Set foods eaten to 0
gameOver = false; //Game is not over anymore
title.remove(); //Remove the Initial Game Title
food.remove(); //Remove any residual foods
snake.position.x = width/2; //Snake starts in the center
snake.position.y = height/2;
dir="right"; //It will be moving right initially
createFood(); //Make the initial food object
updateSprites(true); //Redraw the sprite objects
}
//----------------------------------------------------Program End------------------------------------------------------------------