xxxxxxxxxx
114
// here is a comment
/*
adding objects
by Mariam
11/11/24
*/
let player = Player();
let bird1, bird2;
let charIdleSprite, charWalkSprite, charEatSprite;
let birdFlyingSprite, birdEatenSprite;
function preload() {
charIdleSprite = loadImage("sprites/blueWalking.gif");
charWalkSprite = loadImage("sprites/blueIdle.gif");
charEatSprite = loadImage("sprites/blueEatss.gif");
birdFlyingSprite = loadImage("sprites/birdFlying.gif");
birdEatenSprite = loadImage("sprites/birdEaten.gif");
}
function setup() {
createCanvas(400, 400);
bird1 = Bird();
bird2 = Bird();
}
function draw() {
background(200);
player.move();
player.display();
bird1.move();
bird1.display();
bird2.move();
bird2.display();
}
// player object
// objects have capital letter
function Player() {
let x = 200;
let y = 300;
//animation values
let animationState = 0;
// 0 is idle
// 1 is walk
// 2 is eat
function move() {
animationState = 0; // assume player is idle
if (keyIsDown(RIGHT_ARROW)) {
x = x + 5;
animationState = 1; // walk animation
}
if (keyIsDown(LEFT_ARROW)) {
x = x - 5;
animationState = 1; // walk animation
}
}
function display() {
if (animationState == 0) {
image(charIdleSprite, x, y);
} else if (animationState == 1) {
image(charWalkSprite, x, y);
}
}
return { move, display };
}
function Bird() {
let x = random(0, width);
let y = 0;
let speed = random(2,8);
let animationState = 0;
// 0 is idle
// 1 is easten
function move() {
y = y + speed;
if (y > height) {
y = 0;
x = random(0, width);
speed = random(2, 8);
}
}
function display() {
if (animationState == 0) {
image(birdFlyingSprite, x, y)
}
}
return {move, display}
}
function mousePressed() {
// save("character.jpg");
}