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

              
                html,
body {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  background: #111;
  color: #eee;
  font: caption;
}

.dg {
  font: caption !important;
  text-shadow: none !important;
}

.dg .property-name {
  font: small-caption !important;
  line-height: 27px !important;
  user-select: none;
}


              
            
!

JS

              
                var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });

var speed=8;// movement speed
var curvature =0.03 //how curved interpolated walls will be higher is more curvature  , but more expensive

function preload() {
     game.load.baseURL = '//examples.phaser.io/';
    game.load.crossOrigin = 'anonymous';
    //game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
    game.load.image('dude', 'assets/sprites/phaser-dude.png');
}

function create() {
  game.physics.startSystem(Phaser.Physics.P2JS);
  game.physics.p2.restitution=1;
  
  circle= game.add.sprite(300,300);
  game.physics.p2.enable(circle,true);
  circle.body.setCircle(20);
  circle.body.velocity.y=-1500;
  circle.body.collideWorldBounds = true;
  
  wall=game.add.sprite(0,0);
  game.physics.p2.enable(wall,true);
  wall.body.static=true;
  wall2=game.add.sprite(game.width/2,0);
  game.physics.p2.enable(wall2,true);
  wall2.body.static=true;

  wallgroup=game.add.group();
  wallgroup.add(wall);
  wallgroup.add(wall2);

  randomizeLinear(wall);  
  randomizeInterpolation(wall2,8);
}

function update() {
  wallgroup.forEach(function(wall){checkLimits(wall)}); //check if outside game
  wallgroup.forEach(function(wall){wall.body.x-=speed});//move walls
}

function render() {

}

function checkLimits(wall){
  if(wall.body.x<-game.width/2){
    wall.body.x=game.width*3/2;
    wall.body.y=0;
    var chance=Math.random();
    if(chance<0.3){
      randomizeLinear(wall);  
    }
    if(chance>=0.3&&chance<0.6){
      randomizeSin(wall);  
    }
    if(chance>=0.6&&chance<1){
      randomizeInterpolation(wall,8); 
    }
  }
}

function randomizeInterpolation(sprite,n){
  var nodes=[new Array(n),new Array(n)]; //nodes that will be interpolated
  for (var i = 0; i < nodes[1].length; i++){
    nodes[0][i] = i*(game.width/(n+1))+101; //distribute evenly
    nodes[1][i] = game.rnd.between(32, 332); //random y location
  }
  var intermediatepoints=[];  
  var x = 1 / game.width;
  for (var i = 0; i <= 1; i += x/curvature)
  {
      //var px = game.math.linearInterpolation(nodes[0],i);
      //var py = game.math.linearInterpolation(nodes[1],i);
      //var px = game.math.bezierInterpolation(nodes[0], i);
      //var py = game.math.bezierInterpolation(nodes[1], i);
      var px = game.math.catmullRomInterpolation(nodes[0], i);
      var py = game.math.catmullRomInterpolation(nodes[1], i);
      intermediatepoints.push([px,py]);
  }
  drawPoly(sprite,intermediatepoints);
}


function randomizeSin(sprite){//draw sin wave
  var intermediatepoints=[];  
  length=60;
  var data = game.math.sinCosGenerator(length, 50*Math.random()+20, 10, 2*Math.PI);//length, sinAmplitude, cosAmplitude, frequency)
  sin = data.sin;
  for(i=1;i<length;i++){
    intermediatepoints.push([i*10,sin[i]+100]);
  }
  drawPoly(sprite,intermediatepoints);
}

function randomizeLinear(sprite){
  var intermediatepoints=[[game.width/2,150*Math.random()]];//you can provide your own random (convex)  shape [[x1,y1],[x2,y2],...[xn,yn]] 
  drawPoly(sprite,intermediatepoints);//build the poly
}

function drawPoly(sprite,points){
  var datapoints=[];//empty the data
  datapoints.push([sprite.x,0],[sprite.x,100]);//push the left corner of the wall
  points.forEach(function(element) {datapoints.push([element[0]+sprite.x,element[1]])}, this);//push the intermediate shape data
  datapoints.push([sprite.x+game.width,100],[sprite.x+game.width,0]);//push the right corner of the wall
  sprite.body.clearShapes();//clear it
  sprite.body.addPolygon(null,datapoints);//redraw it
}
              
            
!
999px

Console