-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Classic Frogger Game #594
Merged
Merged
Classic Frogger Game #594
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
088a852
minimum viable product
YuraZagor a73ed68
correcter switch case and added ;-s
YuraZagor f268987
added dimention constants
YuraZagor 997b6a7
consts added + Player.prototype
YuraZagor d712661
const
YuraZagor a7371b2
collision separated
YuraZagor 7dc2542
Character ancestor-class added
YuraZagor e7e9026
added player passed as an attr
YuraZagor f9dfa54
;-s checked
YuraZagor 8a06ad1
sprite variables added
YuraZagor 740cb0d
[1,2,3].map used
YuraZagor 0ce02b9
[1,2,3].map corrected
YuraZagor 9bfab1d
player as arg to class Enemy
YuraZagor 26b074a
bugs removed
YuraZagor 20d126f
this.player
YuraZagor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
minimum viable product
- Loading branch information
commit 088a852d8d54f6243a7db7489dd3d20b378360ce
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
let allEnemies = [] | ||
let allSpeeds = [200, 250, 350] | ||
let rangeY = [73, 156, 239] | ||
|
||
function Player(x, y, sprite) { | ||
this.x = x; | ||
this.y = y; | ||
this.sprite=sprite; | ||
}; | ||
Player.prototype.render = function() { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y) | ||
}; | ||
Player.prototype.update = function() { | ||
}; | ||
let player = new Player ( 202, 405, 'images/char-boy.png' ) | ||
|
||
function Enemy(x, y, sprite, speedX) { | ||
Player.call(this, x, y, sprite); | ||
this.speedX = speedX; | ||
}; | ||
|
||
Enemy.prototype = Object.create(Player.prototype); | ||
|
||
Enemy.prototype.update = function(dt){ | ||
this.x += this.speedX * dt; | ||
if (this.x > 505){ | ||
this.x = -55 | ||
this.speedX = allSpeeds[Math.floor(Math.random()*3)] | ||
} | ||
if (this.y === player.y){ | ||
if ((this.x - player.x < 40 ) && (player.x - this.x < 40 )) { | ||
player.x = 202; | ||
player.y = 405; | ||
} | ||
|
||
} | ||
}; | ||
|
||
function addBug() { | ||
rangeY.forEach ( | ||
(levelMark)=> allEnemies.push( new Enemy(0, levelMark, 'images/enemy-bug.png', allSpeeds[Math.floor(Math.random()*3)] )) | ||
) | ||
}; | ||
|
||
console.log(player) | ||
player.handleInput = function (direction) { | ||
switch (direction) { | ||
case 'left': | ||
if (this.x > 0){ | ||
this.x -= 101 }; | ||
break; | ||
case 'up': | ||
this.y -= 83; | ||
if(this.y < 50){ | ||
this.x = 202; | ||
this.y = 405; | ||
} | ||
break; | ||
case 'right': | ||
if (this.x < 400){ | ||
this.x += 101}; | ||
break; | ||
case 'down': | ||
if (this.y < 400){ | ||
this.y += 83 }; | ||
break; | ||
default: | ||
break; | ||
}; | ||
}; | ||
|
||
document.addEventListener('keyup', function(e) { | ||
const allowedKeys = { | ||
37: 'left', | ||
38: 'up', | ||
39: 'right', | ||
40: 'down' | ||
}; | ||
player.handleInput(allowedKeys[e.keyCode]); | ||
}); | ||
|
||
addBug() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One of the purposes of OOP is that class defines all behaviors of the class.
Please explain why this approach was taken.
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.
My logic then was as follows: this kind of behavior is typical only to player object. As in this case we do not need this functionality in Player-prototyped enemy-objects , so I did not prototype it in Player.
Also, TBH, I am a newbie who tryed ways - and this one worked. Re-wrote with Player.prototype
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.
player.propertyName = function ()
worked, as expected. We still can have functions as object properties.But what if we have a multiplayer game? OOP helps to have objects blue-prints and avoid code duplication.
Oh, I see. You based enemies on
Player
.So in fact
Player
is an "any" character.The good thing was that you came up with having a base class for shared properties and behaviour.
The improvemnt would be to have both
Enemy
andPlayer
class extend such base class with their own specifics.Are you up to improve your code accordingly?
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.
yes, that way it makes more sense and now I was able to solve ' player.x = playerStartX' omitting .
Done