From 784d63a7801171cd98d66a9aa87372c3906e9173 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 19 Aug 2012 17:11:01 +0200 Subject: [PATCH] no magic numbers --- herzle.html | 3 +-- herzle.js | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/herzle.html b/herzle.html index f2c71c9..5397b32 100644 --- a/herzle.html +++ b/herzle.html @@ -11,7 +11,6 @@ } #player { position: absolute; - bottom: 10%; } #enemy { position: absolute; @@ -28,7 +27,7 @@ - +
3
<
diff --git a/herzle.js b/herzle.js index 257e005..1d59487 100644 --- a/herzle.js +++ b/herzle.js @@ -1,7 +1,7 @@ debug = true; -player_pos = [300, 500]; -enemy_pos = [300, 0]; +player_pos = [0.5 * window.innerWidth, 0.9 * window.innerHeight]; +enemy_pos = [0.5 * window.innerWidth, 0.1 * window.innerHeight]; pewpew = 0; document.onkeydown = onKeyDown; @@ -97,16 +97,19 @@ function render() { // render player var playerdiv = document.getElementById("player"); - playerdiv.style.left = (player_pos[0] - - Math.round(playerdiv.getClientRects()[0].width/2)) + 'px'; - // FIXME playerdiv.style.top = player_pos[1]; + playerdiv.style.left = (player_pos[0] + - Math.round(playerdiv.getClientRects()[0].width/2)) + 'px'; + playerdiv.style.top = (player_pos[1] + - Math.round(playerdiv.getClientRects()[0].height/2)) + 'px'; if (debug) playerdiv.style.border = "1px solid"; // render enemy var enemydiv = document.getElementById("enemy"); - enemydiv.style.left = enemy_pos[0]; - enemydiv.style.top = enemy_pos[1]; + enemydiv.style.left = (enemy_pos[0] + - Math.round(enemydiv.getClientRects()[0].width/2)) + 'px'; + enemydiv.style.top = (enemy_pos[1] + - Math.round(enemydiv.getClientRects()[0].height/2)) + 'px'; if (debug) enemydiv.style.border = "1px solid"; } @@ -133,7 +136,7 @@ function animate() { var off = min + parseInt(Math.random() * (max-min)); enemy_pos[0] += off; if (enemy_pos[0] < 0) enemy_pos[0] = 0; - if (enemy_pos[0] > 600) enemy_pos[0] = 600; + if (enemy_pos[0] > window.innerWidth) enemy_pos[0] = window.innerWidth; } function gameLoop() { @@ -143,3 +146,7 @@ function gameLoop() { setTimeout("gameLoop()", 100); } + +function init() { + gameLoop(); +}