Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Enemies our player must avoid
- // declaring the enemy object
- var Enemy = function(x, y, speed) {
- // Variables applied to each of our instances go here,
- // we've provided one for you to get started
- // The image/sprite for our enemies, this uses
- // a helper we've provided to easily load images
- this.x = x;
- this.y = y;
- this.speed = speed;
- this.sprite = 'images/enemy-bug.png';
- };
- // Update the enemy's position, required method for game
- // Parameter: dt, a time delta between ticks
- // setting enemy speed
- 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.
- if (this.x <= 500)
- {
- this.x = (this.speed * dt) + this.x;
- }
- else
- {
- this.x = 0;
- }
- };
- // Draw the enemy on the screen, required method for game
- // enemy image placed on screen
- Enemy.prototype.render = function() {
- ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
- };
- // setting up multiple enemy characters
- var enemy1 = new Enemy(200,300, 30);
- var enemy2 = new Enemy(100,200, 60);
- var enemy3 = new Enemy(300,100, 90);
- var allEnemies = [enemy1,enemy2,enemy3];
- // Now write your own player class
- // This class requires an update(), render() and
- // a handleInput() method.
- // declaring the player class
- var Player = function(x,y) {
- this.x = x;
- this.y = y;
- this.sprite = 'images/char-boy.png'
- };
- //declaring new player variable
- var player = new Player (100,300);
- // reset postition of player on the event of a reset
- Player.prototype.reset = function() {
- this.x = 100;
- this.y = 300;
- if (this.x >= 505)
- {
- this.x = 100;
- this.y = 300;
- }
- if (this.y >= 606)
- {
- this.x = 100;
- this.y = 300;
- }
- };
- // updating parameters. If player leaves screen and if player reaches water
- Player.prototype.update = function() {
- for (var i = 0; i < 3; i++) {
- if ((this.x + 50 > allEnemies[i].x) && (this.x < allEnemies[i].x + 60) && (this.y + 50 > allEnemies[i].y) && (this.y < allEnemies[i].y + 60))
- {
- console.log("collision")
- player.reset();
- //please fix the logics on reset and update
- }
- }
- };
- // player image placed on screen
- Player.prototype.render = function(){
- ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
- };
- // setting up parameters for the event of user selected direction for player
- Player.prototype.handleInput = function(direction){
- if(direction === 'left' && this.x >= 100)
- {
- this.x = (this.x - 100);
- }
- if(direction === 'up' && this.y >= 80)
- {
- this.y = (this.y - 100);
- }
- if(direction === 'right' && this.x <= 400)
- {
- this.x = (this.x + 100);
- }
- if(direction === 'down' && this.y <= 300)
- {
- this.y = (this.y + 100);
- }
- if (this.y <= 100)
- {
- player.reset();
- }
- if (this.y >= 490)
- {
- player.reset();
- }
- };
- // Now instantiate your objects.
- // Place all enemy objects in an array called allEnemies
- // Place the player object in a variable called player
- // This listens for key presses and sends the keys to your
- // Player.handleInput() method. You don't need to modify this.
- document.addEventListener('keyup', function(e) {
- var allowedKeys = {
- 37: 'left',
- 38: 'up',
- 39: 'right',
- 40: 'down'
- };
- player.handleInput(allowedKeys[e.keyCode]);
- });
Add Comment
Please, Sign In to add comment