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

              
                
              
            
!

CSS

              
                body, canvas {
  margin: 0px; 
  padding: 0px; 
  position: absolute; 
  background-color: #303030; 
}
              
            
!

JS

              
                const nRocks = 8;
const rockRadius = 100/1000;
const rockDensity = 1562.5;

const maxEnergy = 2000;
const energyGainedPerFrame = 3;
const thrustCostPerFrame = 3;
const thrustAccelPerFrame = 0.02/1000;

const shotSpeed = 4/1000;
const shotDamage = 500;
const shotCost = 50;
const shotDelayFrames = 30;
const shotLifeFrames = 240;

const deathFrames = 300;


var rocks = [];
var ships = [];
var shots = [];


function createRock() {
  this.x = random( 1 );
  this.y = random( 1 );
  var ang = random( TAU );
  var spd = random( 0.5/1000 );
  this.vx = spd * cos( ang );
  this.vy = spd * sin( ang );
  this.radius = random(rockRadius/2, rockRadius);
  this.invMass = 1/(sq(this.radius)*rockDensity);
  this.angle = random( TAU );
  this.bumps = [];
  for( var i=random(8,12); i>0; i-- ) {
    this.bumps.push( this.radius*(1 - 0.5 * pow(random(1), 3)) );
  }
  
  this.move = function() {
    this.angle = (this.angle + 0.005) % TAU;
    this.x += this.vx;
    this.y += this.vy;
    this.x = (this.x + 1) % 1;
    this.y = (this.y + 1) % 1;
  }
  
  this.show = function() {
    for( var i=0; i<2; i++ ) {
      for( var j=0; j<2; j++ ) {
        var x = (this.x + 0.5) % 1 - 0.5;
        var y = (this.y + 0.5) % 1 - 0.5;
        push();
        translate( x + i, y + j );
        rotate( this.angle );
        noStroke();
        fill( 0, 30, 60, 30 );
        ellipse( 0, 0, 2*this.radius, 2*this.radius );
        fill( 0, 0, 70 );
        beginShape();
        for( var ib=this.bumps.length-1; ib>=0; ib-- ) {
          var ang = TAU * ib / this.bumps.length;
          vertex( this.bumps[ib] * cos(ang), 
                  this.bumps[ib] * sin(ang) );
        }
        endShape();
        pop();
        
      }
    }
  }
}


function createShot( x, y, vx, vy, ang, clr ) {
  this.x = x;
  this.y = y;
  this.vx = vx + shotSpeed * cos( ang );
  this.vy = vy + shotSpeed * sin( ang );
  this.color = clr;
  this.life = shotLifeFrames;
  
  this.move = function() {
    this.x += this.vx;
    this.y += this.vy;
    this.x = (this.x + 1) % 1;
    this.y = (this.y + 1) % 1;
    this.life--;
  }
  
  this.show = function() {
    stroke( this.color, 50, 100 );
    strokeWeight( 3/1000 );
    line( this.x, this.y, this.x - this.vx, this.y - this.vy );
  }
}


function createShip( clr, ex, ey, keyLeft, keyRight, keyFire, keyThrust ) {
  this.x = random( 1 );
  this.y = random( 1 );

  this.angle = random( TAU );
  var speed = 1.0/1000;
  this.vx = speed * cos( this.angle );
  this.vy = speed * sin( this.angle );
  this.invMass = 1;
  this.radius = 24/1000;

  this.color = clr;
  this.keyLeft = keyLeft;
  this.keyRight = keyRight;
  this.keyFire = keyFire;
  this.keyThrust = keyThrust;
  this.shotClock = 0;
  this.isThrusting = false;
  this.energy = maxEnergy;
  this.ex = ex;
  this.ey = ey;
  this.deathClock = 0;

  this.respawn = function() {
    this.x = random( 1 );
    this.y = random( 1 );

    this.angle = random( TAU );
    var speed = 1.0/1000;
    this.vx = speed * cos( this.angle );
    this.vy = speed * sin( this.angle );
    this.energy = maxEnergy;
    this.shotClock = 0;
  }

  this.checkControls = function() {
    if( this.deathClock > 0 ) { return; }
    if( keyIsDown( this.keyLeft )) {
      this.turn(-1);
    }
    if( keyIsDown( this.keyRight ) ) {
      this.turn( 1 );
    }
    if( keyIsDown( this.keyFire ) ) {
      this.fireShot();
    }
    if( keyIsDown( this.keyThrust ) ) {
      this.thrust();
    }
    
  }

  this.turn = function( dir ) {
    if( this.energy > 30 ) {
      this.angle += 0.04 * dir;
      this.energy -= 1;
    }
  }
  
  this.fireShot = function() {
    if( this.shotClock < 1 && this.energy > 150 ) {
      shots.push( new createShot(this.x, this.y, this.vx, this.vy, this.angle, this.color) );
      this.shotClock = shotDelayFrames;
      this.energy -= shotCost;
    }
  }
  
  this.thrust = function() {
    if( this.energy > 100 ) {
      this.vx += thrustAccelPerFrame * cos( this.angle );
      this.vy += thrustAccelPerFrame * sin( this.angle );
      this.isThrusting = true;
      this.energy -= thrustCostPerFrame;
    }
  }
  
  this.move = function() {
    if( this.deathClock > 0 ) {
      this.deathClock--;
      if( this.deathClock == 0 ) {
        this.respawn();
      }
    }
    if( this.energy < 0 ) {
      this.deathClock = deathFrames;
      this.energy = maxEnergy;
    }
    var speed = sqrt(this.vx * this.vx + this.vy * this.vy);
    if( speed > 1/1000 ) {
      this.vx *= 0.996;
      this.vy *= 0.996;
    }
    this.x += this.vx;
    this.y += this.vy;
    this.x = (this.x + 1) % 1;
    this.y = (this.y + 1) % 1;
    if( this.shotClock > 0 ) {
      this.shotClock--;
    }
    this.energy =
      min(maxEnergy, this.energy+energyGainedPerFrame);
  }
  
  this.show = function() {
    if( this.deathClock > 0 ) {
      noStroke();
      fill( this.color, 30, 100, 100 * this.deathClock/deathFrames );
      ellipse( this.x, this.y, 2*this.radius, 2*this.radius );
      return;
    }
    push();
    translate( this.x, this.y );
    rotate( this.angle );
    noStroke();
    fill( this.color, 100, 60, 60 );
    ellipse( 0, 0, 48/1000, 48/1000 );
    scale( 2.0/1000 );
 
    // wings
    fill( (1/12 + this.color) % 1, 90, 90 );
    triangle( 12, 0, -3, -2, -8, -8 );
    triangle( 12, 0, -3,  2, -8,  8 );
    
    // body
    fill( this.color, 80, 80 );
    triangle( 12, 0, -3, -2, -3, 2 );
    
    // tail
    fill( (11/12+this.color)%1, 70, 90 );
    triangle( 0, 0, -6, -2, -6, 2 );
    
    // thrust
    if( this.isThrusting ) {
      fill( 2/3, 20, 100 );
      triangle( -11, 0, -6, -1, -6, 1 );
      this.isThrusting = false;
    }
    pop();
  }
  
  this.showEnergy = function() {
    push();
    strokeWeight(0.001);
    stroke( 0, 0, 100 );
    fill( this.color, 100, 40, 50 );
    rect( ex, ey, 0.16, 0.02 );
    if( this.deathClock == 0 ) {
      noStroke();
      fill( this.color, 100, 100, 60 );
      rect( ex, ey, this.energy / maxEnergy * 0.16, 0.02 );
    }
    pop();
  }
}


function setup() {
  var canvasSize = min( windowWidth, windowHeight );
  createCanvas( canvasSize, canvasSize );
  background( 0 );
  colorMode( HSB, 1, 100, 100, 100 );
  
  rocks.push( new createRock );
  for( var i=1; i<nRocks; i++ ) {
    var r = new createRock;
    var isOverlapping;
    do {
      isOverlapping = false;
      for( var j=0; j<rocks.length; j++ ) {
        var dx = (rocks[j].x - r.x + 1.5) % 1 - 0.5;
        var dy = (rocks[j].y - r.y + 1.5) % 1 - 0.5;
        if( dx*dx+dy*dy < sq(r.radius+rocks[j].radius) ) {
          isOverlapping = true;
        }
      }
      if( isOverlapping ) {
        r.x = random( 1 );
        r.y = random( 1 );
      }
    } while( isOverlapping );
    rocks.push( r );
  }

  ships.push( new createShip( 0, 0.02, 0.96, LEFT_ARROW, RIGHT_ARROW, UP_ARROW, DOWN_ARROW ) );
  ships.push( new createShip( 1./3., 0.02, 0.92, 74, 76, 73, 75 ) );
  ships.push( new createShip( 2./3., 0.02, 0.88, 65, 68, 87, 83 ) );
}


function doBounce( r1, r2 ) {
  var f = 0;
  var dx = (r2.x - r1.x + 1.5) % 1 - 0.5;
  var dy = (r2.y - r1.y + 1.5) % 1 - 0.5;
  var d2 = dx*dx+dy*dy;
  if( d2 < sq(r1.radius+r2.radius) ) {
    // bump
    var d = sqrt(d2);
    dx /= d;
    dy /= d;
    var dvx = r2.vx - r1.vx;
    var dvy = r2.vy - r1.vy;
    var vn = dx * dvx + dy * dvy;
    if( vn < 0 ) {
      f = 2 * vn / (r1.invMass + r2.invMass);
      r1.vx += f * r1.invMass * dx;
      r1.vy += f * r1.invMass * dy;
      r2.vx -= f * r2.invMass * dx;
      r2.vy -= f * r2.invMass * dy;
    }
  }
  return -f;
}


function draw() {
  background( 0, 128 );
  scale( min( width, height ) );

  for( var i=0; i<ships.length; i++ ) {
    ships[i].checkControls();
  }

  for( var i=0; i<rocks.length; i++) {
    var r1 = rocks[i];
    r1.move();
    for( var j=0; j<i; j++ ) {
      doBounce( r1, rocks[j] );
    }
  }

  for( var i=shots.length-1; i>=0; i-- ) {
    shots[i].move();
    var hit = false;
    for( var j=0; j<rocks.length; j++ ) {
      var dx = (rocks[j].x - shots[i].x + 1.5) % 1 - 0.5;
      var dy = (rocks[j].y - shots[i].y + 1.5) % 1 - 0.5;
      if( dx*dx+dy*dy < sq(rocks[j].radius) ) {
        hit = true;
      }
    }
    for( var j=0; j<ships.length; j++ ) {
      if( shots[i].color != ships[j].color ) {
        var dx = (ships[j].x - shots[i].x + 1.5) % 1 - 0.5;
        var dy = (ships[j].y - shots[i].y + 1.5) % 1 - 0.5;
        if( dx*dx+dy*dy < sq(ships[j].radius) ) {
          hit = true;
          if( ships[j].deathClock == 0 ) {
            ships[j].energy -= shotDamage;
          }
        }
      }
    }
    if( shots[i].life <= 0 || hit ) {
      shots[i] = shots[shots.length-1];
      shots.length--;
    }
  }

  for( var i=0; i<ships.length; i++ ) {
    var s1 = ships[i];
    s1.move();
    if( s1.deathClock == 0 ) {
      for( var j=0; j<rocks.length; j++ ) {
        s1.energy -= 100000 * doBounce( s1, rocks[j] );
      }
      for( var j=0; j<i; j++ ) {
        if( ships[j].deathClock == 0 ) {
          var f = 10000 * doBounce( s1, ships[j] );
          s1.energy -= f;
          ships[j].energy -= f;
        }
      }
    }
  }

  for( var i=0; i<rocks.length; i++) {
    rocks[i].show();
  }
  for( var i=0; i<shots.length; i++ ) {
    shots[i].show();
  }
  for( var i=0; i<ships.length; i++ ) {
    ships[i].show();
  }
  for( var i=0; i<ships.length; i++ ) {
    ships[i].showEnergy();
  }
  
  push();
  noStroke();
  fill( 0.16, 100, 100 );
  scale(2/1000);
  text( floor(frameCount / millis() * 100000)/100, 10, 20 );
  //var speed = sqrt(ships[0].vx*ships[0].vx+ships[0].vy*ships[0].vy);
  //text( floor(speed * 1000 * 1000), 10, 40 );
  pop();
}
 

function windowResized() {
  var canvasSize = min( windowWidth, windowHeight );
  resizeCanvas( canvasSize, canvasSize );
}


              
            
!
999px

Console