xxxxxxxxxx
126
// Connie Hu - Creative Coding Lab Project A
// Project Title: Pond Scum
// 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;
let song1;
let song2;
let song3;
// preload images used in sketch
function preload() {
frog = loadImage("images/frog.png");
song2 = loadSound("game_music/song2.mp3");
song3 = loadSound("game_music/song3.mp3");
}
function setup() {
canvas = createCanvas(500, 500);
song1 = loadSound("game_music/song1.mp3", loaded);
canvas.parent("canvas_placement"); // placement for interactive window
w = floor(width / rez);
h = floor(height / rez);
frameRate(1);
snake = new Snake(); // create snake instance
foodLocation(); // call foodLocation() function
bg = "#fff6d3"; // background color
//bg = loadImage('images/self_portrait.png');
// transparency_slider = createSlider(0, 255, 255);
// transparency_slider.parent('slider_placement');
button = createButton("Music 1");
button.parent("song_switcher");
button.mousePressed(togglePlaying);
function loaded() {
song1.play();
}
}
// toggele between game songs!
function togglePlaying() {
if (song1.isPlaying()) {
song1.pause();
song2.play();
song2.setVolume(0.3);
button.html("Music 2");
} else if (song2.isPlaying()) {
song2.pause();
song3.play();
song3.setVolume(0.3);
button.html("Music 3");
} else {
song3.pause();
song1.play();
song1.setVolume(0.3);
button.html("Music 1");
}
}
function foodLocation() {
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y); // create food vector
}
// defining how to move
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() {
background(bg);
scale(rez);
// check each loop
// spawn more a new food every time one is eaten
if (snake.eat(food)) {
foodLocation();
}
// spawn food
image(frog, food.x, food.y, 2, 2);
snake.update();
snake.show(frog);
// Display stats and game over when game ends!
if (snake.endGame()) {
textSize(2);
background("#1e847f");
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;
}
// display score count!
strokeWeight(0.2);
stroke("Black");
textSize(2);
fill("White");
text(snake.body.length, 23, 2);
}