-
-
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
Frogger-game #204
Merged
Merged
Frogger-game #204
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,141 @@ | ||
const EDGE_POSITION = { | ||
rightX: 404, | ||
leftX: 0, | ||
topY: -15, | ||
bottomY: 390, | ||
}; | ||
|
||
const PLAYER_START_POSITION = { | ||
x: 202, | ||
y: 390, | ||
}; | ||
|
||
const MOVE_STEP = { | ||
axisX: 101, | ||
axisY: 81, | ||
}; | ||
|
||
const GAME_BOARD_WIDTH = 505; | ||
|
||
class Character { | ||
constructor(x, y, sprite) { | ||
this.x = x; | ||
this.y = y; | ||
this.sprite = sprite; | ||
} | ||
|
||
render() { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); | ||
} | ||
} | ||
|
||
class Enemy extends Character { | ||
constructor(x, y, speed, direction, sprite, player) { | ||
super(x, y, sprite); | ||
this.speed = speed; | ||
this.direction = direction; | ||
this.player = player; | ||
} | ||
|
||
update(dt) { | ||
if (this.direction === "left") { | ||
this.x -= dt * this.speed; | ||
if (this.x < 0) { | ||
this.x = GAME_BOARD_WIDTH + 10; | ||
} | ||
} else { | ||
this.x += dt * this.speed; | ||
if (this.x > GAME_BOARD_WIDTH) { | ||
this.x = -10; | ||
} | ||
} | ||
} | ||
|
||
checkOverlap() { | ||
if ( | ||
this.player.x < this.x + 60 && | ||
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. What is |
||
this.player.x + 60 > this.x && | ||
this.player.y < this.y + 60 && | ||
this.player.y + 60 > this.y | ||
) { | ||
this.player.x = PLAYER_START_POSITION.x; | ||
this.player.y = PLAYER_START_POSITION.y; | ||
} | ||
} | ||
} | ||
|
||
class Player extends Character { | ||
constructor(x, y, sprite) { | ||
super(x, y, sprite); | ||
this.sprite = "images/char-boy.png"; | ||
} | ||
|
||
update() { | ||
if (this.y === EDGE_POSITION.topY) { | ||
setTimeout(() => { | ||
this.x = PLAYER_START_POSITION.x; | ||
this.y = PLAYER_START_POSITION.y; | ||
}, 1000); | ||
} | ||
} | ||
|
||
handleInput(btn) { | ||
console.log("handleInput method"); | ||
if (btn === "up" && this.y !== EDGE_POSITION.topY) { | ||
this.y -= MOVE_STEP.axisY; | ||
} else if (btn === "down" && this.y !== EDGE_POSITION.bottomY) { | ||
this.y += MOVE_STEP.axisY; | ||
} else if (btn === "left" && this.x !== EDGE_POSITION.leftX) { | ||
this.x -= MOVE_STEP.axisX; | ||
} else if (btn === "right" && this.x !== EDGE_POSITION.rightX) { | ||
this.x += MOVE_STEP.axisX; | ||
} | ||
} | ||
} | ||
|
||
const enemiesSettings = [ | ||
{ | ||
posX: -10, | ||
posY: 55, | ||
speed: generateSpeed(200, 300), | ||
direction: "right", | ||
img: "images/enemy-bug.png", | ||
}, | ||
{ | ||
posX: GAME_BOARD_WIDTH + 10, | ||
posY: 136, | ||
speed: generateSpeed(100, 300), | ||
direction: "left", | ||
img: "images/enemy-bug-reverse.png", | ||
}, | ||
{ | ||
posX: -5, | ||
posY: 217, | ||
speed: generateSpeed(100, 300), | ||
direction: "right", | ||
img: "images/enemy-bug.png", | ||
}, | ||
]; | ||
|
||
const player = new Player(PLAYER_START_POSITION.x, PLAYER_START_POSITION.y); | ||
|
||
const allEnemies = enemiesSettings.map( | ||
({ posX, posY, speed, direction, img }) => { | ||
return new Enemy(posX, posY, speed, direction, img, player); | ||
} | ||
); | ||
|
||
document.addEventListener("keyup", function (e) { | ||
var allowedKeys = { | ||
37: "left", | ||
38: "up", | ||
39: "right", | ||
40: "down", | ||
}; | ||
|
||
player.handleInput(allowedKeys[e.keyCode]); | ||
}); | ||
|
||
function generateSpeed(min, max) { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
} |
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.
WHat is
10
here and across the codebase? I guess-10
is the same but just negative. If true then havingOFFSET = 10
and referring to it asOFFSET
and-OFFSET
would be fine.Of course
OFFSET
word doesn't explain it application, so invent a better name.