var birds = []
var trees = []
var deadTrees = []
var timberJacks = []
var angle = 70
var angle2 = 50
var x = 30
var y = 50
function setup() {
createCanvas(600, 500)
frameRate(10)
}
function draw() {
//draw backgrounds
push()
drawDesertAndSky()
drawNightWorld()
drawSun()
pop()
//rotating lines for the sun
push()
translate(150,0)
for (y = 50; y <= 300; y += 2){
rotate (radians(angle))
stroke (255, 215, 0)
line (30, y, x+2, y)
}
pop()
push()
for (x = 30; x <= 400; x += 5){
rotate (radians(angle2))
stroke (255, 255, 100)
line (x-25, x-40, x-40, 0.8*40)
}
pop()
//draw trees and deadtrees
push()
for (var i = 0; i < trees.length; i++){
trees[i].displays()
}
pop()
push()
for (var i = 0; i < deadTrees.length; i++){
deadTrees[i].showss()
}
pop()
//draw timberjacks
push()
updateAndDisplayTimberJacks()
addNewTimberJackWithSomeRandomProbability()
pop()
//draw birds
push()
updateAndDisplayBirds();
removeBirdsThatHaveSlippedOutOfView();
addNewBirdsWithSomeRandomProbability();
pop()
//texts
push()
fill(0,100,0)
textSize(18);
strokeWeight(3)
stroke(0,100,0)
text("CURRENT WORLD",100,70)
textSize(16);
text("WE DON'T WANT OUR WORLD DESTROYED!",10,100)
strokeWeight(2)
text("Click to Plant Trees Here! :)",40,150)
fill(0)
textSize(20)
strokeWeight(3)
stroke(0)
text("FALLEN WORLD",430,50)
textSize(16);
strokeWeight(2)
text("Klik to pLanT dEAd TrEEs:(",400,80)
pop()
}
//functions that draw the background
function drawDesertAndSky(){
noStroke()
fill(237, 201, 155)//desert
rect(0,170,400,330)
fill(135,206,235)//sky
rect(0,0,400,170)
}
function drawNightWorld(){
noStroke()
fill(48,25,52)
rect(400,170,200,330)
fill(216,191,216)
rect(400,0,200,170)
}
function drawSun(){
translate(150,0)
fill(255,200,0)//sun
ellipse(0,0,100,100)
line(50,50,85,85)
line(75,20, 125, 30)
line(20,75, 25 ,125)
}
//interactive mousePressed function to draw trees
function mousePressed(){
if (mouseX <= 400){
var tree_instance = makeTree(mouseX, constrain(mouseY,150,550), 0)//plant live trees on the ground
trees.push(tree_instance)
}else{
var deadTree_instance = makeDeadTree(mouseX, constrain(mouseY,180,550))
deadTrees.push(deadTree_instance)
}
}
//the tree & dead tree functions
function makeTree(tx, ty, th){
var tree = {x: tx, y: ty, height: th,
displays: treeDraw}
return tree
}
function treeDraw(){
fill(0,255,0)
triangle(this.x,this.y-this.height,this.x-7,this.y+15-this.height,this.x+7,this.y+15-this.height)
fill(50,30,0)
rect(this.x-2,this.y+15-this.height,4,10+this.height)
//trees grow
if(this.height <= 10){
this.height+=0.1
}
}
function makeDeadTree(tx, ty){
var deadTree = {x: tx, y: ty,
showss: deadTreeDraw}
return deadTree
}
function deadTreeDraw(){
push()
fill(129)
triangle(this.x+10,this.y-4,this.x+10,this.y+8,this.x+25,this.y)
rect(this.x,this.y,10,4)
pop()
}
//timberjacks
function makeTimberjack(jx, jy){
var timberJack = {x: jx, y: jy,
display: timberJackDraw,
speedX: random(3,6),
speedY: random(-1,1),
move: timberJackMove}
return timberJack
}
function timberJackDraw(){
push()
strokeWeight(1)
ellipse(this.x,this.y,9,12)
//body
line(this.x,this.y+6,this.x,this.y+20)//body
line(this.x,this.y+15,this.x+5,this.y+15)//left hand
line(this.x,this.y+15,this.x+5,this.y+12)//right hand
line(this.x,this.y+20,this.x+5,this.y+27.5)//right leg
line(this.x,this.y+20,this.x-5,this.y+27.5)//left leg
//eyes
ellipse(this.x-1.5,this.y-1,0.5,0.5)
ellipse(this.x+1.5,this.y-1,0.5,0.5)
arc(this.x, this.y+5, 6, 7.5, PI+1,-1)
//hair
line(this.x,this.y-6,this.x,this.y-11)
line(this.x-1.5,this.y-5.5,this.x-5,this.y-9)
line(this.x+1.5,this.y-6.5,this.x+5,this.y-9)
pop()
//electric saw
push()
noStroke()
fill(0)
rect(this.x+5,this.y+10,5,8)
rect(this.x+7,this.y+12,11,4)
triangle(this.x+18,this.y+12,this.x+18,this.y+16,this.x+23,this.y+12)
pop()
}
function timberJackMove() {
this.x += this.speedX;
this.y += this.speedY
}
function addNewTimberJackWithSomeRandomProbability() {
// With a probability, add a new timberjack to the end.
var newTimberJackLikelihood = 0.05;
if (random(0,1) < newTimberJackLikelihood) {
timberJacks.push(makeTimberjack(0,random(200,470)));
}
}
function updateAndDisplayTimberJacks(){
// Update the timberjacks' positions, and display them.
for (var i = 0; i < timberJacks.length; i++){
timberJacks[i].move();
timberJacks[i].display();
}
}
function removeTimberJacksThatHaveSlippedOutOfView(){
var timberJacksToKeep = [];
for (var i = 0; i < timberJacks.length; i++){
if (timberJacks[i].x > 0) {
timberJacksToKeep.push(timberJacks[i]);
}
}
timberJacks = timberJacksToKeep;
}
//birds
function removeBirdsThatHaveSlippedOutOfView(){
var birdsToKeep = [];
for (var i = 0; i < birds.length; i++){
if (birds[i].x > 0) {
birdsToKeep.push(birds[i]);
}
}
birds = birdsToKeep; // remember the surviving birds
}
function updateAndDisplayBirds(){
// Update the birds' positions, and display them.
for (var i = 0; i < birds.length; i++){
birds[i].move();
birds[i].display();
}
}
function addNewBirdsWithSomeRandomProbability() {
// With a probability, add a new bird to the end.
var newBirdLikelihood = 0.02;
if (random(0,1) < newBirdLikelihood) {
birds.push(makeBird(width,random(20,160),0));
}
}
// method to update position of birds every frame
function birdMove() {
this.x -= this.speedX;
this.y += this.speedY
}
// draw the birds
function birdDisplay() {
push()
fill(this.c)
triangle(this.x,this.y,this.x+6,this.y+1.5,this.x+6,this.y-1.5)//beak
ellipse(this.x+9,this.y,7,7) //head
ellipse(this.x+17,this.y+4,15,8)//body
arc(this.x+18, this.y, 10, 10, -0.7, PI-0.7,CHORD)//wings
ellipse(this.x+8,this.y-1,1,1)//eyes
pop()
}
//make new bird with different location, color, speedXY
function makeBird(birthLocationX,birthLocationY,color) {
var bird = {x: birthLocationX,
y: birthLocationY,
c: color,
speedX: random(5,8),
speedY: random(-1.5,1.5),
move: birdMove,
display: birdDisplay}
return bird;
}
Since the theme for the project is climate change, I want to write an interactive program that presents scenery that compares a current world and a fallen world to warn people about the consequence of climate change.
The inspiration comes from the trees I was drawing in project 11. I thought of trees immediately when the topic was said to be climate change. Then I had the idea that comparison would be the best way to send out a message of warning. Therefore, the timber jacks in my program were born to play the role of antagonist; the night world is created to demonstrate the potential consequence if we don’t take climate change seriously.
For the interaction part, I want users to be able to plant trees themselves. In the current world, they can plant a tree by clicking on the canvas and the tree would grow gradually to a maximum height. But if the user tries to plant trees in the fallen world, the trees would turn out to be dead trees. This is to imply that once climate change gets to a point, there is no turning back (for a long long time).
Though I did a lot of decorations like the birds and the transformations and loops just for the rays of sunlight, I feel like I could make the scenery more real if I have more time. I might want to use sin wave to draw the desert; I might draw more detailed timber jacks; I might add clouds. But overall, I feel like the program is quite complete, as the message is delivered and I believe that I’ve done all 6 technical merits.