The code you provided appears to implement a basic platformer game setup, including a ground surface and platforms. The ground surface is currently not moving, which is typically what you would want in a scrolling platformer. However, if you want to keep the ground stationary while the platforms scroll, we need to ensure that the ground does not update its position, whereas the platforms do.
Here’s the corrected approach:
-
Keep the ground stationary: The ground should always be in the last row of the grid and should not be processed in a manner that would allow it to "scroll" off the screen. The positions for drawing the ground remain fixed, while only the platforms should be updated.
-
Update render logic: Render the ground without changes in its position after it is initially created.
Here’s an updated version of your code with adjustments made to the syncUpdatePositions and render functions to ensure the ground remains still while the platforms move:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mushroom Kingdom Platformer</title>
<style>
body {
margin: 0;
overflow: hidden; /* Prevent scrollbars */
}
canvas {
display: block; /* Center the canvas */
background-color: #77c9d2; /* Sky color */
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="480" height="320"></canvas>
<script>
// Constants defining the grid and gameplay parameters
const GRID_SIZE = 32; // Size of each grid cell in pixels
const GRID_WIDTH = 15; // Number of grid cells in width
const GRID_HEIGHT = 10; // Number of grid cells in height
const SCROLL_SPEED = 2; // Speed of platform scrolling
const HORIZONTAL_GAP = 2; // Minimum gap between platforms
const HOLE_CHANCE = 0.2; // Chance of a hole appearing in the ground
// Get the canvas and context
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d"); // Get the canvas context
// Class definition for game objects
class GameObject {
constructor(col, row, width, height) {
this.col = col; // X-coordinate in grid units
this.row = row; // Y-coordinate in grid units
this.width = width; // Width in grid units
this.height = height; // Height in grid units
}
// Get pixel position based on grid coordinates
getPixelPosition() {
return {
x: this.col * GRID_SIZE,
y: this.row * GRID_SIZE,
width: this.width * GRID_SIZE,
height: this.height * GRID_SIZE,
};
}
get color() {
return this instanceof Platform ? "#ffab00" : "#8B4513"; // Set color for platforms and ground
}
}
class Platform extends GameObject {
constructor(col, row, width) {
super(col, row, width, 1); // Platforms have a height of 1
}
}
class Ground extends GameObject {
constructor() {
super(0, GRID_HEIGHT - 1, GRID_WIDTH, 1); // Ground occupies the entire last row
}
}
// Initialize a pre-rendered grid array
const grid = Array.from({ length: GRID_HEIGHT }, () => Array(GRID_WIDTH).fill(null));
// Function to create platforms above the ground
function createPlatforms(count) {
let lastCol = 0; // Start placing platforms from the first column
for (let i = 0; i < count; i++) {
const width = Math.floor(Math.random() * 3) + 1; // Random width: between 1 and 3
const row = GRID_HEIGHT - 3; // Fixed row for platforms above ground
const col = lastCol + width + HORIZONTAL_GAP; // Ensure horizontal gap
// Only place platform within the grid boundaries
if (col < GRID_WIDTH) {
const platform = new Platform(col, row, width);
grid[row][col] = platform; // Place platform in the grid
}
lastCol = col; // Update for next platform placement
}
}
// Create platforms
createPlatforms(5); // Create 5 platforms
// Create the ground with holes
const groundRow = GRID_HEIGHT - 1; // Row for the ground
for (let col = 0; col < GRID_WIDTH; col++) {
// Introduce holes in the ground
if (Math.random() < HOLE_CHANCE) {
grid[groundRow][col] = null; // Introduce a hole
} else {
grid[groundRow][col] = new Ground(); // Filling with ground object
}
}
// Synchronization mechanism to update object positions
function syncUpdatePositions() {
// Update position of platforms
for (let row = 0; row < GRID_HEIGHT; row++) {
for (let col = 0; col < GRID_WIDTH; col++) {
const obj = grid[row][col];
if (obj instanceof Platform) {
obj.col -= SCROLL_SPEED / GRID_SIZE; // Move left
// Reset platforms off-screen
if (obj.col + obj.width < 0) {
obj.col = GRID_WIDTH; // Reset to the right edge
}
}
}
}
}
// Render function for game objects
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear previous frame
// Draw ground (always the last row)
for (let col = 0; col < GRID_WIDTH; col++) {
// If there's a hole, leave it open
if (grid[groundRow][col] === null) {
ctx.clearRect(col * GRID_SIZE, groundRow * GRID_SIZE, GRID_SIZE, GRID_SIZE); // Clear area for holes
} else {
ctx.fillStyle = "#8B4513"; // Ground color
ctx.fillRect(col * GRID_SIZE, groundRow * GRID_SIZE, GRID_SIZE, GRID_SIZE); // Draw ground
}
}
// Draw platforms
for (let row = 0; row < GRID_HEIGHT; row++) {
for (let col = 0; col < GRID_WIDTH; col++) {
const cell = grid[row][col];
if (cell instanceof Platform) {
const position = cell.getPixelPosition();
ctx.fillStyle = cell.color; // Set platform color
ctx.fillRect(position.x, position.y, position.width, position.height); // Draw platform
}
}
}
}
// Main game loop for continuous updating and rendering
function gameLoop() {
syncUpdatePositions(); // Update platform positions
render(); // Render platforms and the ground layer
requestAnimationFrame(gameLoop); // Request next frame
}
// Start the game
gameLoop(); // Begin the game loop
</script>
</body>
</html>
Key Changes:
- The ground is rendered without moving, and any logic in
syncUpdatePositions now only affects the platforms.
- Holes in the ground are still rendered correctly and do not get affected when the platforms scroll.
This will give you a fixed ground surface while the platforms can still scroll left across the screen. If you have further changes or features you'd like to implement, please let me know!