// Kyle Leve
// kleve@andrew.cmu.edu
// Section A
// Project-10-Landscape
var img1;
var img2;
var snowman = [];
var terSpeed1 = 0.0002;
var terDetail1 = 0.01;
var terSpeed2 = 0.00025;
var terDetail2 = 0.005;
var terSpeed3 = 0.0003;
var terDetail3 = 0.0001;
// Loads ski guy and snowman images
function preload() {
img1 = loadImage('https://i.imgur.com/9Bjj5oY.jpg');
img2 = loadImage('https://i.imgur.com/6UIcB6H.jpg');
}
function setup() {
createCanvas(480, 480);
frameRate(10);
}
function draw() {
background('indigo');
// Dark blue mountains
fill(0, 20, 70);
beginShape();
vertex(0, height);
for (var x = 0; x < width; x++) {
var z = (x * terDetail1) + (millis() * terSpeed1);
var y = map(noise(z), 0, 1, 0, height/3);
vertex(x, y);
}
vertex(width, height);
endShape();
// Lighter blue mountains
fill(0, 20, 120);
beginShape();
vertex(0, height);
for (t = 0; t < width; t++) {
var s = (t * terDetail2) + (millis() * terSpeed2);
var r = map(noise(s), 0, 1, 0, height - 100);
vertex(t, r);
}
vertex(width, height);
endShape();
// Snow hill
fill(255);
beginShape();
vertex(0, height);
for (a = 0; a < width; a++) {
var b = (a * terDetail3) + (millis() * terSpeed3);
var c = map(noise(b), 0, 0.75, 0, height - 70);
vertex(a, c);
}
vertex(width, height);
endShape();
// Ski guy image
noStroke();
image(img1, 20, c + 40, 70, 70);
displaySnowman();
removeSnowman();
newSnowman();
}
// To show the snowman
function displaySnowman() {
for (var i = 0; i < snowman.length; i++){
snowman[i].move();
snowman[i].display();
}
}
// Removes any snowmen that go off the screen
function removeSnowman() {
var keepSnowman = [];
for (var i = 0; i < snowman.length; i++){
if (snowman[i].x + snowman[i].breadth > 0) {
keepSnowman.push(snowman[i]);
}
}
snowman = keepSnowman;
}
// Creates snowmen
function newSnowman() {
var snowmanChance = 0.0005;
if (random(0.1) < snowmanChance) {
snowman.push(makeSnowman(width));
}
}
// Moves snowmen
function snowmanMove() {
this.x += this.speed;
}
// Calls the snowman image
function snowmanDisplay() {
var b = (a * terDetail3) + (millis() * terSpeed3);
var c = map(noise(b), 0, 0.75, 0, height - 70);
noStroke();
push();
translate(this.x, height - 100);
image(img2, 10, 0, 70, 100);
pop();
}
// Sets the location of the snowman and moves it across the screen
function makeSnowman(LocationX) {
var snow = {x: LocationX,
breadth: 50,
speed: -20.0,
move: snowmanMove,
display: snowmanDisplay}
return snow;
}
I found this project to be very fun because it allowed me to create a scene even if it just repeated itself. I decided to create a winter scene with a guy skiing and some snowmen. I used the snowmen as an object that would randomly generate itself as the scene continued.