Skip to content

Commit

Permalink
Classic Frogger Game (#594)
Browse files Browse the repository at this point in the history
* minimum viable product

* correcter switch case and added ;-s

* added dimention constants

* consts added + Player.prototype

* const

* collision separated

* Character ancestor-class added

* added player passed as an attr

* ;-s checked

* sprite variables added

* [1,2,3].map used

* [1,2,3].map corrected

* player as arg to class Enemy

* bugs removed

* this.player
  • Loading branch information
YuraZagor authored Oct 5, 2022
1 parent c2648c4 commit ac6b2a3
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions submissions/YuraZagor/Frogger/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const allEnemies = [];
const speedLow = 200;
const speedMedium = 275;
const speedHigh = 350;
const allSpeeds = [speedLow, speedMedium, speedHigh];
const enemyRestartX = -55;
const collisionOffset = 40;
const cellX = 101;
const cellY = 83;
const spriteRepositioningY = -10;
const playerStartX = 2*cellX;
const playerStartY = 5*cellY + spriteRepositioningY ;
const fieldWidth = 5*cellX;
const fieldHight = 5*cellY;
const rangesY = [1, 2, 3].map(item => item*cellY+spriteRepositioningY);

function Character (x, y, sprite) {
this.x = x;
this.y = y;
this.sprite=sprite;
};

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

function Enemy(x, y, speedX, player) {
Character.call(this, x, y, 'images/enemy-bug.png');
this.speedX = speedX;
this.player = player;
};
Enemy.prototype = Object.create(Character.prototype);

Enemy.prototype.update = function(dt){
this.x += this.speedX * dt;
if (this.x > fieldWidth){
this.x = enemyRestartX;
this.speedX = allSpeeds[Math.floor(Math.random()*3)];
};
this.checkColision()
};
Enemy.prototype.checkColision = function(){
if (this.y === this.player.y) {

if ((this.x - this.player.x < collisionOffset ) && (this.player.x - this.x < collisionOffset )) {
this.player.x = playerStartX;
this.player.y = playerStartY;
};
};
};

function addBug() {
rangesY.forEach ( (levelMark)=> allEnemies
.push( new Enemy(0, levelMark, allSpeeds[Math.floor(Math.random()*3)], player))
)
};

function Player(x, y) {
Character.call(this, x, y, 'images/char-boy.png');
};

Player.prototype = Object.create(Character.prototype);

Player.prototype.update = function(dt){
};

Player.prototype.handleInput = function (direction) {
switch (direction) {
case 'left':
if (this.x > 0){
this.x -= cellX
};
break;
case 'up':
this.y -= cellY;
if(this.y < cellY + spriteRepositioningY){
this.x = playerStartX;
this.y = playerStartY;
}
break;
case 'right':
if (this.x < fieldWidth - cellX ){
this.x += cellX
};
break;
case 'down':
if (this.y < fieldHight + spriteRepositioningY ){
this.y += cellY
};
break;
default:
break;
};
};

document.addEventListener('keyup', function(e) {
const allowedKeys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
player.handleInput(allowedKeys[e.keyCode]);
});
const player = new Player ( playerStartX, playerStartY );

addBug()

0 comments on commit ac6b2a3

Please sign in to comment.