Add reset button and glider and pulsar on ctrl and shift click

This commit is contained in:
2025-02-24 11:45:23 +01:00
parent f68977a557
commit 922a7d3160
3 changed files with 118 additions and 1 deletions

View File

@@ -23,6 +23,7 @@
<body>
<canvas id="game-of-life-canvas"></canvas>
<button id="play-pause"></button>
<button id="reset">Reset</button>
<script src="./bootstrap.js"></script>
</body>

View File

@@ -28,7 +28,13 @@ canvas.addEventListener("mousedown", event => {
const row = Math.min(Math.floor(canvasTop / (CELL_SIZE + 1)), height - 1);
const col = Math.min(Math.floor(canvasLeft / (CELL_SIZE + 1)), width - 1);
universe.toggle_cell(row, col);
if (event.ctrlKey) {
universe.glider(row, col);
} else if (event.shiftKey) {
universe.pulsar(row, col);
} else {
universe.toggle_cell(row, col);
}
drawGrid();
drawCells();
@@ -38,6 +44,12 @@ const ctx = canvas.getContext('2d');
const playPauseButton = document.getElementById("play-pause");
const reset = document.getElementById("reset");
reset.addEventListener("click", event => {
universe.kill_all();
});
const drawGrid = () => {
ctx.beginPath();
ctx.strokeStyle = GRID_COLOR;