xxxxxxxxxx
57
/*
Based on Daniel Shiffman's Random Walker
http://codingtra.in
https://youtu.be/l__fEY1xanY
https://thecodingtrain.com/CodingChallenges/052-random-walk.html
See also Introduction to the Nature of Code v2.0 by Daniel Shiffman:
https://drive.google.com/file/d/1G_16tPKByN9ya6l2Ws58X-OJK1yex9IX/view
*/
let x, y;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
stroke(255);
strokeWeight(4);
x = width / 2;
y = height / 2;
}
function draw() {
point(x, y); //creates the initial dot in the middle of the screen
let randomNum = floor(random(4)); //generates a random integer between 0 and 4
// console.log(randomNum);
if (randomNum == 0) {
x += 4; //if the random number choosen is 0, x increases by 4 and a new point is drawn
} else if (randomNum == 1) {
x -= 4; //if the random number choosen is 1 x decreased by 4 and a new point is drawn
} else if (randomNum == 2) {
y += 4; //if the random number choosen is 2, y increases by 4 and a new point is drawn
} else {
y -= 4; //if any other number is choosen, y decreases by 4 and a new point is drawn
}
if (x > width || x < 0) {
x = width / 2; //if x ever gets to be bigger or smaller then the width, the new point starts in the middle of the width of the screen
}
if (y > height || y < 0) {
y = height / 2; //if the y ever gets to be bigger or smaller then the height, the new point starts in the middle of the height
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
background(0); //resizes the canvas and starts the design over again
}
function mousePressed(){
background(0)
x = mouseX;
y = mouseY;
} //when the mouse is pressed, the design starts over and the new position starts where the mouse was clicked