From 58e65b2967b57389e4adf0447529eb93804b9906 Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 24 Mar 2025 12:25:55 +0800 Subject: [PATCH] added Robot class, robot module can send move and turn commands to player robot, no connection to AI robots yet --- index.html | 71 ++++++++++++++++++++++++++++++++++++++--------- pyodide-worker.js | 14 ++++++---- robot.js | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 robot.js diff --git a/index.html b/index.html index a6c0214..cb078ab 100644 --- a/index.html +++ b/index.html @@ -17,6 +17,10 @@ overflow-y: auto; border: 1px solid #ccc; } + + #gameCanvas { + border: 1px solid black; + } @@ -29,10 +33,25 @@

Console Output:

- diff --git a/pyodide-worker.js b/pyodide-worker.js index 350b562..be00981 100644 --- a/pyodide-worker.js +++ b/pyodide-worker.js @@ -27,13 +27,17 @@ class RobotModule: def get_sensor(self, name): return get_sensor_data(name) + def move(self, speed): + send_to_main("move", speed) + + def fire(self): + send_to_main("fire", None) + + def turn(self, deg): + send_to_main("turn", deg) + robot = RobotModule() -def fire(): - send_to_main("fire", None) - -def turn(deg): - send_to_main("turn", deg) class ConsoleOutput: def write(self, text): diff --git a/robot.js b/robot.js new file mode 100644 index 0000000..28c3d20 --- /dev/null +++ b/robot.js @@ -0,0 +1,54 @@ +export class Robot { + constructor(id, x, y, color = "blue") { + this.id = id; + this.x = x; + this.y = y; + this.velocity = 0; + this.direction = 0; // Degrees + this.width = 20; + this.height = 20; + this.color = color; + } + + update(ctx){ + this.update_position(); + this.draw(ctx); + } + + update_position() { + const radians = (this.direction * Math.PI) / 180; + this.x += Math.cos(radians) * this.velocity; + this.y += Math.sin(radians) * this.velocity; + } + + move(velocity) { + this.velocity = velocity; + } + + turn(degrees) { + this.direction += degrees; + } + + draw(ctx) { + ctx.fillStyle = this.color; + ctx.save(); + ctx.translate(this.x, this.y); + ctx.rotate((this.direction * Math.PI) / 180); + + // Draw the rectangle (tank body) + ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height); + + // Draw the triangle (direction indicator) + ctx.beginPath(); + ctx.moveTo(this.width / 2, -this.height / 2); // Tip of the triangle (front) + ctx.lineTo(this.width, 0); // Bottom left of triangle + ctx.lineTo(this.width / 2, this.height / 2); // Bottom right of triangle + ctx.closePath(); + + ctx.fill(); // Fill both the rectangle and the triangle + ctx.restore(); + } + + + +}