xxxxxxxxxx
247
//Tanvi Mishra
//Ben
//Code 1, Assignment 3: Personal Time
//So, for my personal time prject I tried to create a character that marches to the beat of their own drum. Their sleep cycle is determined by when their energy levels get used up rather than just day and night. This project is dedicated to the many allnighters I have and will pull throughout the semester.
function setup() {
frameRate(5);
noStroke();
createCanvas(600, 600);
dayBlue = color('#1BC0DB');//all the colour codes
dayLight = color('#8FCCD6');
nightBlue = color('#0F0F35');
nightPurple = color('#36124C');
moonColour = color('#D2EDF3');
sunColour = color('#F4FF79');
groundColour = color('#421700');
cloudArray= []; //holds 6 clouds x position, y position and size
cloudCount = 6;
timeSinceStart=0; //for sun/moon durations
movementInc = (height/15) //for the sun and moon rise
movement = height
sign = 1; //used to dictate direction of sun and moon
length = 100; //character dimensions and positions
breadth = 80;
xPos=0;
yPos=height-150;
energyLevel = random(2400,3400); //energy level of the character
totalEnergyLevel = energyLevel //initial energy level of the character
printText ="LET'S START!"; //for messages
rotateDegree = 0;
start=0;
flag = 0
}
function draw(){
if( start%2==0){
displayScreenBG()
}
else
displayScreen1();
if( mouseIsPressed){
start+=1;
}
}
function displayScreen1() {
timeSinceStart+=1
if (movement >= height ){ //changes dir when ellipse moves beyond height
sign=-1;
movement+=sign *movementInc
}
else if (movement <= -20 ) { //changes dir when ellipse moves less height
sign =+1
movement+=sign *movementInc
}
else
movement+=sign *movementInc
if((timeSinceStart%60)<30){ //this checks if it is day or night and call fn accordingly
dayTime();
}
else{
nightTime();
}
//ground colour
fill(groundColour);
rect(0, height-height/6, width,height/6);
snooze(); //displays message according to energy levels
keyTypedMotion(); // moves the character accorrding to keys pressed
}
function displayScreenBG(){
background(0);
if ( flag == 0){
textSize(30);
fill('#F05123');
text('Click mouse to start', 80, 80);
text('Press d to move forward', 80, 120);
text('Press a to move backward', 80, 160);
text('Press w to jump up', 80, 200);
}
else{
textSize(30);
fill('#F05123');
text('THANK YOU!!',230, height/2-100);
}
}
function dayTime(){
setGradient(dayLight,dayBlue); // fn for background colour
ellipseMove(sunColour,movement,sign); //fn for sun colour, speed, direction
cloudSetup(); //fns for cloud creation and movement
cloudMovement();
}
function cloud(x, y, size) {
// creates the cloud shape
fill(255, 255, 255);
noStroke();
arc(x, y, 25 * size, 20 * size, PI + TWO_PI, TWO_PI);
arc(x + 10, y, 25 * size, 45 * size, PI + TWO_PI, TWO_PI);
arc(x + 25, y, 25 * size, 35 * size, PI + TWO_PI, TWO_PI);
arc(x + 40, y, 30 * size, 20 * size, PI + TWO_PI, TWO_PI);
}
function cloudSetup(){
for (i = 0; i < cloudCount; i++) { //creates 6 clouds and assigns random size and positions
var newCloud = {
xPos: random(width),
yPos: random(height-100),
size: random(0.5, 1.8)
};
cloudArray.push(newCloud);
}
}
function cloudMovement(){
for (cl = 0; cl < cloudCount; cl++) {
var currentObj = cloudArray[cl];//sets each cloud to a temp variable
cloud(currentObj.xPos, currentObj.yPos, currentObj.size);
currentObj.xPos += 5;//moves the position of cloud to the left bit by bit...
currentObj.yPos += random(-2, 2); //slight up and down movement
if (cloudArray[cl].xPos > width+20) {
cloudArray.splice(cl, 1); // when it reaches the end, splice/remove it
}
}
}
function nightTime(){
setGradient(nightBlue,nightPurple,sign);// fn for background colour
//stars
fill('#FFFFFF')
noStroke();
for(i=0; i<random(20,50);i++){
ellipse(random(width),random(height),random(1,3));
}
ellipseMove(moonColour,movement); //fn for sun colour, speed, direction
}
function setGradient(c1, c2) {
//fn to set a gradient when two colours are inputted
//this snippet of code is borrowed...
noFill();
for (var y = 0; y < height; y++) {
var inter = map(y, 0, height, 0, 1);
var c = lerpColor(c1, c2, inter);
stroke(c);
line(0, y, width, y);
}
noStroke();
}
function ellipseMove(c,movement){
//moves the sun or moon up and down
fill(c)
ellipse(width-(width/10), movement ,80);
}
function character(){
//uses various shapes to create the simple character
fill('#F05123');
rect(xPos,yPos-length,breadth,length);
rect(xPos+breadth/5,yPos,breadth/5,length/2);
rect(xPos+breadth/2,yPos,breadth/5,length/2);
fill('#0F0F35');
rect(xPos+breadth/10,yPos+ 5-length,breadth-2*(breadth/10), 10);
rect(xPos+2*breadth/10, yPos-45-length,breadth-4.5*(breadth/10), 50);
fill('#FFFFFF');
ellipse(xPos+breadth-breadth/5,yPos+50-length, 20,35);
fill('#3F1B0D');
ellipse(xPos+breadth-breadth/5+5,yPos+50-length, 10,15);
}
function keyTypedMotion() {
if (key === 'd') {
xPos+=40 //moves forward when d is pressed
energyLevel -=40 //decreases the energy level since energy is being spent
}
else if (key === 'a') {
xPos-=40 //moves backward when a is pressed
energyLevel -=80 //decreases the energy level since energy is being spent
}
else if (key === 'w') {
yPos-=60
setTimeout (pause,300) //pauses between Y position = 540 and Y position = 600 thus indicating a jump
key ='s'
energyLevel -=120 //decreases the energy level since energy is being spent
}
character(); //calls the fn
if(xPos>width){ //creates rollover effect
xPos=0
}
else if (xPos<0){
xPos = width
}
}
function pause(){
yPos=height-150 //just used to pause
}
function snooze(){
messageDisplay(printText)
if(energyLevel<=-30){
printText = 'DONE!';
flag=1 // open final page
displayScreenBG(); //used to exit the game
}
else if(energyLevel <0.1*totalEnergyLevel){
printText = 'GOODNIGHT!';
scale(0.3) //indicate char is asleep
translate(0,1170);
}
else if(energyLevel <0.4*totalEnergyLevel){
printText = 'SOOOOOO TIRED....';
scale(0.6) //indicate char is gonna be asleep
translate(0,335);
}
else if(energyLevel <0.7*totalEnergyLevel){
printText = 'I AM GETTING TIRED';
scale(0.9) //indicate char is asleep
translate(0,55);
}
}
function messageDisplay(printText){
textSize(32);
fill('#F05123');
text(printText, 40, 540);
}