-
-
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 #282
Closed
Closed
Js-oop frogger game #282
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
341f90d
Add index.js tiny-js-world
4ee0b35
Merge branch 'main' of https://github.com/kottans/frontend-2022-homew…
0a24a58
Add app.js
ffc6d09
Merge branch 'main' into js-oop
HelenGreent 2ef30b8
Delete task tiny-JS-world from feature js-oop
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
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,155 @@ | ||
const canvas = { | ||
width: 101, | ||
height: 82, | ||
number_of_blocks_x: 5, | ||
number_of_blocks_y: 6, | ||
padding_bottom: 62, | ||
}; | ||
|
||
const enemyConfiguration = { | ||
min_speed: 100, | ||
top_initial_y: 60, | ||
middle_initial_y: 145, | ||
bottom_initial_y: 230, | ||
padding: 50, | ||
}; | ||
|
||
const playerConfiguration = { | ||
initial_x: 200, | ||
initial_y: 400, | ||
}; | ||
|
||
const water_edge = 52; | ||
const initial_enemy_x = -canvas.width; | ||
const edge_x = canvas.width * canvas.number_of_blocks_x; | ||
const edge_y = | ||
canvas.height * canvas.number_of_blocks_y - canvas.padding_bottom; | ||
|
||
// Enemies our player must avoid | ||
const Enemy = function (y) { | ||
// Variables applied to each of our instances go here, | ||
// we've provided one for you to get started | ||
const random_speed = Math.floor(Math.random() * 200); | ||
this.x = initial_enemy_x; | ||
this.y = y; | ||
this.speed = enemyConfiguration.min_speed + random_speed; | ||
|
||
// The image/sprite for our enemies, this uses | ||
// a helper we've provided to easily load images | ||
this.sprite = "images/enemy-bug.png"; | ||
}; | ||
|
||
// Update the enemy's position, required method for game | ||
// Parameter: dt, a time delta between ticks | ||
Enemy.prototype.update = function (dt) { | ||
// You should multiply any movement by the dt parameter | ||
// which will ensure the game runs at the same speed for | ||
// all computers. | ||
|
||
this.x += this.speed * dt; | ||
if (this.x > edge_x) { | ||
this.x = -canvas.width; | ||
} | ||
}; | ||
|
||
// Draw the enemy on the screen, required method for game | ||
Enemy.prototype.render = function () { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); | ||
}; | ||
|
||
// Now write your own player class | ||
// This class requires an update(), render() and | ||
// a handleInput() method. | ||
const Player = function (x, y) { | ||
this.x = x; | ||
this.y = y; | ||
this.sprite = "images/char-boy.png"; | ||
}; | ||
|
||
Player.prototype.update = function () { | ||
this.checkCollision(); | ||
}; | ||
|
||
Player.prototype.checkCollision = function () { | ||
allEnemies.forEach(function (enemy) { | ||
if ( | ||
this.y - enemyConfiguration.padding < enemy.y && | ||
this.y + enemyConfiguration.padding > enemy.y && | ||
this.x - enemyConfiguration.padding < enemy.x && | ||
this.x + enemyConfiguration.padding > enemy.x | ||
) { | ||
this.lose(); | ||
} | ||
}, this); | ||
}; | ||
|
||
Player.prototype.render = function () { | ||
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); | ||
}; | ||
|
||
Player.prototype.handleInput = function (keyCode) { | ||
if (this.y >= 0 && keyCode === "up") { | ||
this.y -= canvas.height; | ||
} | ||
if (this.y < edge_y - canvas.height && keyCode === "down") { | ||
this.y += canvas.height; | ||
} | ||
if (this.x > 0 && keyCode === "left") { | ||
this.x -= canvas.width; | ||
} | ||
if (this.x < edge_x - canvas.width && keyCode === "right") { | ||
this.x += canvas.width; | ||
} | ||
if (this.y < water_edge) { | ||
this.wins(); | ||
} | ||
}; | ||
|
||
Player.prototype.resetPosition = function () { | ||
this.x = playerConfiguration.initial_x; | ||
this.y = playerConfiguration.initial_y; | ||
}; | ||
|
||
Player.prototype.wins = function () { | ||
setTimeout(() => { | ||
alert("You win! Congratulation"); | ||
this.resetPosition(); | ||
}, 100); | ||
}; | ||
|
||
Player.prototype.lose = function () { | ||
setTimeout(() => { | ||
this.resetPosition(); | ||
}, 100); | ||
alert("Game over. Try again!"); | ||
}; | ||
|
||
// Now instantiate your objects. | ||
// Place all enemy objects in an array called allEnemies | ||
// Place the player object in a variable called player | ||
|
||
const allEnemies = [ | ||
new Enemy(enemyConfiguration.top_initial_y), | ||
new Enemy(enemyConfiguration.middle_initial_y), | ||
new Enemy(enemyConfiguration.bottom_initial_y), | ||
]; | ||
|
||
const player = new Player( | ||
playerConfiguration.initial_x, | ||
playerConfiguration.initial_y | ||
); | ||
|
||
// This listens for key presses and sends the keys to your | ||
// Player.handleInput() method. You don't need to modify this. | ||
function handleClick(e) { | ||
const allowedKeys = { | ||
37: "left", | ||
38: "up", | ||
39: "right", | ||
40: "down", | ||
}; | ||
|
||
player.handleInput(allowedKeys[e.keyCode]); | ||
} | ||
|
||
document.addEventListener("keyup", handleClick); |
This file was deleted.
Oops, something went wrong.
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.