Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Player {
}

class Platform {
constructor() {
constructor({ x, y }) {
this.position = {
x: 200,
y: 325
x,
y
}
this.width = 200
this.height = 20
Expand All @@ -53,7 +53,15 @@ class Platform {
}

const player = new Player()
const platform = new Platform()
const platforms = [
new Platform({
x: 200,
y: 100
}),
new Platform({
x: 500,
y: 200
})]

const keys = {
right: {
Expand All @@ -68,25 +76,35 @@ function animate() {
requestAnimationFrame(animate)
c.clearRect(0, 0, canvas.width, canvas.height)
player.update()
platform.draw()
platforms.forEach((platform) => {
platforms.draw()
})

if (keys.right.pressed && player.position.x < 600) {
player.velocity.x = 5
} else if (keys.left.pressed && player.position.x > 100) {
player.velocity.x = -5
} else {
player.velocity.x = 0

if (keys.right.pressed) {
platform.position.x -= 5
}if (keys.left.pressed) {
platform.position.x += 5
platforms.forEach((platform) => {
platforms.position.x -= 5
})

}else if (keys.left.pressed) {
platforms.forEach((platform) => {
platforms.position.x += 5
})

}
}
// Platform Collision Logic
if(player.position.y + player.height <= platform.position.y && player.position.y + player.height + player.velocity.y >= platform.position.y && player.position.x + player.width >= platform.position.x && player.position.x && player.position.x <= platform.position.x + platform.width) {
player.velocity.y = 0
}
platforms.forEach((platform) => {
if (
player.position.y + player.height <= platform.position.y && player.position.y + player.height + player.velocity.y >= platform.position.y && player.position.x + player.width >= platform.position.x && player.position.x && player.position.x <= platform.position.x + platform.width) {
player.velocity.y = 0
}
})
}

animate()
Expand Down