it seems that the gravity is reversed here. check collision mechanismus systema. player…
- User
it seems that the gravity is reversed here. check collision mechanismus systema. player should be able to jump on platforms and stay on platforms normally as in Mario platformer game. each obstacle-platform should be as impenetrable wall in dependence of its current size. output only functions to add or fix, without full original code.: const canvas=document.getElementById("game-canvas");const context=canvas.getContext("2d");canvas.width=640;canvas.height=480;const GRAVITY=80;const ENEMY_RADIUS=10;const PLAYER_RADIUS=10;const TRACTION=.9;const OBSTACLE_COUNT=50;const OBSTACLE_GAP=0;let playerX=1e3;let playerY=100;let isPlayerAlive=true;let enemyX=1e3;let enemyY=100;let enemySpeedX=0;let enemySpeedY=0;let enemyMultiplier=0;let playerSpeedX=0;let playerSpeedY=0;let playerIsOnGround=false;let lasers=[];let shootingTimer=0;let obstacles=[];const obstacleWidth=100;const obstacleHeight=5;let offsetX=0;let offsetY=0;let scrollSpeed=1;function applyGravity(){if(!playerIsOnGround){playerSpeedY+=GRAVITY/60}}function applyTraction(){playerSpeedX*=TRACTION}function movePlayer(){if(!isPlayerAlive)return;playerX+=playerSpeedX;playerY+=playerSpeedY;if(playerX<0){playerX=0}if(playerX+PLAYER_RADIUS2>canvas.width){playerX=canvas.width-PLAYER_RADIUS2}context.fillStyle="pink";context.fillRect(playerX-offsetX,playerY-offsetY,PLAYER_RADIUS2,PLAYER_RADIUS2)}if(playerX<-PLAYER_RADIUS){playerX=canvas.width-PLAYER_RADIUS;generateNewObstacle()}if(playerX>canvas.width-PLAYER_RADIUS){playerX=-PLAYER_RADIUS;generateNewObstacle()}function moveEnemy(){if(!isPlayerAlive)return;const targetIndex=findNearestObstacle(playerX,playerY);const targetX=obstacles[targetIndex].x+obstacleWidth/2;const targetY=obstacles[targetIndex].y+obstacleHeight/2;const epsilon=.2;if(Math.abs(enemyX-targetX)>epsilon||Math.abs(enemyY-targetY)>epsilon){const angle=Math.atan2(targetY-enemyY,targetX-enemyX);enemySpeedX=Math.cos(angle)2;enemySpeedY=Math.sin(angle)2}else{enemySpeedX=0;enemySpeedY=0}enemyX+=enemySpeedX;enemyY+=enemySpeedY;context.fillStyle="cyan";context.fillRect(enemyX-ENEMY_RADIUS-offsetX,enemyY-ENEMY_RADIUS,ENEMY_RADIUS2,ENEMY_RADIUS2)}function findNearestObstacle(x,y){let minIndex=0;let minDistance=Number.MAX_VALUE;for(let i=0;i<obstacles.length;i++){const dx=x-(obstacles[i].x+obstacleWidth/2);const dy=y-(obstacles[i].y+obstacleHeight/2);const distance=Math.sqrt(dxdx+dydy);if(distance<minDistance){minDistance=distance;minIndex=i}}return minIndex}function isColliding(x,y,width,height){let collisionDetected=false;let isPlayerOnTop=false;let nearestObstacle=null;let minDistance=Number.MAX_VALUE;for(const obstacle of obstacles){if(obstacle&&x<obstacle.x+obstacle.width&&x+width>obstacle.x&&y<obstacle.y+obstacle.height&&y+height>obstacle.y){const dx=Math.max(0,Math.min(x+width,obstacle.x+obstacle.width)-Math.max(x,obstacle.x));const dy=Math.max(0,Math.min(y+height,obstacle.y+obstacle.height)-Math.max(y,obstacle.y));const distance=Math.sqrt(dxdx+dydy);if(distance<minDistance){minDistance=distance;nearestObstacle=obstacle}}}if(nearestObstacle){const dx=Math.max(0,Math.min(x+width,nearestObstacle.x+nearestObstacle.width)-Math.max(x,nearestObstacle.x));const dy=Math.max(0,Math.min(y+height,nearestObstacle.y+nearestObstacle.height)-Math.max(y,nearestObstacle.y));const distance=Math.sqrt(dxdy);if(distance<PLAYER_RADIUS){if(dy>dx&&x+width/2>nearestObstacle.x&&x+width/2<nearestObstacle.x+nearestObstacle.width){playerY=nearestObstacle.y-PLAYER_RADIUS2}else{playerX=nearestObstacle.x+nearestObstacle.width}playerSpeedX=0}collisionDetected=true}}if(!isPlayerOnTop){playerIsOnGround=false}return collisionDetected}function checkCollision(){if(isColliding(playerX,playerY,PLAYER_RADIUS2)){playerY-=playerSpeedY;offsetY-=playerSpeedY;playerSpeedY=0;playerIsOnGround=true}else{playerIsOnGround=false}}function detectObstacleCollision(){for(const obstacle of obstacles){if(playerX+PLAYER_RADIUS2>obstacle.y&&playerY<obstacle.y+obstacle.height){if(playerX+PLAYER_RADIUS2}else if(playerX>obstacle.x+obstacle.width-5){playerX=obstacle.x+obstacle.width}else{if(playerY+PLAYER_RADIUS2;playerIsOnGround=true}if(playerY>obstacle.y+obstacle.height-5){playerY=obstacle.y+obstacle.height;playerSpeedY=0}}}if(enemyX+ENEMY_RADIUS2>obstacle.y&&enemyY<obstacle.y+obstacle.height){if(enemyX+ENEMY_RADIUS2}else if(enemyX>obstacle.x+obstacle.width-5){enemyX=obstacle.x+obstacle.width}else{if(enemyY+ENEMY_RADIUS2}else if(enemyY>obstacle.y+obstacle.height-5){enemyY=obstacle.y+obstacle.height;enemySpeedY=0}}}}}function shootLaser(){const laserSpeed=2;const angle=Math.atan2(playerX-enemyX);const laserSpeedX=Math.cos(angle)2){const randomColor="rgb("+Math.floor(Math.random()(canvas.width-obstacleWidth);const obstacleY=i*(canvas.height/OBSTACLE_COUNT)+OBSTACLE_GAP;obstacles.push({x:obstacleX,y:obstacleY,width:obstacleWidth,height:obstacleHeight})}}function setPlayerStartPosition(){const startingObstacle=obstacles[0];playerX=startingObstacle.x+obstacleWidth/2-PLAYER_RADIUS;playerY=startingObstacle.y-PLAYER_RADIUS(canvas.width-obstacleWidth);const obstacleY=obstacles.length>0?obstacles[obstacles.length-1].y-canvas.height/OBSTACLE_COUNT-OBSTACLE_GAP:canvas.height;obstacles.push({x:obstacleX,y:obstacleY,width:obstacleWidth,height:obstacleHeight})}function checkEnemyDeath(){if(isPlayerAlive&&playerSpeedX>=0&&playerY+PLAYER_RADIUS2>enemyX-ENEMY_RADIUS&&playerX<enemyX+ENEMY_RADIUS){playerIsOnGround=false;playerSpeedY=-20;setTimeout(()=>{playerSpeedY=0},100);enemyX=-100;setTimeout(spawnNewEnemy,1e3);enemyMultiplier++}}function spawnNewEnemy(){enemyX=Math.random()2);enemyY=Math.random()2);for(let i=0;i<enemyMultiplier-10;i++){shootLaser()}}function draw(timestamp){context.fillStyle="black";context.fillRect(0,0,canvas.width,canvas.height);const elapsedSeconds=(timestamp-lastFrameTime)/1e3;lastFrameTime=timestamp;applyGravity(elapsedSeconds);applyTraction();movePlayer();moveEnemy();moveLasers();detectObstacleCollision();drawLasers();drawObstacles();checkEnemyDeath();if(playerY<offsetY+PLAYER_RADIUS2&&playerX<offsetX+obstacleWidth+PLAYER_RADIUS*2){offsetY-=canvas.height/OBSTACLE_COUNT;playerY+=canvas.height/OBSTACLE_COUNT;enemyY+=canvas.height/OBSTACLE_COUNT;generateNewObstacle()}requestAnimationFrame(draw)}let lastFrameTime=0;createObstacles();setPlayerStartPosition();draw();document.addEventListener("keydown",event=>{if(event.code==="KeyA"){playerSpeedX=-5}if(event.code==="KeyD"){playerSpeedX=5}if(event.code==="KeyW"&&playerIsOnGround){playerSpeedY=-20;playerIsOnGround=false}});document.addEventListener("keyup",event=>{if(event.code==="KeyA"||event.code==="KeyD"){playerSpeedX=0}});
- Assistant
To ensure that the player and enemy jump on platforms and stay on platforms normally, we need to switch the gravity direction and make some changes to the collision mechanism. Here's the modified code considering the fact that the gravity is reversed:
// Change the gravity value to a negative value const GRAVITY = -80;
// Inside the function isColliding // Change the playerY adjustment playerY = nearestObstacle.y + PLAYER_RADIUS * 2;
// Inside the function detectObstacleCollision // Change the playerY adjustment when player is above an obstacle playerY = obstacle.y + PLAYER_RADIUS * 2 + obstacle.height;
// Change the enemyY adjustment when the enemy is above an obstacle enemyY = obstacle.y + ENEMY_RADIUS * 2 + obstacle.height;
With these changes, the gravity will be reversed, and the player and enemy will be able to jump on platforms and stay on platforms as normal.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.