-
-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My PR Classic Frogger Game #516
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
const gameProperties = { | ||
cellHeight: 83, | ||
cellWidth: 100, | ||
playerXStart: 200, | ||
playerYStart: 383, | ||
up: 0, | ||
down: 383, | ||
left: 0, | ||
right: 400, | ||
collisionX: 50, | ||
collisionY: 40, | ||
frontierForEnemiesLeft: -150, | ||
frontierForEnemiesRight: 500, | ||
startYForCreateEnemy: 50, | ||
stepYForCreateEnemy: 90, | ||
increaseSpeed: 70, | ||
}; | ||
|
||
class Game { | ||
constructor({ | ||
cellHeight, | ||
cellWidth, | ||
playerXStart, | ||
playerYStart, | ||
up, | ||
down, | ||
left, | ||
right, | ||
collisionX, | ||
collisionY, | ||
frontierForEnemiesLeft, | ||
frontierForEnemiesRight, | ||
startYForCreateEnemy, | ||
stepYForCreateEnemy, | ||
increaseSpeed, | ||
}) { | ||
this.level = 1; | ||
this.isStop = false; | ||
this.isGameOver = false; | ||
this.CELL_HEIGHT = cellHeight; | ||
this.CELL_WIDTH = cellWidth; | ||
this.PLAYER_X_START = playerXStart; | ||
this.PLAYER_Y_START = playerYStart; | ||
this.up = up; | ||
this.down = down; | ||
this.left = left; | ||
this.right = right; | ||
this.collisionX = collisionX; | ||
this.collisionY = collisionY; | ||
this.frontierForEnemiesLeft = frontierForEnemiesLeft; | ||
this.frontierForEnemiesRight = frontierForEnemiesRight; | ||
this.startYForCreateEnemy = startYForCreateEnemy; | ||
this.stepYForCreateEnemy = stepYForCreateEnemy; | ||
this.increaseSpeed = increaseSpeed; | ||
} | ||
} | ||
|
||
class Character { | ||
constructor(x, y, game) { | ||
this.x = x; | ||
this.y = y; | ||
this.game = game; | ||
} | ||
} | ||
|
||
class Enemy extends Character { | ||
constructor({ x, y, speed, game, player }) { | ||
super(x, y, game); | ||
this.sprite = "images/enemy-bug.png"; | ||
this.speed = speed; | ||
this.player = player; | ||
} | ||
|
||
update(dt) { | ||
this.x += | ||
dt * | ||
(this.speed + 0.4 * this.speed * this.game.level) * | ||
!this.game.isStop; | ||
if (this.x > this.game.frontierForEnemiesRight) { | ||
this.x = this.game.frontierForEnemiesLeft; | ||
} | ||
if (this.isCollision()) { | ||
this.player.start(); | ||
this.game.level = 1; | ||
} | ||
} | ||
|
||
isCollision() { | ||
return ( | ||
this.x > this.player.x - this.game.collisionX && | ||
this.x < this.player.x + this.game.collisionX && | ||
this.y > this.player.y - this.game.collisionY && | ||
this.y < this.player.y + this.game.collisionY | ||
); | ||
} | ||
|
||
render() { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); | ||
} | ||
} | ||
|
||
class Player extends Character { | ||
constructor(game) { | ||
super(1, 1, game); | ||
this.sprite = "images/char-boy.png"; | ||
this.x = this.game.PLAYER_X_START; | ||
this.y = this.game.PLAYER_Y_START; | ||
} | ||
|
||
handleInput(key) { | ||
switch (key) { | ||
case "up": | ||
if (this.y > this.game.up) { | ||
this.y -= this.game.CELL_HEIGHT; | ||
} | ||
break; | ||
case "down": | ||
if (this.y < this.game.down) { | ||
this.y += this.game.CELL_HEIGHT; | ||
} | ||
break; | ||
case "left": | ||
if (this.x > this.game.left) { | ||
this.x -= this.game.CELL_WIDTH; | ||
} | ||
break; | ||
case "right": | ||
if (this.x < this.game.right) { | ||
this.x += this.game.CELL_WIDTH; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
if (this.isWin()) { | ||
this.renderLevel(); | ||
} | ||
} | ||
|
||
renderLevel() { | ||
const body = document.querySelector(".message"); | ||
let div = `<div class="class__modal">You LEVEL${this.game.level}!</div>`; | ||
this.game.isStop = true; | ||
this.game.level++; | ||
this.start(); | ||
document.removeEventListener("keyup", playerMove); | ||
body.innerHTML = div; | ||
setTimeout(() => { | ||
this.game.isStop = false; | ||
document.addEventListener("keyup", playerMove); | ||
body.innerHTML = ""; | ||
}, 1500); | ||
} | ||
|
||
isWin() { | ||
return this.y <= 0; | ||
} | ||
|
||
update() {} | ||
start() { | ||
this.x = this.game.PLAYER_X_START; | ||
this.y = this.game.PLAYER_Y_START; | ||
} | ||
render() { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); | ||
} | ||
} | ||
|
||
const game = new Game(gameProperties); | ||
const player = new Player(game); | ||
|
||
const createEnemy = (numberOfEnemies) => { | ||
const arrOfEnemies = []; | ||
for (let i = 0; i < numberOfEnemies; i++) { | ||
arrOfEnemies.push(i); | ||
} | ||
return arrOfEnemies; | ||
}; | ||
|
||
const allEnemies = createEnemy(3).map((number) => { | ||
return new Enemy({ | ||
x: 0, | ||
y: game.startYForCreateEnemy + number * game.stepYForCreateEnemy, | ||
speed: game.increaseSpeed + number * game.increaseSpeed, | ||
game: game, | ||
player: player, | ||
Comment on lines
+185
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is legal shortcut to create object properties from variables where key will have the name same as the variable in use.
|
||
}); | ||
}); | ||
|
||
function playerMove(e) { | ||
const allowedKeys = { | ||
37: "left", | ||
38: "up", | ||
39: "right", | ||
40: "down", | ||
}; | ||
player.handleInput(allowedKeys[e.keyCode]); | ||
} | ||
|
||
document.addEventListener("keyup", playerMove); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bonus: one of the methods to create an array knowing its length and pre-fill it with some values based on index is
Array.from({length: numberOfEnemies}, (_, index) => index)
Prepend with
return
and call it a day.Still worth having a function for this one-liner as function provides a semantic meaning to the action.