A small JavaScript golf game ⛳. View demo here: codepen.io.
-
Add JavaScript code containing class
Golf
in an HTML document:<script src="js/golf.js" type="text/javascript"></script>
-
Add HTML element for the game in
body
section:<div id="golf-container"></div>
-
Сreate an instance of class
Golf
after theDOMContentLoaded
event:document.addEventListener("DOMContentLoaded", () => { golf = new Golf("golf-container", 35, 20); });
Class
Golf
constructor parameters:containerId
— ID of HTML element for the game;cellSizePx
— size of cell in pixels, default 35 pixels;speedMs
— frame duration in milliseconds, default 20 milliseconds.