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

add frogger task #330

Merged
merged 1 commit into from
Sep 7, 2022
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
131 changes: 131 additions & 0 deletions submissions/pashapushak1996/frogger-game/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const enemiesConfig = {
TOP_POS: 51,
MIDDLE_POS: 141,
BOTTOM_POS: 226,
};
Comment on lines +1 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following would add to the readability.

const ENEMIES_CONFIG = {
    TOP_POS: 51,
    MIDDLE_POS: 141,
    BOTTOM_POS: 226,
};

When we see ALL_CAPS in code we instantly understand it is a sort of fundamental global constant.


const playerConfig = {
POS_X: 200,
POS_Y: 400
};

const canvasConfig = {
CELL_WIDTH: 101,
CELL_HEIGHT: 83,
HEIGHT: 600,
WIDTH: 500
};

const gameConfig = {
PADDING_TOP: 20,
PADDING_LEFT: 78,
MIN_SPEED: 100,
MAX_SPEED: 300
}

const Enemy = function (x, y, speed, player) {
this.x = x;
this.y = y;
this.speed = speed;
this.player = player;
this.sprite = 'images/enemy-bug.png';
};

Enemy.prototype.checkCollision = function () {
if (this.player) {
if (this.x + gameConfig.PADDING_LEFT > this.player.x &&
this.x - gameConfig.PADDING_LEFT < this.player.x &&
this.y - gameConfig.PADDING_TOP < this.player.y &&
this.y + gameConfig.PADDING_TOP > this.player.y
) {
alert(`You lose! Score: ${ player.score }`);

this.resetEnemyPosition();
this.player.resetPosition();
}
}
};

Enemy.prototype.resetEnemyPosition = function () {
this.x = -canvasConfig.CELL_WIDTH;
};

Enemy.prototype.update = function (dt) {
if (dt) {
this.x += dt * this.speed;
}

if (this.x >= canvasConfig.WIDTH) {
this.resetEnemyPosition();
}

this.checkCollision();
};

Enemy.prototype.render = function () {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};

const Player = function (x, y) {
this.x = x;
this.y = y;
this.score = 0;
this.sprite = 'images/char-boy.png';
};

Player.prototype = Enemy.prototype;

Player.prototype.handleInput = function (key) {
switch (key) {
case 'ArrowUp':
if (this.y > 0) {
this.y -= canvasConfig.CELL_HEIGHT;
}
if (this.y < 0) {
this.score++;

setTimeout(() => {
alert(`You win! Score: ${ this.score }`);
this.resetPosition();
}, 100);
}
break;
case 'ArrowDown':
if (this.y < playerConfig.POS_Y) {
this.y += canvasConfig.CELL_HEIGHT;
}
break;
case 'ArrowLeft':
if (this.x > 0) {
this.x -= canvasConfig.CELL_WIDTH;
}
break;
case 'ArrowRight':
if (this.x < canvasConfig.WIDTH - canvasConfig.CELL_WIDTH) {
this.x += canvasConfig.CELL_WIDTH;
}
break;
default:
break;

}
};

Player.prototype.resetPosition = function () {
this.x = playerConfig.POS_X;
this.y = playerConfig.POS_Y;
}

const player = new Player(playerConfig.POS_X, playerConfig.POS_Y);

const addEnemies = () => Object.values(enemiesConfig).map(pos => {
const randomSpeed = Math.random() * (gameConfig.MAX_SPEED - gameConfig.MIN_SPEED) + gameConfig.MIN_SPEED;

return new Enemy(-canvasConfig.CELL_WIDTH, pos, randomSpeed, player);
});

const allEnemies = addEnemies();

document.addEventListener('keyup', function ({ key }) {
player.handleInput(key);
});