diff --git a/game.js b/game.js new file mode 100644 index 0000000..c3f61f5 --- /dev/null +++ b/game.js @@ -0,0 +1,121 @@ +import { Robot } from "./robot.js"; + +const consoleElement = document.getElementById("console"); +const gameCanvas = document.getElementById("gameCanvas"); +const ctx = gameCanvas.getContext("2d"); + +let pyodideWorker = startPyodideWorker(); +let robots = createInitialRobots(); +let paused = false; + +// ✅ Function to create the Pyodide Worker +function startPyodideWorker() { + const worker = new Worker("pyodide-worker.js"); + + // ✅ Reattach the event listener when a new worker is created + worker.onmessage = (event) => { + if (paused) return; + + switch (event.data.type) { + case "console": + logToConsole(event.data.data); + break; + case "error": + logToConsole(`${event.data.message}`); + break; + case "fire": + fire(); + break; + case "turn": + turn(event.data.data); + break; + case "move": + move(event.data.data); + break; + } + }; + + return worker; +} + +// ✅ Function to create initial robots +function createInitialRobots() { + return { + "player": new Robot("player", 50, 50, "blue"), + "enemy1": new Robot("enemy1", 200, 150, "red"), + "enemy2": new Robot("enemy2", 400, 250, "red") + }; +} + +// ✅ Function to log messages to console +function logToConsole(text) { + console.log(text.replace("", "").replace("", "")); + consoleElement.innerHTML += text.replace(/\n/g, "
") + "
"; + consoleElement.scrollTop = consoleElement.scrollHeight; +} + +// ✅ Game Control Functions +function fire() { + logToConsole("🔥 Gun Fired! 🔥"); +} + +function turn(deg) { + robots["player"].turn(deg); +} + +function move(distance) { + robots["player"].move(distance); +} + +// ✅ Pause/Resume Function +function togglePause() { + paused = !paused; + document.getElementById("pause-button").innerText = paused ? "Resume" : "Pause"; +} + +// ✅ Reset Function (Fixed) +function resetGame() { + // Terminate the worker + pyodideWorker.terminate(); + + // Restart the worker and rebind event listener + pyodideWorker = startPyodideWorker(); + + // Reset the robots to their initial state + robots = createInitialRobots(); + + // Clear the console + consoleElement.innerHTML = ""; + + // Unpause the game if it was paused + paused = false; + document.getElementById("pause-button").innerText = "Pause"; +} + +// ✅ Game Loop +function gameLoop() { + if (!paused) { + ctx.clearRect(0, 0, gameCanvas.width, gameCanvas.height); + for (let id in robots) { + robots[id].update(ctx); + } + } + requestAnimationFrame(gameLoop); +} + +// Start game loop +gameLoop(); + +// ✅ Button Event Listeners +document.getElementById("compile-button").addEventListener("click", () => { + if (paused) return; + const code = document.getElementById("python-code").value; + consoleElement.innerHTML = ""; + pyodideWorker.postMessage({ + type: "execute", + code: code + }); +}); + +document.getElementById("pause-button").addEventListener("click", togglePause); +document.getElementById("reset-button").addEventListener("click", resetGame); diff --git a/index.html b/index.html index cb078ab..70ac71e 100644 --- a/index.html +++ b/index.html @@ -29,6 +29,9 @@

+ + +

Console Output:

@@ -37,105 +40,8 @@

2D Tank Simulation

- + + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2963302 --- /dev/null +++ b/readme.md @@ -0,0 +1,8 @@ +import robot +import time + +robot.move(0.5) +await time.sleep(1) +while True: + robot.turn(5) + await time.sleep(0.1) \ No newline at end of file diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..cce3a75 --- /dev/null +++ b/todo.md @@ -0,0 +1,9 @@ +DO + + +IN PROGRESS + + +DONE +Add Pause Button +Add reset button. \ No newline at end of file