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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                
              
            
!

CSS

              
                body {
  background-color: #222222;
}
              
            
!

JS

              
                // 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

Console