From c5876c87e1a0b53d23eb591eb3bebec713589f83 Mon Sep 17 00:00:00 2001 From: shoe Date: Sat, 22 Mar 2025 00:59:40 +0000 Subject: [PATCH] Implement colors, and let the user kill a random walker --- index.html | 5 +++-- walkers.js | 53 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/index.html b/index.html index 588e4c9..0a332d9 100644 --- a/index.html +++ b/index.html @@ -9,8 +9,9 @@

as in i am repeatedly breaking stuff all the time

code modified from https://jackmckew.dev/interactive-random-walkers-with-javascript.html

- - + + +
diff --git a/walkers.js b/walkers.js index 0c2d723..bfd9252 100644 --- a/walkers.js +++ b/walkers.js @@ -2,23 +2,43 @@ * Buttons that interact with the UI * ****************************************************************************/ +// a rainbow-adjacent set of colors for the walkers to cycle through +var walker_colors = [ + "#33FF00", // green, also from https://superuser.com/a/1206781 + "#0033ff", // blue + "#a553fc", // purple + "#FF2E00", // red + "#FFB000", // amber, also from https://superuser.com/a/1206781. this is the default color used, so it is last in the array. +]; +var color_idx = 0; + function create_new_walker() { var my_row = Math.floor(Math.random() * n_rows); var my_col = Math.floor(Math.random() * n_cols); - // TODO: CHOOSE COLOR, ONLY DO IT IN SEQUENCE FROM A LIST OF KNOWN-GOOD - // COLORS. THE SEQUENCE WILL BE FINE. - var my_color = "#FFB000"; + var my_color = walker_colors[color_idx]; + // TODO: ADD SPEED TO THIS WHOLE DEAL walkers.push({row: my_row, col: my_col, color: my_color}); + color_idx = (color_idx + 1).mod(walker_colors.length); } -function reset() { - // kill all the walkers and trails - walkers.splice(0, walkers.length); - trails.splice(0, trails.length); +function destroy_random_walker() { + // destroy a random walker. + // we can't get the trail, that'll have to fade on its own + var choice = Math.floor(Math.random() * walkers.length); + var removed = walkers.splice(choice, 1); + + // hide where the walker used to be + draw_grid_square(removed[0].row, removed[0].col, "black"); +} + +function destroy_all_walkers() { + // kill all the walkers, but not their trails -- i like the look better + var removed = walkers.splice(0, walkers.length); // draw black everywhere on the canvas to hide the dead bodies D: - ctx.fillStyle = "black"; - ctx.fillRect(0, 0, canvas.width, canvas.height); + for (var i = 0; i < removed.length; i++) { + draw_grid_square(removed[i].row, removed[i].col, "black"); + } } /**************************************************************************** @@ -185,11 +205,16 @@ var ctx = canvas.getContext('2d') canvas.width = window.innerWidth; canvas.height = window.innerHeight; -// find the UI buttons and give them click handlers to activate -var reset_btn = document.getElementById("button-reset"); -var new_walker_btn = document.getElementById("button-new-walker"); -reset_btn.addEventListener("click", reset); -new_walker_btn.addEventListener("click", create_new_walker); +// find the UI buttons and give them click handlers to activate our functions +document + .getElementById("button-create-walker") + .addEventListener("click", create_new_walker); +document + .getElementById("button-destroy-walker") + .addEventListener("click", destroy_random_walker); +document + .getElementById("button-reset-walkers") + .addEventListener("click", destroy_all_walkers); // compute the initial rows and columns var cell_size = 22;