Skip to content
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

New branch #11

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Binary file added images/Squirrel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 66 additions & 30 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,28 @@ monsterImage.onload = function () {
};
monsterImage.src = "images/monster.png";

// Squirrel image
var squirrelReady = false;
var squirrelImage = new Image();
squirrelImage.onload = function () {
squirrelReady = true;
};
squirrelImage.src = "images/squirrel.png";

// Game objects
var hero = {
speed: 256 // movement in pixels per second
};
var monster = {};
var squirrel = {};

var monstersCaught = 0;
var monstersNum = 1;
var monster = [
{'monsterName': 'monster1',
'x' : 50,
'y': 100
}
];

// Handle keyboard controls
var keysDown = {};
Expand All @@ -52,9 +68,46 @@ var reset = function () {
hero.x = canvas.width / 2;
hero.y = canvas.height / 2;

// Throw the monster somewhere on the screen randomly
monster.x = 32 + (Math.random() * (canvas.width - 64));
monster.y = 32 + (Math.random() * (canvas.height - 64));
for (var i = 1; i <= monstersNum; i++) {
console.log ("monster number = " + monstersNum)
// Push monster x, y to array
console.log ("i= " + i)
monster.push([
monster.x = 32 + (Math.random() * (canvas.width - 64)),
monster.y = 32 + (Math.random() * (canvas.height - 64))
]);
console.log("print array: " + monster)
console.log("print specific monster x coords:" + monster[i][1])
}

};


// Draw everything
var render = function () {
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);
}
if (heroReady) {
ctx.drawImage(heroImage, hero.x, hero.y);
}
for (var i = 0, len = monster.length; i < len; i++) {
if (monsterReady) {
ctx.drawImage(monsterImage, monster[i][0], monster[i][1]);
}
}

if (squirrelReady) {
ctx.drawImage(squirrelImage, squirrel.x, squirrel.y);
}

// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Goblins caught: " + monstersCaught, 32, 32)
ctx.fillText("Goblins total: " + monstersNum, 32, 62);
};

// Update game objects
Expand All @@ -72,39 +125,22 @@ var update = function (modifier) {
hero.x += hero.speed * modifier;
}

// Are they touching?
// Are they touching?
for (var i = 0, len = monster.length; i < len; i++) {
if (
hero.x <= (monster.x + 32)
&& monster.x <= (hero.x + 32)
&& hero.y <= (monster.y + 32)
&& monster.y <= (hero.y + 32)
hero.x <= (monster[i][0] + 32)
&& monster[i][0] <= (hero.x + 32)
&& hero.y <= (monster[i][1] + 32)
&& monster[i][1] <= (hero.y + 32)
) {
++monstersCaught;
++monstersNum
monster = [50,50]
reset();
}
}
};

// Draw everything
var render = function () {
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);
}

if (heroReady) {
ctx.drawImage(heroImage, hero.x, hero.y);
}

if (monsterReady) {
ctx.drawImage(monsterImage, monster.x, monster.y);
}

// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Goblins caught: " + monstersCaught, 32, 32);
};

// The main game loop
var main = function () {
Expand Down