Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    <div class="display">
        <div class="bar"></div>
        <div class="ball"></div>
        <div class="explain-label">click to start</div>
        <div class="left-button">
            <div>←</div>
        </div>
        <div class="right-button">
            <div>→</div>
        </div>
    </div>

              
            
!

CSS

              
                .display {
    position: relative;
    width: 400px;
    height: 500px;
    border-radius: 10px;
    background: #333;
    border: 2px solid #cc7799;
}

.bar {
    position: absolute;
    width: 60px;
    height: 20px;
    background: #99ff99;
}

.block {
    position: absolute;
    box-sizing: border-box;
    width: 40px;
    height: 20px;
    border-radius: 4px;
    border: 2px solid #ffffff;
    background: #339999;
}

.ball {
    position: absolute;
    width: 10px;
    height: 10px;
    border-radius: 30px;
    background: #ff3333;
    border: 2px solid #ffffff;
}

.explain-label {
    position: absolute;
    text-align: center;
    width: 400px;
    font-size: 20px;
    color: #ffffff;
    pointer-events: none;
}

.left-button,
.right-button {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 28px;
    font-weight: bold;
    color: #ffffff;
    background: #99ff9980;
    width: 100px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    border: 2px solid #ffffff;
}
              
            
!

JS

              
                window.onload = () => {

    /**
     * domの生成・位置取得
     */
    const setDomPosition = (dom, pos) => {
        dom.style.left = `${pos.x}px`;
        dom.style.top = `${pos.y}px`;
    }

    const getDomPosition = (dom) => {
        const pos = {
            x: parseFloat(dom.style.left),
            y: parseFloat(dom.style.top)
        };
        return pos;
    }

    const createDom = (className) => {
        const dom = document.createElement('div');
        dom.classList.add(className);
        return dom;
    }

    const addChildDom = (parentDom, childDom) => {
        parentDom.append(childDom);
    }

    const removeChildDom = (parentDom, childDom) => {
        parentDom.removeChild(childDom);
    }

    const changeDomTextContent = (dom, textContent) => {
        dom.textContent = textContent;
    }

    /**
     * 入力イベント
     */
    const leftButtonPress = () => {
        barLeftSpeed = 5;
    }

    const leftButtonRelease = () => {
        barLeftSpeed = 0;
    }

    const rightButtonPress = () => {
        barRightSpeed = 5;
    }

    const rightButtonRelease = () => {
        barRightSpeed = 0;
    }

    const displayRelease = () => {
        if (gameMode === "startWait") {
            changeDomTextContent(explainLabelDom, "break all blocks!");
            gameMode = "gamePlaying";
        }
    }

    /**
     * メインループイベント
     */
    const mainLoop = () => {
        switch (gameMode) {
            case "gamePlaying":
                {
                    // バーの操作
                    const barPos = getDomPosition(barDom);
                    const barMovedPos = {
                        x: barPos.x + barRightSpeed - barLeftSpeed,
                        y: barPos.y
                    };
                    if (barMovedPos.x >= 0 && barMovedPos.x <= 340) {
                        setDomPosition(barDom, barMovedPos);
                    }

                    // ボール移動
                    const ballPos = getDomPosition(ballDom);
                    let ballMovedPos = {
                        x: ballPos.x + ballVec.x,
                        y: ballPos.y + ballVec.y
                    };
                    if (ballMovedPos.x > 390) {
                        ballMovedPos.x = 390;
                        ballVec.x = -ballVec.x;
                    }
                    if (ballMovedPos.x < 0) {
                        ballMovedPos.x = 0;
                        ballVec.x = -ballVec.x;
                    }
                    if (ballMovedPos.y < 0) {
                        ballMovedPos.y = 0;
                        ballVec.y = -ballVec.y;
                    }
                    if (ballMovedPos.y > 510) {
                        init(); // ボールが画面下にいってしまうとゲームオーバーなので初期化する
                    } else {
                        setDomPosition(ballDom, ballMovedPos);
                    }

                    // ブロックとの衝突処理
                    for (const blockDom of blockDoms) {
                        const blockPos = getDomPosition(blockDom);
                        if (checkCollision({ x: blockPos.x, y: blockPos.y, width: 40, height: 20 }, { x: ballMovedPos.x, y: ballMovedPos.y, width: 10, height: 10 })) {
                            removeChildDom(displayDom, blockDom);
                            blockDoms = blockDoms.filter((dom => {
                                return dom != blockDom;
                            }));
                            ballVec.y = -ballVec.y;
                        }
                    }

                    // バーと球の衝突処理
                    if (checkCollision({ x: barPos.x, y: barPos.y, width: 60, height: 20 }, { x: ballMovedPos.x, y: ballMovedPos.y, width: 10, height: 10 })) {
                        ballMovedPos.y = barDom.y;
                        ballVec.y = -ballVec.y;
                    }

                    // ブロックが全てなくなったか調べる
                    if (blockDoms.length === 0) {
                        gameMode = "clear";
                        changeDomTextContent(explainLabelDom, "CLEAR!");
                    }


                    break;
                }
        }
        window.requestAnimationFrame(mainLoop);
    };
    window.requestAnimationFrame(mainLoop);

    /**
     * ロジック部分
     */

    // 初期化関数
    const init = () => {
        // 状態の初期化
        gameMode = "startWait";

        changeDomTextContent(explainLabelDom, "click to start");

        // 静的な定義DOMの座標定義
        setDomPosition(barDom, { x: 175, y: 410 });
        setDomPosition(leftButtonDom, { x: 50, y: 440 });
        setDomPosition(rightButtonDom, { x: 250, y: 440 });
        setDomPosition(ballDom, { x: 190, y: 300 });
        setDomPosition(explainLabelDom, { x: 0, y: 350 });

        // ボール向き定義
        ballVec = { x: 3, y: -3 };


        // 画面にブロックが残っていれば、全部削除してblockDomsを空にする
        while (blockDoms.length > 0) {
            removeChildDom(displayDom, blockDoms.pop());
        }


        //ブロック生成 (w:20px h:10px)
        for (let xIndex = 0; xIndex < 10; xIndex++) {
            for (let yIndex = 0; yIndex < 10; yIndex++) {
                const blockDom = createDom("block");
                setDomPosition(blockDom, { x: xIndex * 40, y: yIndex * 20 });
                addChildDom(displayDom, blockDom);
                blockDoms.push(blockDom);
            }
        }
    }


    const calcLength = (pos1, pos2) => {
        return Math.sqrt((pos1.x - pos2.x) ** 2 + (pos1.y - pos2.y) ** 2);
    }

    const checkCollision = (rect1, rect2) => {
        if ((rect1.x + rect1.width) >= rect2.x && rect1.x <= (rect2.x + rect2.width) && (rect1.y + rect1.height) >= rect2.y && rect1.y <= (rect2.y + rect2.height)) {
            return true;
        }
        return false;
    }



    // 副作用を持つ状態変数の定義
    let blockDoms = [];
    let barLeftSpeed = 0;
    let barRightSpeed = 0;
    let ballVec = { x: 5, y: 5 };
    let gameMode = "startWait"; // "startWait"|"gamePlaying"|"clear"



    // 初期から存在するDOMの定義
    const displayDom = document.getElementsByClassName("display")[0];
    const barDom = document.getElementsByClassName("bar")[0];
    const ballDom = document.getElementsByClassName("ball")[0];
    const leftButtonDom = document.getElementsByClassName("left-button")[0];
    const rightButtonDom = document.getElementsByClassName("right-button")[0];
    const explainLabelDom = document.getElementsByClassName("explain-label")[0];


    /**
     * 入力系
     */
    // MEMO: PCとスマホでタッチにベントが異なるので複数作成する 
    //       実はPC・スマホ両方使えるpointerdown/pointerupというイベントがあるのだが、古いsafariがこれで動作しないので不採用
    //       あと数年たったら死滅すると思われるのでpointer系のイベントが使えるようになるはず
    // PC用
    leftButtonDom.addEventListener("mousedown", leftButtonPress, false);
    leftButtonDom.addEventListener("mouseup", leftButtonRelease, false);
    leftButtonDom.addEventListener("mouseout", leftButtonRelease, false);
    rightButtonDom.addEventListener("mousedown", rightButtonPress, false);
    rightButtonDom.addEventListener("mouseup", rightButtonRelease, false);
    rightButtonDom.addEventListener("mouseout", rightButtonRelease, false);
    displayDom.addEventListener("mouseup", displayRelease, false);


    // スマホ用
    leftButtonDom.addEventListener("touchstart", leftButtonPress, false);
    leftButtonDom.addEventListener("touchend", leftButtonRelease, false);
    rightButtonDom.addEventListener("touchstart", rightButtonPress, false);
    rightButtonDom.addEventListener("touchend", rightButtonRelease, false);
    displayDom.addEventListener("touchend", displayRelease, false);


    init();
}
              
            
!
999px

Console