xxxxxxxxxx
168
egg = '🥚'
hatching_chick = '🐣'
baby_chick = '🐤'
front_facing_baby_chick = '🐥'
rooster = '🐓'
rooster_head = '🐔'
chicken_leg = '🍗'
fried_egg = '🍳'
chickens = []
fox = '🦊'
cat = '🐈'
let fence_width = 10
let font
let predator
function setup() {
canvas = createCanvas(400, 400)
canvas.mouseClicked(onclick)
new_chicken = new Chicken(width/2 + random(-20, 20), height/2 + random(-20, 20))
chickens.push(new_chicken)
}
function draw() {
background(220)
stroke(0)
fill(255)
rect(
fence_width,
fence_width,
width - 2 * fence_width,
height - 2 * fence_width,
)
for (let chicken of chickens) {
chicken.draw()
}
predator?.draw()
}
function onclick() {
if (chickens.length == 4) {
predator = new Predator(width / 2, height / 2)
}
new_chicken = new Chicken(mouseX, mouseY)
chickens.push(new_chicken)
}
class Predator {
constructor(x, y) {
if (random() > 0.5) {
this.emoji = fox
} else {
this.emoji = cat
}
this.x = x
this.y = y
this.dx = random(-2, 2)
this.dy = random(-2, 2)
this.tsize = random(50, 100)
}
draw() {
this.x += this.dx
this.y += this.dy
let border = fence_width + this.tsize / 2
if (this.x > width - border || this.x < border) {
this.dx *= -1
}
if (this.y > height - border || this.y < border) {
this.dy *= -1
}
textSize(this.tsize)
text(this.emoji, this.x - this.tsize / 2, this.y + this.tsize / 2)
}
}
class Chicken {
constructor(x, y) {
this.growing_chicken = [
egg,
hatching_chick,
baby_chick,
front_facing_baby_chick,
rooster,
chicken_leg,
]
this.x = x
this.y = y
this.state = 0
this.tsize = random(25, 75)
this.time = 0
this.dx = random(-2, 2)
this.dy = random(-2, 2)
}
draw() {
this.time += deltaTime
if (this.state < this.growing_chicken.length - 2 && this.time > 1000) {
this.time = 0
this.state += 1
}
if (this.state == this.growing_chicken.length - 2) {
this.x += this.dx
this.y += this.dy
let border = fence_width + this.tsize / 2
if (this.x > width - border || this.x < border) {
this.dx *= -1
}
if (this.y > height - border || this.y < border) {
this.dy *= -1
}
}
if (predator) {
let boxsize = 1.3 * this.tsize
const chickenBox = [
this.x - boxsize / 2.5,
this.y - boxsize / 2.5,
boxsize,
boxsize,
]
let predBoxsize = 1.3 * predator.tsize
const predatorBox = [
predator.x - predBoxsize / 2.5,
predator.y - predBoxsize / 2.5,
predBoxsize,
predBoxsize,
]
if((predatorBox.x > chickenBox[0] && predatorBox.x < chickenBox[0] + chickenBox[2]
&& predatorBox.y > chickenBox[1] && predatorBox.y < chickenBox[1] + chickenBox[3])
||
(this.x > predatorBox[0] && this.x < predatorBox[0] + predatorBox[2]
&& this.y > predatorBox[1] && this.y < predatorBox[1] + predatorBox[3])
){
this.time = 0
this.state = this.growing_chicken.length - 1
}
}
textSize(this.tsize)
text(
this.growing_chicken[this.state],
this.x - this.tsize / 2,
this.y + this.tsize / 2,
)
}
}