add some floor obstacle and make canvas move up as in tower, with constantly new…
- User
add some floor obstacle and make canvas move up as in tower, with constantly new obstacles appearing as platforms to jump up.: const canvas=document.getElementById("game-canvas");const context=canvas.getContext("2d");canvas.width=480;canvas.height=320;const GRAVITY=60;const ENEMY_RADIUS=10;const PLAYER_RADIUS=10;const TRACTION=.999;const OBSTACLE_COUNT=6;let playerX=50;let playerY=250;let isPlayerAlive=true;let enemyX=400;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=320;const obstacleHeight=5;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,playerY,PLAYER_RADIUS2,PLAYER_RADIUS2)}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;if(shootingTimer<=0){shootLaser();shootingTimer=1}shootingTimer-=1e-4;context.fillStyle="cyan";context.fillRect(enemyX-ENEMY_RADIUS,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){for(const obstacle of obstacles){if(x<obstacle.x+obstacle.width&&x+width>obstacle.x&&y<obstacle.y+obstacle.height&&y+height>obstacle.y){return true}}return false}function checkCollision(){if(isColliding(playerX,playerY,PLAYER_RADIUS2,PLAYER_RADIUS2)){if(playerSpeedY>0){playerY-=playerSpeedY;playerSpeedY=0;playerIsOnGround=true}else{playerY-=playerSpeedY;playerSpeedY=0}}else{playerIsOnGround=false}if(isColliding(enemyX-ENEMY_RADIUS,enemyY-ENEMY_RADIUS,ENEMY_RADIUS2,ENEMY_RADIUS2)){enemySpeedX=-enemySpeedX;enemySpeedY=-enemySpeedY;enemyX+=enemySpeedX;enemyY+=enemySpeedY}}function detectObstacleCollision(){for(const obstacle of obstacles){if(playerX+PLAYER_RADIUS2>obstacle.x&&playerX<obstacle.x+obstacle.width&&playerY+PLAYER_RADIUS2>obstacle.y&&playerY<obstacle.y+obstacle.height){if(playerX+PLAYER_RADIUS2<obstacle.x+5){playerX=obstacle.x-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(playerY-enemyY,playerX-enemyX);const laserSpeedX=Math.cos(angle)2){const randomColor="rgb("+Math.floor(Math.random()(canvas.height-obstacleHeight);obstacles.push({x:obstacleX,y:obstacleY,width:obstacleWidth,height:obstacleHeight})}}function checkEnemyDeath(){if(isPlayerAlive&&playerSpeedY>=0&&playerY+PLAYER_RADIUS2>enemyX-ENEMY_RADIUS&&playerX<enemyX+ENEMY_RADIUS){playerSpeedY=-20;playerIsOnGround=false;enemyY=-100;enemyX=-100;setTimeout(spawnNewEnemy,1e3);enemyMultiplier++}}function spawnNewEnemy(){enemyX=Math.random()2);enemyY=Math.random()2);for(let i=0;i<enemyMultiplier-1;i++){shootLaser()}}function draw(){context.fillStyle="black";context.fillRect(0,0,canvas.width,canvas.height);applyGravity();applyTraction();movePlayer();moveEnemy();moveLasers();checkCollision();drawLasers();drawObstacles();checkEnemyDeath();detectObstacleCollision();requestAnimationFrame(draw)}createObstacles();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
- User
where how to change direction canvas moving up or down?
- Assistant
You can change the direction the canvas moves (up or down) by modifying the
offsetYvalue in thedraw()function. If you want to move the canvas down, you need to increase theoffsetYvalue, and if you want to move it up, you have to decrease theoffsetYvalue.Here is an example of how you can change the direction and speed of canvas movement:
- Add a new variable
scrollSpeed:
let scrollSpeed = 1; // Move canvas up by default, use a negative value for moving down- Modify the
draw()function to apply thescrollSpeedto theoffsetYvalue:
function draw() { context.fillStyle = "black"; context.fillRect(0, 0, canvas.width, canvas.height); applyGravity(); applyTraction(); movePlayer(); moveEnemy(); moveLasers(); checkCollision(); drawLasers(); drawObstacles(); checkEnemyDeath(); offsetY += scrollSpeed; // Change this line to apply the scrollSpeed to offsetY // Detect if player has reached a new obstacle if (playerY < offsetY + PLAYER_RADIUS * 2) { offsetY -= canvas.height / OBSTACLE_COUNT; playerY += canvas.height / OBSTACLE_COUNT; enemyY += canvas.height / OBSTACLE_COUNT; generateNewObstacle(); } detectObstacleCollision(); requestAnimationFrame(draw); } - Add a new variable
- User
need to make player spawn on some platform, because it drops down from the beginning, which makes no sense.
- Assistant
To spawn the player on a platform at the beginning of the game, you can modify the initial
playerXandplayerYvalues to match the position of the first platform. You can do this by updating theplayerYvalue based on the starting position of the first platform in theobstaclesarray after creating the initial obstacles.Here's how you can modify the code to spawn the player on a starting platform:
- Create a function to set the player's starting position:
function setPlayerStartPosition() { const startingObstacle = obstacles[0]; playerX = startingObstacle.x + obstacleWidth / 2 - PLAYER_RADIUS; playerY = startingObstacle.y - PLAYER_RADIUS * 2; }- Call the
setPlayerStartPosition()function after creating the initial obstacles:
createObstacles(); setPlayerStartPosition(); draw();Now, the player will spawn on top of the first platform at the beginning of the game. You can change the platform the player spawns on by modifying the index in the array used in the function. For example, if you want the player to spawn on the second platform, replace with .
- User
when you tell "Create a function" you need to specify place where to drop this function.
- Assistant
Apologies for the confusion. You can place the
setPlayerStartPosition()function right after thecreateObstacles()function definition. Here's the complete code with the new function placement:// ... (previous code) function createObstacles() { for (let i = 0; i < OBSTACLE_COUNT; i++) { const obstacleX = 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 * 2; } // ... (remaining code) createObstacles(); setPlayerStartPosition(); draw();Now, the
setPlayerStartPosition()function is defined right after thecreateObstacles()function, and it will be called after the initial obstacles are created to set the player's starting position.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.