Skip to content
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

limit hero movement inside canvas frame #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,31 @@ var reset = function () {
hero.y = canvas.height / 2;

// Throw the monster somewhere on the screen randomly
monster.x = 32 + (Math.random() * (canvas.width - 64));
monster.y = 32 + (Math.random() * (canvas.height - 64));
monster.x = 32 + (Math.random() * ((canvas.width - 32) - 65));
monster.y = 32 + (Math.random() * ((canvas.height - 32) - 68));
};

// Update game objects
var update = function (modifier) {
if (38 in keysDown) { // Player holding up
hero.y -= hero.speed * modifier;
if (hero.y > 32) {
hero.y -= hero.speed * modifier;
}
}
if (40 in keysDown) { // Player holding down
hero.y += hero.speed * modifier;
if (hero.y < canvas.height - 68) {
hero.y += hero.speed * modifier;
}
}
if (37 in keysDown) { // Player holding left
hero.x -= hero.speed * modifier;
if (hero.x > 32) {
hero.x -= hero.speed * modifier;
}
}
if (39 in keysDown) { // Player holding right
hero.x += hero.speed * modifier;
if (hero.x < canvas.width - 65) {
hero.x += hero.speed * modifier;
}
}

// Are they touching?
Expand Down