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

Frogger-game #204

Merged
merged 5 commits into from
Aug 21, 2022
Merged
Changes from 3 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
141 changes: 141 additions & 0 deletions submissions/zhenyakornilov/frogger-game/app.js
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;
Copy link
Member

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 having OFFSET = 10 and referring to it as OFFSET and -OFFSET would be fine.
Of course OFFSET word doesn't explain it application, so invent a better name.

}
} else {
this.x += dt * this.speed;
if (this.x > GAME_BOARD_WIDTH) {
this.x = -10;
}
}
}

checkOverlap() {
if (
this.player.x < this.x + 60 &&
Copy link
Member

Choose a reason for hiding this comment

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

What is 60?

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);
}