xxxxxxxxxx
/*
Get the focus on canvas that keys would work: press the mouse button on canvas
BIRD:
bigger: ARROW-UP
smaller: ARROW-DOWN
rotate: hold the mouse button down
reset the game: R
*/
let bird;
let img;
let score = 0;
let speedOfRect = 0.3;
let amountOfRect = 500;
let birdRotationSpeed = 10;
let birdSpeed = 15; //ex 5 faster, ex. 15 slower
function preload() {
img = loadImage('bg.jpg');
bird = createSprite(windowWidth/2, windowHeight/2, 100, 40);
bird.addAnimation('floating', 'bird-1.png', 'bird-2.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60);
bird.addAnimation('moving',
'bird-1.png', 'bird-2.png', 'bird-3.png', 'bird-4.png',
'bird-5.png', 'bird-6.png', 'bird-7.png','bird-8.png');
createAbstractElements(); //creates a collection of abstract pink rectangles
}
function draw() {
let resizedWidth = img.height * width / img.height;
push(); //creates the background image and effect
img.resize(resizedWidth, height);
image(img, 0, 0, img.width, img.height);
filter(POSTERIZE, map(mouseX, 0, width, 10, 50));
pop();
bird.velocity.x = (mouseX-bird.position.x)/birdSpeed; //moves the bird toward the mouseX
bird.velocity.y = (mouseY-bird.position.y)/birdSpeed; //moves the bird toward the mouseY
//when mouse is to the left, flips the bird
if (mouseX < bird.position.x - 10) {
bird.mirrorX(-1);
bird.changeAnimation('moving');
} else if (mouseX > bird.position.x + 10) {
bird.mirrorX(1); //unflip the bird
bird.changeAnimation('moving');
} else {
//when close to the mouse, don't move the bird
bird.changeAnimation('floating');
}
if(mouseIsPressed) {
bird.rotation -= birdRotationSpeed; //rotates the bird
bird.changeAnimation('floating');
} else {
bird.rotation = 0;
}
if(keyIsDown(UP_ARROW)){
bird.scale += 0.05; //makes the bird bigger
}
if(keyIsDown(DOWN_ARROW)){
bird.scale -= 0.05; //makes the bird smaller
}
//moves the collection of abstract pink rectangles
for (let i = 0; i < collectibles.length; i++) {
collectibles[i].position.x += collectibles[i].width * speedOfRect;
if (collectibles[i].position.x > width) {
collectibles[i].position.x = 0;
}
}
bird.overlap(collectibles, collect); //bird collects the rectangle when overlaps
drawSprites(); //draws the sprites
textSize(40);
fill(255);
textAlign(CENTER, CENTER);
if (score < amountOfRect) {
text(score, 80, 40);
} else {
text('Nice!', 80, 40);
text('Press R to reset the game', width/2, 40);
}
}
function collect(collector, collected) //collector is the bird and collected is the rectangle
{
score += 1;
collected.remove(); //removes the rectangle
}
function createAbstractElements() {
collectibles = new Group();
for(let j = 0; j < amountOfRect; j++){
let abstractElement = createSprite( //creates rectangles
random(0, windowWidth),
random(10, windowHeight - 10),
random(5, 20),
random(5, 20)
);
abstractElement.shapeColor = color( //adds color pink
252,
random(100, 220),
random(200, 255)
);
collectibles.add(abstractElement);
}
}
function resetGame(){
score = 0;
collectibles.removeSprites();
createAbstractElements();
}
function keyPressed() {
if (key === 'r' || key === 'R') {
resetGame();
}
}