neingeist
/
30shooter
Archived
1
0
Fork 0

no magic numbers

master
neingeist 12 years ago
parent 83c34d496e
commit 784d63a780

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

@ -1,7 +1,7 @@
debug = true; debug = true;
player_pos = [300, 500]; player_pos = [0.5 * window.innerWidth, 0.9 * window.innerHeight];
enemy_pos = [300, 0]; enemy_pos = [0.5 * window.innerWidth, 0.1 * window.innerHeight];
pewpew = 0; pewpew = 0;
document.onkeydown = onKeyDown; document.onkeydown = onKeyDown;
@ -98,15 +98,18 @@ function render() {
// render player // render player
var playerdiv = document.getElementById("player"); var playerdiv = document.getElementById("player");
playerdiv.style.left = (player_pos[0] playerdiv.style.left = (player_pos[0]
- Math.round(playerdiv.getClientRects()[0].width/2)) + 'px'; - Math.round(playerdiv.getClientRects()[0].width/2)) + 'px';
// FIXME playerdiv.style.top = player_pos[1]; playerdiv.style.top = (player_pos[1]
- Math.round(playerdiv.getClientRects()[0].height/2)) + 'px';
if (debug) if (debug)
playerdiv.style.border = "1px solid"; playerdiv.style.border = "1px solid";
// render enemy // render enemy
var enemydiv = document.getElementById("enemy"); var enemydiv = document.getElementById("enemy");
enemydiv.style.left = enemy_pos[0]; enemydiv.style.left = (enemy_pos[0]
enemydiv.style.top = enemy_pos[1]; - Math.round(enemydiv.getClientRects()[0].width/2)) + 'px';
enemydiv.style.top = (enemy_pos[1]
- Math.round(enemydiv.getClientRects()[0].height/2)) + 'px';
if (debug) if (debug)
enemydiv.style.border = "1px solid"; enemydiv.style.border = "1px solid";
} }
@ -133,7 +136,7 @@ function animate() {
var off = min + parseInt(Math.random() * (max-min)); var off = min + parseInt(Math.random() * (max-min));
enemy_pos[0] += off; enemy_pos[0] += off;
if (enemy_pos[0] < 0) enemy_pos[0] = 0; 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() { function gameLoop() {
@ -143,3 +146,7 @@ function gameLoop() {
setTimeout("gameLoop()", 100); setTimeout("gameLoop()", 100);
} }
function init() {
gameLoop();
}