xxxxxxxxxx
177
let i = 0;
let chickclick;
let mamaclick;
let startclick;
let chick;
let mama;
// Width of the chick and mom
let chickWidth = 70;
let mamaWidth = 90;
let startWidth = 70
// Height of the chick and mom
let chickHeight = 70;
let mamaHeight = 90;
let startHeight = 70
// Starting X position of chick and mom, no value to start
let chickxpos;
let mamaxpos;
let startxpos = 258
// Starting Y position of chick and mom, no value to start
let chickypos;
let mamaypos;
let startypos = 130
// Speed of the chick and mom
let chickxspeed = 12;
let chickyspeed = 12;
// use 12 for speed for a doable challenge
let mamaxspeed = 7;
let mamayspeed = 7;
// Left or Right
let xdirection = 1;
//up or down
let ydirection = 1;
function setup() {
// Set the starting position of the shape
createCanvas(600, 300);
chickxpos = width / 4;
chickypos = height / 4;
mamaxpos = width / 4;
mamaypos = height / 4;
chick = loadImage("chick1.png");
mama = loadImage("mama.png");
wood = loadImage("wood.jpeg");
frameRate(30);
}
function draw() {
startScreen()
if (startclick) {
game() } else {
startScreen()
}
}
function startScreen(){
background(54, 45, 42);
fill(54, 45, 42);
stroke(0);
strokeWeight(35);
rect(0, 0, width, height);
textSize(32);
fill(255);
strokeWeight(2);
text("Catch Me If You Can", 150, 100);
image(mama, 50, 100, 70, 70);
image(chick, 480, 100, 70, 70);
fill(0);
stroke(255);
rect(startxpos, startypos, startWidth, startHeight);
fill(255);
stroke(0);
textSize(12);
strokeWeight(2);
text("Catch the baby chick", 240, 230);
text("Avoid the mother chicken", 230, 250);
textSize (25);
text ('Start',265,175)
}
function game() {
image(wood, width / 2, height / 2, width, height);
imageMode(CENTER);
fill(255, 255, 255);
textSize(20);
if (chickclick) {
chickclick = false;
if (!mamaclick) {
i = i + 1;
}
/* fill(0, 0, 0);
rect(15, 0, 270, 30);*/
fill(255, 255, 255);
}
if (mamaclick) {
fill(0, 0, 0);
rect(15, 0, 270, 30);
fill(255, 255, 255);
text("Game Ended! Final Score: " + i, 20, 20);
} else {
fill(0, 0, 0);
rect(15, 0, 90, 30);
fill(255, 255, 255);
text("Score: " + i, 20, 20);
// Update the position of the shape
mamaxpos = mamaxpos + xdirection * mamaxspeed;
mamaypos = mamaypos + ydirection * mamayspeed;
//right wall
if (mamaxpos > width - mamaWidth / 2) {
xdirection = -1;
}
//left wall
if (mamaxpos < mamaWidth / 2) {
xdirection = 1;
}
//bottom
if (mamaypos > height - mamaHeight / 2) {
ydirection = -1;
}
// top
if (mamaypos < mamaHeight / 2) {
ydirection = 1;
}
// Update the position of the shape
chickxpos = chickxpos + xdirection * chickxspeed;
chickypos = chickypos + ydirection * chickyspeed;
//right wall
if (chickxpos > width - chickWidth / 2) {
xdirection = -1;
}
//left wall
if (chickxpos < chickWidth / 2) {
xdirection = 1;
}
//bottom
if (chickypos > height - chickHeight / 2) {
ydirection = -1;
}
// top
if (chickypos < chickHeight / 2) {
ydirection = 1;
} }
// Draw the shape
image(chick, chickxpos, chickypos, chickWidth, chickHeight);
// where the image of chick is placed
imageMode(CENTER);
image(mama, mamaxpos, mamaypos, mamaWidth, mamaHeight);
// where the image of mom chicken is placed
imageMode(CENTER);
}
function mousePressed() {
// Check if mouse is inside the chick
let a = dist(mouseX, mouseY, chickxpos, chickypos);
if (a < chickWidth / 2) {
chickclick = true;
}
// Check if mouse is inside the mama
let b = dist(mouseX, mouseY, mamaxpos, mamaypos);
if (b < mamaWidth / 2) {
mamaclick = true;
}
let c = dist(mouseX, mouseY, startxpos, startypos);
if (c < startWidth/2) {
startclick = true;
}
}