xxxxxxxxxx
// Credits
// Coin Sound Effect: https://freesound.org/people/cabled_mess/sounds/350873/
let player, coin;
let coinSound, bigCoinSound, poisonousCoinSound;
let coins;
let bigCoins;
let poisonousCoins;
let walls;
let score = 0;
function preload() {
coinSound = loadSound("coin.wav");
bigCoinSound = loadSound("bigCoin.wav");
poisonousCoinSound = loadSound("poisonoisCoin.wav");
}
function setup() {
new Canvas(600, 600);
player = new Sprite(0, height / 2);
coins = new Group();
coins.color = 'yellow';
coins.diameter = 10;
bigCoins = new Group();
bigCoins.color = 'rgb(130,249,130)';
bigCoins.diameter = 30;
poisonousCoins = new Group();
poisonousCoins.color = 'rgb(230,74,255)';
poisonousCoins.diameter = 4;
poisonousCoins.strokeWeight = 0;
walls = new Group();
// Create all the coins that will go in the group
for (let i = 0; i < 50; i++) {
let c = new coins.Sprite();
c.x = random(-500, 500);
c.y = random(-200, 800);
}
// Create all the coins that will go in the group
for (let i = 0; i < 15; i++) {
let c = new bigCoins.Sprite();
c.x = random(-500, 500);
c.y = random(-200, 800);
}
// Create all the coins that will go in the group
for (let i = 0; i < 75; i++) {
let c = new poisonousCoins.Sprite();
c.x = random(-500, 500);
c.y = random(-200, 800);
}
// Put walls to contain player outside of camera frame
let w = new walls.Sprite(-500, height/2);
w.height = 1000;
w.width = 20;
w.collider = "static";
let w1 = new walls.Sprite(500, height/2);
w1.height = 1000;
w1.width = 20;
w1.collider = "static";
let w2 = new walls.Sprite(0, 800);
w2.height = 20;
w2.width = 1000;
w2.collider = "static";
let w3 = new walls.Sprite(0, -200);
w3.height = 20;
w3.width = 1000;
w3.collider = "static";
}
function draw() {
background("rgb(127,127,255)")
// Player Movement
//player.moveTowards(mouse, 0.1);
player.speed = 4;
if (kb.pressing("up")) {
player.direction = -90;
// player.vel.y = -4;
} else if (kb.pressing("down")) {
player.direction = 90;
// player.vel.y = 4;
} else if (kb.pressing("right")) {
player.direction = 0;
// player.vel.x = 4;
} else if (kb.pressing("left")) {
player.direction = 180;
// player.vel.x = -4;
} else {
player.speed = 0;
}
// To collide with a group, the syntax is
// similar
// if (player.overlaps(coins)) {
// coinSound.play();
// // This code does play the sound, but does not remove the coin!
// To accomplish that, we need to use a "callback" function, which
// means that we give the name of the function we want to call when
// the collision happens as the second argument to the overlaps function
// }
// Here's what we do instead:
player.overlaps(coins, collectCoin);
// Collide with the big coins
player.overlaps(bigCoins, collectBigCoin);
// Collide with the poisonous coins
player.overlaps(poisonousCoins, collectPoisonousCoin)
// Update the score at the very end, after all collisions have happened
player.text = score;
// Update the camera to follow the player
camera.x = player.x;
camera.y = player.y;
// text("(" + mouseX + ", " + mouseY + ")", mouseX, mouseY);
}
function collectCoin(p, c) {
coinSound.play();
c.remove();
score++;
}
function collectBigCoin(p, c) {
bigCoinSound.play();
c.remove();
score = score + 5;
}
function collectPoisonousCoin(p, c) {
poisonousCoinSound.play();
c.remove();
score = score - 5;
}