123

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              body {
  background-color: #222222;
}
            
          
!
            
              // forked from hothukurou's "数学で弾の挙動決める(真上)" http://jsdo.it/hothukurou/6mAp
enchant();  //enchant.jsの初期化

const shotInterval = 15;    //弾の発射間隔
const shotSpeed = 5;    //弾の速さ(1フレームに何px動くか)

const DEG_TO_RAD = (2 * Math.PI) / 360;    //上の度数法をRadに変換する為の数値
const rotateInterval = 30 * DEG_TO_RAD;    //弾の回転移動角度(30度)

window.onload = function () {   //html読み込み完了時に実行する関数
    const game = new Game(400, 400); //width:400px,height:400pxのゲーム画面を設置
    game.fps = 30; //fpsを30に設定
    game.onload = function () { //gameオブジェクトのロード完了時に実行する関数
        const scene = new Scene(); //spriteを貼り付けるsceneを定義
        scene.backgroundColor = "#ffffff";    //sceneの背景を白にする

        //TIPS:黒四角Spriteを定義して座標決めてsceneに貼る
        const blackRect = new Sprite(16, 16);   //width:16px,height:16pxのSpriteを設置
        blackRect.moveTo(192, 192); //(x,y)=(192,192)に設置
        blackRect.backgroundColor = "#000000";//黒で塗る
        blackRect.time = 0; //フレームカウント用の変数
        blackRect.onenterframe = function () {  //毎フレーム毎に実行する関数
            this.time++;    //フレーム毎にtimeを加算する
            if (this.time % shotInterval == 0) {    //shotIntervalフレーム経ったら弾発射
                const bullet = createBullet(this);  //弾を自分の座標に設置
                scene.addChild(bullet); //sceneに弾を貼り付ける
            }
        }
        scene.addChild(blackRect);  //sceneに黒四角を貼り付ける

        //TIPS:引数のspriteを発射元にして赤四角の弾を作成して返す関数
        function createBullet(sprite) {
            const bullet = new Sprite(8, 8);   //width:8px,height:8pxのSpriteを設置
            bullet.moveTo(sprite.x, sprite.y);  //発射元の座標に弾を合わせる
            bullet.backgroundColor = "#ff0000"; //赤に設定
            bullet.rad = (sprite.time / shotInterval) * rotateInterval;    //現在の時間から角度を生成
            bullet.onenterframe = function () {
                this.x += Math.cos(this.rad) * shotSpeed;    //移動角度のcosがX軸移動方向
                this.y += Math.sin(this.rad) * shotSpeed;    //移動角度のsinがY軸移動方向
                if (bullet.x <= -30 || bullet.x >= 430 || bullet.y <= -30 || bullet.y >= 430) { //画面外か判定
                    this.parentNode.removeChild(this);  //画面外なら自身を削除する
                }
            }
            return bullet; //上で作成したspriteを返す
        }


        game.pushScene(scene);//sceneをgameに入れる
    }
    game.start();   //gameオブジェクトを開始する
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console