how to make this ground platform initial starting position to be drawn from the…
- User
how to make this ground platform initial starting position to be drawn from the beginning of execution on whole canvas width? also, add player spawn position at left-top corner.: class Platform { constructor(x, y, w, h) { this.x = x; this.y = y; this.w = w; this.h = h; } collidesWith(obj) { if (obj.y + obj.h <= this.y) return false; if (obj.y >= this.y + this.h) return false; if (obj.x + obj.w <= this.x) return false; if (obj.x >= this.x + this.w) return false; const objAbove = obj.y + obj.h - obj.vy <= this.y; const objBelow = obj.y - obj.vy >= this.y + this.h; const objLeft = obj.x + obj.w - obj.vx <= this.x; const objRight = obj.x - obj.vx >= this.x + this.w; if (obj.vy > 0 && objAbove && !objBelow) { obj.y = this.y - obj.h; obj.vy = 0; obj.jumping = false; return true; } if (obj.vy < 0 && !objAbove && objBelow) { obj.y = this.y + this.h; obj.vy = 0; return true; } if (obj.vx < 0 && objRight) { obj.x = this.x + this.w; obj.vx = 0; return true; } if (obj.vx > 0 && objLeft) { obj.x = this.x - obj.w; obj.vx = 0; return true; } return false; } } class GroundPlatform extends Platform { constructor(x, y, w, h) { super(x, y, w, h); } } class Player { constructor(x, y, w, h) { this.x = x; this.y = y; this.w = w; this.h = h; this.vx = 0; this.vy = 0; this.jumping = false; } move(keys) { const friction = .8; const gty = 0.8; if (keys[87] && !this.jumping) { this.vy -= 10; this.jumping = true; } if (keys[68]) { this.vx += 0.8; } if (keys[65]) { this.vx -= 0.8; } this.vx *= friction; this.vy += gty; this.x += this.vx; this.y += this.vy; if (this.x < 0) { this.x = 0; } if (this.y < 0) { this.y = 0; } if (this.x + this.w > canvas.width) { this.x = canvas.width - this.w; this.vx = 0; } if (this.y + this.h > canvas.height) { this.y = canvas.height - this.h; this.vy = 0; this.jumping = false; } } }
class Projectile { constructor(x, y, vx, vy) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.radius = 3; this.color = "red"; } update() { this.x += this.vx; this.y += this.vy; } draw(ctx) { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); ctx.fillStyle = this.color; ctx.fill(); } }
class Entity { constructor() { this.x = canvas.width; this.y = Math.random() * canvas.height; this.w = 10; this.h = 10;
this.vy = 0; this.jumping = false; this.projectiles = []; this.color = "blue";
} jump() { if (!this.jumping) { this.vy -= 25 - Math.random() * 25; this.jumping = true; } } update(platforms) { for (let i = 0; i < platforms.length; i++) { platforms[i].collidesWith(this); } const friction = .99; const gty = 1; this.vx = friction; this.vy += gty; this.x += this.vx; this.y += this.vy;this.period = Math.PI; this.minVx = -2 + Math.sin(Math.random() * 2 * Math.PI) * 2; this.maxVx = -5 + Math.cos(Math.random() * 2 * Math.PI) * -10; this.vx = this.vxForce = randomRange(this.minVx, this.maxVx); const t = performance.now() / 1000; const wave = Math.sin(t * Math.PI / this.period); this.vxForce = lerp(this.maxVx, this.minVx, (wave + 5) / 200, 10); this.x += this.vxForce;
if (Math.random() < .01) { this.projectiles.push(new Projectile(this.x, this.y, -2 - Math.random() * 6, -2 + Math.random() * 8)); } for (let i = 0; i < this.projectiles.length; i++) { this.projectiles[i].update(); if (this.projectiles[i].x < 0 || this.projectiles[i].y < 0 || this.projectiles[i].x > canvas.width || this.projectiles[i].y > canvas.height) { this.projectiles.splice(i, 1); i; } } if (Math.random() < .01) { this.jump(); } } draw(ctx) { ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.w, this.h); for (let i = 0; i < this.projectiles.length; i++) { this.projectiles[i].draw(ctx); } } } - Assistant
To make the ground platform start on the whole canvas width you can change its starting position (x value) and its width (w value) to match the canvas width. Moreover, you need to spawn the player at the left-top corner by setting its x and y value to 0.
Here is the modified code:
class GroundPlatform extends Platform { constructor(y, h) { super(0, y, canvas.width, h); } } // Spawn ground platform using the constructor const groundPlatform = new GroundPlatform(canvas.height - 50, 20); // Spawn player at left-top corner const player = new Player(0, 0, 10, 10);Now replace your existing GroundPlatform constructor and player spawn position in your Game constructor with the new code:
class Game { constructor(canvas) { ... this.platforms = []; this.player = new Player(0, 0, 10, 10); // Spawn player at left-top corner this.platforms.push(new GroundPlatform(canvas.height - 50, 20)); // Spawn ground platform ... } ... }This will create a ground platform along the whole canvas width and spawn the player at the top-left corner.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.