Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                * {
  margin: 0;
  padding: 0;
}
body { 
  background-color: #222;
} 

/*ゲーム画面*/
#game-screen {
  margin: 0 auto;
}



              
            
!

JS

              
                //設定用
const Config = {
  //画面の解像度
  Screen: {
    Width: 512,//幅
    Height: 512,//高さ
    BackGroundColor: 0x444444,//背景色
  },
  Keys: { //キーボード入力
    Up: "w",
    Right: "d",
    Down: "s",
    Left: "a",
    A: "n",
    B: "m",
    Start: "Enter"
  },
  //vpadType: DirKey8way,
  vpadType: DirKey4way /*切り替えできます*/
}

//読みこむデータリスト(名前:ファイルパス)
const Resource = {
  Player: 'https://dl.dropbox.com/s/myi6mgjgsyh5e7y/player.png',
}

//上のリストを読み込みやすい形に変えてます
const assets = [];
for (let key in Resource) {
  assets.push(Resource[key]);
}

let core;//ゲームの基幹プログラム用の変数
let inputManager;

//ブラウザの読み込みが完了したら実行されます
window.onload = () => {
  //設定した画面サイズでcoreを作成
  core = new Game(Config.Screen.Width, Config.Screen.Height, Config.Screen.BackGroundColor);
  inputManager = new InputManager();

  //データのロード
  core.preload(assets);

  //読み込み完了でメインシーンに切り替わります
  core.onload = () => {
    core.replaceScene(new MainScene());
  }
}


let player;
//メインシーン
class MainScene extends Container {
  constructor(){
    super();

    player = new Player(64, 64);
    this.addChild(player);
  }
  update(delta){
    super.update(delta);
    inputManager.gamePad.update();

    if(inputManager.checkButton("Start") == inputManager.keyStatus.DOWN){
      core.pushScene(new PauseScene());
    }   
  }
}

//全キャラクター共通基本クラス
class Actor extends EnchantSprite {
  constructor(w, h){
    super(w, h);
    this.anchor.set(0.5, 1);//Y座標の中心は足元に
    this.vx = 0;
    this.vy = 0;
    this.fpX = 0;//小数点以下保存用
    this.fpY = 0;

    //アニメーション用
    this.animation = {
      isPlayAuto: false,
      frameNumbers: [],
      intervals: [],
      type: null,
      index: 0,
      count: 0,
      data: {}
    }
  }
  //アニメーション切り替えプログラム
  changeAnimationPattern(type){
    const anim = this.animation;
    anim.frameNumbers = anim.data[type].frameNumbers;
    anim.intervals = anim.data[type].intervals;
    anim.type = type;
    anim.index = 0;
    anim.count = 0;
    this.frameNumber = anim.frameNumbers[0];
  }
  //キャラの座標から小数点を抜く
  posRemoveFractionalPart(){
    const x = this.x | 0;//小数点以下を切り捨て
    this.fpX = this.x - x;
    this.x = x;
    const y = this.y | 0;
    this.fpY = this.y - y;
    this.y = y;
  }
  //キャラの座標に小数点以下を戻す
  posRestoreFractionalPart(){
    this.x += this.fpX;
    this.y += this.fpY;
  }
  update(delta){
    super.update(delta);
    //自動アニメーション処理
    const anim = this.animation;
    if(anim.isPlayAuto && ++anim.count > anim.intervals[anim.index]){
      if(++anim.index >= anim.intervals.length){
        anim.index = 0;
      }
      this.frameNumber = anim.frameNumbers[anim.index];
      anim.count = 0;
    }
  }
}

//プレイヤークラス
class Player extends Actor {
  constructor(w, h){
    super(w, h);
    this.image = core.resources[Resource.Player].texture; 
    this.position.set(Config.Screen.Width*0.5, Config.Screen.Height*0.75);
    this.maxSpeed = 4;
    this.acceleration = 0.24;
    this.friction = 0.08;
    this.isOnGround = true;
    this.gravity = 0.3;

    this.animation.data = {
      stand: {
        frameNumbers: [1],
        intervals: [100],
      },
      walk: {
        frameNumbers: [0, 1, 2, 1],
        intervals: [8, 8, 8, 8],
      },
      jump: {
        frameNumbers: [4],
        intervals: [100],
      },
      fall: {
        frameNumbers: [5],
        intervals: [100],
      }
    }
    this.animation.type = 'stand';
    this.animation.isPlayAuto = true;
  }
  update(delta){
    super.update(delta);
    this.posRestoreFractionalPart();//一番最初に小数点を戻す 

    const btn = inputManager.checkButton('A');
    if(btn == inputManager.keyStatus.DOWN){
      if(this.isOnGround){
        this.vy = -8;
        this.changeAnimationPattern('jump');
      }
    }
    if(btn == inputManager.keyStatus.RELEASE){
      if(this.vy < 0){
        this.vy /= 2;
      }
    }
    this.isOnGround = false;
    this.vy += this.gravity;
    this.y += this.vy;
    if(this.y > Config.Screen.Height*0.75){
      this.y = Config.Screen.Height*0.75;
      this.vy = 0;
      this.isOnGround = true;
      if(this.animation.type != 'walk'){
        this.changeAnimationPattern('walk');
      }
      
    }else if(this.vy > 0 && this.animation.type != 'fall'){
      this.changeAnimationPattern('fall');
    }

    switch(inputManager.checkDirection()){
      case inputManager.keyDirections.RIGHT:
        if(this.isOnGround && this.vx == 0){
          this.changeAnimationPattern('walk');
        }
        this.scale.x = 1;
        this.vx += this.acceleration;
        this.vx = this.vx > this.maxSpeed ? this.maxSpeed : this.vx;
        break;
      case inputManager.keyDirections.LEFT:
        if(this.isOnGround && this.vx == 0){
          this.changeAnimationPattern('walk');
        }
        this.scale.x = -1;
        this.vx -= this.acceleration;
        this.vx = this.vx < -this.maxSpeed ? -this.maxSpeed : this.vx;
        break;
      case inputManager.keyDirections.DOWN:
        break;
      default:
        break;
    }

    
    //摩擦的なもの(減速させる)
    if(this.vx > this.friction){
      this.vx -= this.friction;
    }else if(this.vx < -this.friction){
      this.vx += this.friction;
    }else{
      this.vx = 0;
      if(this.isOnGround){
        this.changeAnimationPattern('stand');
      }
    }
    this.x += this.vx;
    this.posRemoveFractionalPart();//一番最後に小数点を取り除く
  }
}




              
            
!
999px

Console