Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="ru">
    <head>
        <meta charset="utf-8">
        <title>html5 game</title>
        <style>
            body{
                background: #2f2f2f;
            }
            canvas{
                display: block;
                position: absolute;
                border: 1px solid #000;
                margin: auto;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
            }
        </style>
    </head>
<body>
</body>
</html>
 
    var canvas, ctx, keystate = {}, frames,
        fps = 60, cur_fps = 0, dt, lastTime = Date.now(), frame_tick = 0,
        _width, _height,
        states = {Splash: 0, Game: 1, Score: 2},    //состояния игры
        keyUP = 38, keyDOWN = 40, keyLEFT = 37, keyRIGHT = 39, keySPACE = 32,
        player, _gravity = 0.5;
    player = {
        x: null,
        y: null,
        speed: 150,
        frame: 0,
        animation: [0,1],
        width: 64,
        height: 64,
        update: function(){
            /*if (keystate[keyLEFT]) this.x -=this.speed;
            if (keystate[keyRIGHT]) this.x +=this.speed;*/
            this.x +=this.speed * dt;
            if (this.x >= canvas.width){
                this.x = 0;
            }
        },
        draw: function(ctx){
            ctx.fillStyle = "#000";
            ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    };
    function main(){
        canvas = document.createElement("canvas");
        canvas.width = 600;
        canvas.height = 520;
        ctx = canvas.getContext("2d");
        document.body.appendChild(canvas);
        _width = canvas.width;
        _height = canvas.height;
        //Input
         document.addEventListener("keydown", function(evt){
         keystate[evt.keyCode] = true;
         });
         document.addEventListener("keyup", function(evt){
         delete keystate[evt.keyCode];
         });
        run();
        player.x = 0;
        player.y = 128;
    }
    function run(){
        update();
        render();
    }
    function update(){
        var d_now = Date.now();
        dt = (d_now - lastTime)/1000;
        player.update();
        lastTime = d_now;
    }
    function render(){
        ctx.fillStyle = "#00aeef";
        ctx.fillRect(0,0,_width,_height);
        player.draw(ctx);
    }
    main();
    setInterval(run, 1000/60);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers