From 4015d36de8bd962c5f2c82685efea320ed5f826c Mon Sep 17 00:00:00 2001 From: Harkirat Singh Date: Mon, 11 May 2015 00:55:05 +0530 Subject: [PATCH 1/4] Added acceleration functionality --- js/game.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/js/game.js b/js/game.js index 0b0117e..7b8760d 100644 --- a/js/game.js +++ b/js/game.js @@ -31,7 +31,9 @@ monsterImage.src = "images/monster.png"; // Game objects var hero = { - speed: 256 // movement in pixels per second + xspeed: 0, // movement in pixels per second + yspeed:0, + acc: 200 }; var monster = {}; var monstersCaught = 0; @@ -59,19 +61,20 @@ var reset = function () { // Update game objects var update = function (modifier) { + hero.x+=hero.xspeed * modifier; + hero.y+=hero.yspeed * modifier; if (38 in keysDown) { // Player holding up - hero.y -= hero.speed * modifier; + hero.yspeed -= hero.acc * modifier; } if (40 in keysDown) { // Player holding down - hero.y += hero.speed * modifier; + hero.yspeed += hero.acc * modifier; } if (37 in keysDown) { // Player holding left - hero.x -= hero.speed * modifier; + hero.xspeed -= hero.acc * modifier; } if (39 in keysDown) { // Player holding right - hero.x += hero.speed * modifier; + hero.xspeed += hero.acc * modifier; } - // Are they touching? if ( hero.x <= (monster.x + 32) From dccf01f3996b629113d6281788824aad8fa160d0 Mon Sep 17 00:00:00 2001 From: Harkirat Singh Date: Mon, 11 May 2015 01:08:29 +0530 Subject: [PATCH 2/4] Added Friction --- js/game.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/js/game.js b/js/game.js index 7b8760d..10bd368 100644 --- a/js/game.js +++ b/js/game.js @@ -33,7 +33,8 @@ monsterImage.src = "images/monster.png"; var hero = { xspeed: 0, // movement in pixels per second yspeed:0, - acc: 200 + acc: 200, + fric:300 }; var monster = {}; var monstersCaught = 0; @@ -61,19 +62,35 @@ var reset = function () { // Update game objects var update = function (modifier) { + var f=0; hero.x+=hero.xspeed * modifier; hero.y+=hero.yspeed * modifier; if (38 in keysDown) { // Player holding up hero.yspeed -= hero.acc * modifier; + f=1; } if (40 in keysDown) { // Player holding down hero.yspeed += hero.acc * modifier; + f=1; } if (37 in keysDown) { // Player holding left hero.xspeed -= hero.acc * modifier; + f=1; } if (39 in keysDown) { // Player holding right hero.xspeed += hero.acc * modifier; + f=1; + } + if (f==0) + { + if (hero.xspeed>0) + hero.xspeed-=hero.fric * modifier; + if (hero.yspeed>0) + hero.yspeed-=hero.fric * modifier; + if (hero.xspeed<0) + hero.xspeed+=hero.fric * modifier; + if (hero.yspeed<0) + hero.yspeed+=hero.fric * modifier; } // Are they touching? if ( @@ -81,8 +98,11 @@ var update = function (modifier) { && monster.x <= (hero.x + 32) && hero.y <= (monster.y + 32) && monster.y <= (hero.y + 32) + ) { ++monstersCaught; + hero.xspeed=0; + hero.yspeed=0; reset(); } }; From 598b4ef722396e6b1d59172b5b21e3d76e961016 Mon Sep 17 00:00:00 2001 From: Harkirat Singh Date: Mon, 11 May 2015 10:19:06 +0530 Subject: [PATCH 3/4] Added top and bottom constraint so that the hero doesnt run out of the game scope --- js/game.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/js/game.js b/js/game.js index 10bd368..eac3529 100644 --- a/js/game.js +++ b/js/game.js @@ -34,7 +34,7 @@ var hero = { xspeed: 0, // movement in pixels per second yspeed:0, acc: 200, - fric:300 + fric:800 }; var monster = {}; var monstersCaught = 0; @@ -92,13 +92,33 @@ var update = function (modifier) { if (hero.yspeed<0) hero.yspeed+=hero.fric * modifier; } + if (hero.x<0) + { + hero.x=3; + hero.xspeed=20; + } + + if (hero.x>472) + { + hero.x=470; + hero.xspeed=-20; + } + if (hero.y<0) + { + hero.y=3; + hero.yspeed=-20; + } + if (hero.y>445) + { + hero.y=440; + hero.yspeed=20; + } // Are they touching? if ( hero.x <= (monster.x + 32) && monster.x <= (hero.x + 32) && hero.y <= (monster.y + 32) && monster.y <= (hero.y + 32) - ) { ++monstersCaught; hero.xspeed=0; @@ -114,7 +134,7 @@ var render = function () { } if (heroReady) { - ctx.drawImage(heroImage, hero.x, hero.y); + ctx.drawImage(heroImage, hero.x, hero.y,40,40); } if (monsterReady) { @@ -127,18 +147,16 @@ var render = function () { ctx.textAlign = "left"; ctx.textBaseline = "top"; ctx.fillText("Goblins caught: " + monstersCaught, 32, 32); + }; // The main game loop var main = function () { var now = Date.now(); var delta = now - then; - update(delta / 1000); render(); - then = now; - // Request to do this again ASAP requestAnimationFrame(main); }; @@ -150,4 +168,4 @@ requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame // Let's play this game! var then = Date.now(); reset(); -main(); +main(); \ No newline at end of file From 352b239e9973d7ef74f9f041d78da3dc82558ba8 Mon Sep 17 00:00:00 2001 From: Harkirat Singh Date: Mon, 11 May 2015 10:27:29 +0530 Subject: [PATCH 4/4] fixes minor shakiness bug --- js/game.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/js/game.js b/js/game.js index eac3529..e9fd97d 100644 --- a/js/game.js +++ b/js/game.js @@ -84,13 +84,33 @@ var update = function (modifier) { if (f==0) { if (hero.xspeed>0) - hero.xspeed-=hero.fric * modifier; + { + if (hero.xspeed0) + { + if (hero.yspeed-1*hero.fric * modifier) + hero.xspeed=0; + else + hero.xspeed+=hero.fric * modifier; + } if (hero.yspeed<0) - hero.yspeed+=hero.fric * modifier; + { + if (hero.yspeed>-1*hero.fric * modifier) + hero.yspeed=0; + else + hero.yspeed+=hero.fric * modifier; + } } if (hero.x<0) {