xxxxxxxxxx
96
// Connie Hu - Week 4 - Game creation!
// Sources used
// Coding Challenge #115: Snake Game Redux by Daniel Shiffman
// https://youtu.be/OMoVcohRgZA
let header
let snake;
let rez = 20;
let food;
let w;
let h;
let bg;
// preload images used in sketch
function preload(){
frog = loadImage('images/frog.png');
}
function setup(){
canvas = createCanvas(500,500);
canvas.parent('canvas_placement'); // to have snake game/cavas right under 'h1'
w = floor(width / rez);
h = floor(height / rez);
frameRate(5);
snake = new Snake();
foodLocation();
header = createElement('h2','Interesting Caption')
bg = loadImage('images/self_portrait.png');
transparency_slider = createSlider(0, 255, 255);
}
function foodLocation() {
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
snake.setDir(-1, 0);
} else if (keyCode === RIGHT_ARROW) {
snake.setDir(1, 0);
} else if (keyCode === DOWN_ARROW) {
snake.setDir(0, 1);
} else if (keyCode === UP_ARROW) {
snake.setDir(0, -1);
} else if (key == ' ') {
snake.grow();
}
}
function draw() {
noTint();
background(bg);
scale(rez);
if (snake.eat(food)) {
foodLocation();
}
snake.update();
snake.show();
// Display stats and game over when game ends!
if (snake.endGame()) {
textSize(2);
background('Purple');
print("You Died!") // prints to Console!
fill('white');
text('GAME OVER', 5, 12);
text("Your snake length: ", 3, 17)
text(snake.body.length, 21,17)
noLoop();
return;
}
// spawn food and color
fill("yellow");
strokeWeight(0.2)
stroke('Black')
tint(255, transparency_slider.value());
image(frog, food.x, food.y, 2, 2);
// display score count!
strokeWeight(0.2)
stroke('Black')
textSize(2);
fill('White');
text(snake.body.length, 18, 3);
}