-
-
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
JS OOP Frogger Game Task #450
Changes from 4 commits
aee3d8b
4c26565
569160f
44c939d
feb2b0b
dee91b1
74aafcc
ceb54fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const CANVAS_HEIGHT = 606; | ||
const CANVAS_WIDTH = 505; | ||
const TILE_HEIGHT = 83; | ||
const TILE_WIDTH = 101; | ||
|
||
const PLAYER_CONF = { | ||
size: { | ||
height: 40, | ||
width: 40 | ||
}, | ||
position: { | ||
x: 202, | ||
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. 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 | ||
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. Any correlation with |
||
}, | ||
enemy: [{ | ||
x: 0, | ||
y: 55, | ||
speed: 280 | ||
}, | ||
{ | ||
x: 0, | ||
y: 135, | ||
speed: 150 | ||
}, | ||
{ | ||
x: 0, | ||
y: 220, | ||
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. Can |
||
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) { | ||
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. Function declaration says that the function expects and enemy as one of the parameters. 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. 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. 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. 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), | ||
]; | ||
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. As a peer developer I am tasked to add 20 more enemies. 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. Yes, we can use loop into 'addEnemiesToInitArray' function to add instance of enemies from 'ENEMIES CONF.enemy' to 'allEnemies'. 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. Let's make it. 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. Done! |
||
|
||
|
||
document.addEventListener('keyup', function (e) { | ||
var allowedKeys = { | ||
37: 'left', | ||
38: 'up', | ||
39: 'right', | ||
40: 'down' | ||
}; | ||
|
||
player.handleInput(allowedKeys[e.keyCode]); | ||
}); |
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.
Looks like there is correlation between tile width and canvas dimensions. Let computer do the calcs.