xxxxxxxxxx
221
/*
adding collision between bird and player
background music: https://opengameart.org/content/gangtazz
ball collision sound effect https://opengameart.org/content/3-ping-pong-sounds-8-bit-style
*/
// lower case player is the instance/clone
// capital Player is the blueprint for the objects
let player = Player();
let ball1, ball2;
let collisionRadius = 10;
// media variables
let charIdleSprite, charCatchSprite, charWalkSprite;
let basketballSprite, ballBreakingSprite;
let backgroundMusic, ballSound;
function preload() {
charIdleSprite = loadImage("sprites/p5idle.gif");
charCatchSprite = loadImage("sprites/p5catch.gif");
charWalkSprite = loadImage("sprites/p5walk.gif");
basketballSprite = loadImage("sprites/basketball.gif");
ballBreakingSprite = loadImage("sprites/ballbreaking.gif");
backgroundMusic = loadSound("audio/Gang$tazz.mp3");
ballSound = loadSound("audio/ping_pong_8bit_beeep.mp3");
}
function setup() {
createCanvas(400, 400);
imageMode(CENTER);
ball1 = Ball();
ball2 = Ball();
console.log(ballSound);
ballSound.play();
backgroundMusic.play();
backgroundMusic.loop(true);
}
function draw() {
background(220);
// call a method from an objects
player.move();
player.display();
ball1.move();
ball1.display();
ball2.move();
ball2.display();
// detects collision between b1 and player //
let ball1Collide = detectCollision(player, ball1);
// reacts to collision event
if (ball1Collide){
player.grab();
ball1.caught();
console.log('collide');
if (!ballSound.isPlaying()){
ballSound.play();
}
}
}
//player object
function Player() {
//member variables (only xist in player function) //
//object properties
//position
let x = 200;
let y = 300;
//animation values
let animationState = 0;
//0 is idle
//1 is walk
//2 is catch
function move() {
// if player is not eating then we assume player is idle //
if (animationState != 2) {
animationState = 0; // idle
}
if (keyIsDown(RIGHT_ARROW)) {
x = x + 5;
animationState = 1;
}
if (keyIsDown(LEFT_ARROW)) {
x = x - 5;
animationState = 1;
}
if (keyIsDown(UP_ARROW)) {
y = y - 5;
animationState = 1;
}
if (keyIsDown(DOWN_ARROW)) {
y = y + 5;
animationState = 1;
}
}
function display() {
if (animationState == 0) {
image(charIdleSprite, x, y);
} else if (animationState == 1) {
image(charWalkSprite, x, y);
} else if (animationState == 2){
image(charCatchSprite, x, y);
}
// debugging the detection area
// noFill();
// circle( x, y, collisionRadius * 2);
}
function getPosition(){
return { x,y };
}
function grab(){
animationState = 2;
}
// return makes member functions available to main program
return { move, display, getPosition, grab };
}
function Ball() {
let x = random(0, width);
let y = 0; // top of canvas
let speed = random(2, 8);
let animationState = 0;
// 0 is idle
// 1 is breaking
// reset the ball when it hits the bottom of
// the canvas
function move() {
y = y + speed;
if (y > height){
y = 0;
x = random(0, width);
speed = random(2, 8);
animationState = 0;
}
}
function display() {
if (animationState == 0) {
image(basketballSprite, x, y);
} else if (animationState == 1) {
image(ballBreakingSprite, x, y)
}
}
function getPosition(){
return { x,y };
}
function caught(){
animationState = 1;
}
return { move, display, getPosition, caught };
}
function detectCollision(objA, objB) {
let aPosition = objA.getPosition();
let bPosition = objB.getPosition();
let d = dist(aPosition.x, aPosition.y, bPosition.x, bPosition.y );
let isColliding = d < collisionRadius;
return isColliding; //returns true or false
}
function mousePressed() {
// save("character.jpg");
}
// save an image of the canvas