数を数えるだけ

概要

任意の FPS で計算と描画を行うだけのプログラムを作る。
Cookie Clicker を超コンパクトにしたような感じ。

ソース

HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>xxxxx</title>
<script src="./main.js"></script>
</head>

<body>
<div id="wrapper">
    <div id="game" class="focusLeft noFancy">
        <div id="javascriptError" style="display: none;"></div>
        <div id="counter">0</div>
    </div>
</div>
</body>

</html>

Java Script

/*=====================================================================================
MISC HELPER FUNCTIONS
=======================================================================================*/
function l(what) {return document.getElementById(what);}

/*================================================================================================
GAME INITIALIZATION
==================================================================================================*/
Game={};

Game.Launch = function()
{
    Game.Load = function()
    {
        Game.Init();
    }

    Game.Init = function()
    {
        Game.ready = 1;
        
        /*=====================================================================================
        VARIABLES
        =======================================================================================*/
        Game.fps = 30;
        Game.counter = 0;
        
        //latency compensator stuff
        Game.time = new Date().getTime();
        Game.accumulatedDelay = 0;
        Game.catchupLogic = 0;
        
        //main loop
        Game.Loop();
    }

    /*=====================================================================================
    LOGIC
    =======================================================================================*/
    Game.Logic = function()
    {
        Game.counter++;
    }

    /*=====================================================================================
    DRAW
    =======================================================================================*/
    Game.Draw = function()
    {
        l('counter').innerHTML = Game.counter.toString();
    }

    /*=====================================================================================
    MAIN LOOP
    =======================================================================================*/
    Game.Loop = function()
    {
        // update game logic !
        Game.catchupLogic = 0;
        Game.Logic();
        Game.catchupLogic = 1;
        
        // latency compensator
        Game.accumulatedDelay += ((new Date().getTime() - Game.time) - 1000 / Game.fps);
        Game.accumulatedDelay = Math.min(Game.accumulatedDelay, 1000 * 5);
        Game.time = new Date().getTime();
        while (Game.accumulatedDelay > 0)
        {
            Game.Logic();
            Game.accumulatedDelay -= 1000 / Game.fps;
        }
        Game.catchupLogic = 0;
        
        Game.Draw();
        
        setTimeout(Game.Loop, 1000 / Game.fps);
    }
}

/*=====================================================================================
LAUNCH THIS THING
=======================================================================================*/
Game.Launch();

window.onload = function()
{
    if (!Game.ready) Game.Load();
};

  • 最終更新:2014-05-14 13:05:27

このWIKIを編集するにはパスワード入力が必要です

認証パスワード