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

JS OOP Frogger Game Task #450

Merged
merged 8 commits into from
Oct 9, 2022
Merged
Changes from 4 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
134 changes: 134 additions & 0 deletions submissions/franchukv/js-oop-frogger/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const CANVAS_HEIGHT = 606;
const CANVAS_WIDTH = 505;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like there is correlation between tile width and canvas dimensions. Let computer do the calcs.

const TILE_HEIGHT = 83;
const TILE_WIDTH = 101;

const PLAYER_CONF = {
size: {
height: 40,
width: 40
},
position: {
x: 202,
Copy link
Member

Choose a reason for hiding this comment

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

You could calculate this from TILE_WIDTH programmatically. So whenever tile width changes, this param changes accordingly and automagically.

y: 380
}
}

const EMENIES_CONF = {
size: {
height: 40,
width: 80
Copy link
Member

Choose a reason for hiding this comment

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

Any correlation with -80?

},
enemy: [{
x: 0,
y: 55,
speed: 280
},
{
x: 0,
y: 135,
speed: 150
},
{
x: 0,
y: 220,
Copy link
Member

Choose a reason for hiding this comment

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

Can y be calculated from row number?

speed: 200
}]
}

const Player = function ({ position, size }) {
this.sprite = 'images/char-boy.png';
this.x = position.x;
this.y = position.y;
this.height = size.height;
this.width = size.width;
};

Player.prototype.resetPlayerPosition = function ({ position }) {
this.x = position.x;
this.y = position.y;
};

Player.prototype.handleInput = function (key) {
switch (key) {
case 'up':
if (this.y > 0) this.y -= TILE_HEIGHT;
break;
case 'down':
if (this.y <= CANVAS_HEIGHT - TILE_HEIGHT * 3) this.y += TILE_HEIGHT;
break;
case 'left':
if (this.x > 0) this.x -= TILE_WIDTH;
break;
case 'right':
if (this.x < CANVAS_WIDTH - TILE_WIDTH) this.x += TILE_WIDTH;
break;
default:
break;
}
};

Player.prototype.update = function () {
if (this.y < 0) {
this.resetPlayerPosition(PLAYER_CONF);
}
};

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

const Enemy = function ({ size }, enemy, player) {
Copy link
Member

Choose a reason for hiding this comment

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

Function declaration says that the function expects and enemy as one of the parameters.
Is it actually an instance of class Enemy expected here?

Copy link
Contributor Author

@franchukv franchukv Oct 3, 2022

Choose a reason for hiding this comment

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

If I understood the question right, the answer will be: actually no, we expect basis properties of 'enemy' for creating an instance of class Enemy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, renamed.

this.sprite = 'images/enemy-bug.png';
this.height = size.height;
this.width = size.width;
this.x = enemy.x;
this.y = enemy.y;
this.speed = enemy.speed;
this.player = player;
};

Enemy.prototype.checkCollision = function () {
if (
this.player.y > this.y - this.height &&
this.player.y - this.player.height < this.y &&
this.player.x + this.player.width > this.x &&
this.player.x < this.x + this.width
) {

this.player.resetPlayerPosition(PLAYER_CONF);
}
};

Enemy.prototype.update = function (dt) {
this.checkCollision();

if (this.x >= CANVAS_WIDTH) {
this.x = 0;
}

this.x += this.speed * dt;
};

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

const player = new Player(PLAYER_CONF);
const allEnemies = [
new Enemy(EMENIES_CONF, EMENIES_CONF.enemy[0], player),
new Enemy(EMENIES_CONF, EMENIES_CONF.enemy[1], player),
new Enemy(EMENIES_CONF, EMENIES_CONF.enemy[2], player),
];
Copy link
Member

Choose a reason for hiding this comment

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

As a peer developer I am tasked to add 20 more enemies.
Adding 20 more nearly identical rows of code is a no-go.
Can we make this code DRYer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can use loop into 'addEnemiesToInitArray' function to add instance of enemies from 'ENEMIES CONF.enemy' to 'allEnemies'.

Copy link
Member

Choose a reason for hiding this comment

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

Let's make it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!



document.addEventListener('keyup', function (e) {
var allowedKeys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};

player.handleInput(allowedKeys[e.keyCode]);
});