Last active
July 7, 2017 21:13
-
-
Save ianfitzpatrick/727c6808bf7f74553894116125244ae7 to your computer and use it in GitHub Desktop.
walker_es6.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Nature of Code | |
// Daniel Shiffman | |
// http://natureofcode.com | |
let walker | |
function setup() { | |
createCanvas(640,360) | |
walker = new Walker() | |
background(127) | |
} | |
function draw() { | |
walker.step() | |
walker.render() | |
} | |
class Walker { | |
constructor() { | |
this.x = width/2 | |
this.y = height/2 | |
} | |
render() { | |
stroke(0) | |
point(this.x,this.y) | |
} | |
step() { | |
let choice = floor(random(4)) | |
if (choice === 0) { | |
this.x++ | |
} else if (choice == 1) { | |
this.x-- | |
} else if (choice == 2) { | |
this.y++ | |
} else { | |
this.y-- | |
} | |
this.x = constrain(this.x, 0, width - 1) | |
this.y = constrain(this.y, 0, height - 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment