rewrite the game for me to make it more responsible and smooth: import React, {…
- User
rewrite the game for me to make it more responsible and smooth:
import React, { useState, useEffect, useRef } from "react";
// Constants const GRAVITY = 980; // Pixels per second squared const JUMP_STRENGTH = -600; // Pixels per second const MOVE_SPEED = 500; // Pixels per second const PLAYER_WIDTH = 50; const PLAYER_HEIGHT = 50; const PLATFORM_WIDTH = 300; // Reduced width to make the game more challenging const PLATFORM_HEIGHT = 120; const VIEW_WIDTH = 800; const VIEW_HEIGHT = 600; // Increased height for more vertical space
// Platform component const Platform = ({ width, height, top, left }) => (
// Player component const Player = ({position}) => (
const lastTimeRef = useRef(performance.now()); const keysPressed = useRef({ left: false, right: false, jump: false }); // Create new platform to the right of the last platform const createNewPlatform = () => ({ width: PLATFORM_WIDTH, height: PLATFORM_HEIGHT, top: Math.max(PLATFORM_HEIGHT, Math.random() * (VIEW_HEIGHT - PLATFORM_HEIGHT - 10)), left: platforms[platforms.length - 1].left + PLATFORM_WIDTH + Math.random() * 400 + 200, }); // Check collision between player and platforms// Check collision between player and platforms const checkCollisionWithPlatforms = () => { const playerRect = { left: playerPosition.x - PLAYER_WIDTH / 2, top: playerPosition.y, right: playerPosition.x + PLAYER_WIDTH / 2, bottom: playerPosition.y + PLAYER_HEIGHT, };
for (let i = 0; i < platforms.length; i++) { const platform = platforms[i]; const platformRect = { left: platform.left, top: platform.top, right: platform.left + platform.width, bottom: platform.top + platform.height, }; if ( playerRect.right > platformRect.left && playerRect.left < platformRect.right && playerRect.top > platformRect.top && playerRect.top < platformRect.bottom ) { // Colliding with platform, return the platform return platform; } } // No collision with any platforms return null; }; // Game loop const gameLoop = () => { const now = performance.now(); const deltaTime = (now - lastTimeRef.current) / 1000.0; lastTimeRef.current = now;
// Check for collisions const collided = checkCollisionWithPlatforms(); // Update player velocity and position const newVelocity = { ...playerVelocity }; let newPositionX = playerPosition.x; if (keysPressed.current.left) { newPositionX -= MOVE_SPEED * deltaTime; } if (keysPressed.current.right) { newPositionX += MOVE_SPEED * deltaTime; } let newPositionY = playerPosition.y; if (!isGrounded) { newVelocity.y += GRAVITY * deltaTime; // Gravity } else { newVelocity.y = 0; } if (keysPressed.current.jump && isGrounded) { newVelocity.y = JUMP_STRENGTH; // Jump setIsGrounded(false); keysPressed.current.jump = false; // Prevent multi-jumping } newPositionY = Math.max(0, newPositionY + newVelocity.y * deltaTime);// Collision response if (collided) { setIsGrounded(true); newPositionY = collided.top - PLAYER_HEIGHT; // Now, the newPositionY is set to the top of the collided platform } else { setIsGrounded(false); }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.