neingeist
/
30shooter
Archived
1
0
Fork 0

no magic numbers

master
neingeist 12 years ago
parent 83c34d496e
commit 784d63a780

@ -11,7 +11,6 @@
}
#player {
position: absolute;
bottom: 10%;
}
#enemy {
position: absolute;
@ -28,7 +27,7 @@
</style>
</head>
<body onload="gameLoop();">
<body onload="init();">
<div id="enemy">3</div>
<div id="player">&lt;</div>
<div id="pewpew"></div>

@ -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();
}