xxxxxxxxxx
130
const maxSize = 600
let creature1 = {
x: 100,
y: 100,
size: 120,
col: '#e466a0',
eyeCol: '#ff0010',
strokeCol: '#ffcc00',
speedX: 3,
speedY: 0.1,
growRate: 1,
health: 300,
declineRate: 0.75,
}
let creature2 = {
x: 200,
y: 300,
size: 100,
col: '#1966a0',
eyeCol: '#ffcc00',
strokeCol: '#ff0010',
speedX: 5,
speedY: 2,
growRate: 1,
health: 300,
declineRate: 1,
}
let creature3 = {
x: 50,
y: 20,
size: 95,
col: '#ffa0a0',
eyeCol: '#ffa0c00',
strokeCol: '#ff0010',
speedX: 5,
speedY: 2,
growRate: 1,
health: 300,
declineRate: 1.2,
}
function setup(){
createCanvas(600, 600)
}
function draw() {
background(220);
let creature_list = [creature1, creature2, creature3]
creature_list.forEach((creature) => {
updateCreature(creature)
if (checkHealth(creature)){
drawCreature(creature)
}
})
}
function checkHealth(creature){
if (creature.health >= 0){
return true
}else{
return false
}
}
function updateCreature(creature){
moveCreature(creature)
checkEdges(creature)
growCreature(creature)
creature.health = creature.health - creature.declineRate
}
function drawCreature(creature){
fill_color = color(creature.col)
fill_color.setAlpha(creature.health)
fill(fill_color)
circle(creature.x, creature.y, creature.size)
stroke(creature.strokeCol)
eye_color = color(creature.eyeCol)
eye_color.setAlpha(creature.health)
drawEyes(creature.x, creature.y, creature.size, eye_color)
}
function drawEyes(x,y,sz,eyeCol){
fill(eyeCol)
circle(x - 20, y - 20, sz/10)
circle(x + 20, y - 20, sz/10)
}
function moveCreature(creature){
creature.x = creature.x + creature.speedX
creature.y = creature.y + creature.speedY
}
function checkEdges(creature){
if(creature.x < 0 || creature.x > 600){
creature.speedX = creature.speedX * -1
}
if(creature.y < 0 || creature.x > 600){
creature.speedY = creature.speedY * -1
}
}
function growCreature(creature){
if(creature.size < maxSize){
creature.size = creature.size + creature.growRate
}else{
resetCreature(creature)
}
}
function resetCreature(creature){
creature.size = random(10,100)
creature.col = pickRandomColor()
creature.eyeCol = pickRandomColor()
creature.strokeCol = pickRandomColor()
}
function pickRandomColor(){
return [random(255),random(255),random(255)]
}