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: #111111;
  background-image: url("https://ak0.picdn.net/shutterstock/videos/13396610/thumb/1.jpg");
  text-align: center;
}

canvas {
  border: #103513 3px solid;
}
              
            
!

JS

              
                var rekt;
var food;
var enemies;
var score;
var speed = 2;
var crashSound;
var popSound;
var qNum = 0;
var sNum = 0;
var maxSNum = 10;
var maxNum = 12;

function setup(){
  createCanvas(600,600);
  textAlign(CENTER);
  sNum = Math.floor(random(1, maxSNum));
  qNum = Math.floor(random(1, maxNum));
  rekt = new Rek();
  enemies = [];
  score = 0;
  food = new Food(30, 30, rndDir(), rndDir());
  enemies[0] = new Enemy(20, 20, rndDir(), rndDir());
  crashSound = new sound("https://actions.google.com/sounds/v1/impacts/crash.ogg");
  popSound = new sound("https://actions.google.com/sounds/v1/cartoon/wood_plank_flicks.ogg");
}

function sound(src) {
    this.sound = document.createElement("audio");
    this.sound.src = src;
    this.sound.setAttribute("preload", "auto");
    this.sound.setAttribute("controls", "none");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
        this.sound.play();
    }
    this.stop = function(){
        this.sound.pause();
    }    
    this.rewind = function() {
        this.sound.currentTime = 0; 
    }
}

function Rek() {
  this.x = 300;
  this.y = 300;
  this.size = 30;
  this.show = function() {
    fill(220);
    stroke(160, 80, 100);
    strokeWeight(3);
    rect(this.x, this.y, this.size, this.size, 5);
    stroke(0);
    strokeWeight(1);
  }
}

function Food(x, y, velX, velY) {
  this.x = x;
  this.y = y;
  this.w = 60;
  this.velX = velX;
  this.velY = velY;
  this.show = function() {
    var red = map(this.x, 0, 600, 150, 255);
    var green = map(this.y, 0, 600, 150, 255);
    fill(red, green, sin(frameCount / 15) * 125 + 125);
    stroke(255);
    strokeWeight(2);
    rect(this.x, this.y, this.w, this.w, 5);
    fill(0);
    text(sNum * qNum, this.x + this.w / 2, this.y + 40);
    this.x+= this.velX;
    this.y+= this.velY;
    if (this.y <= 0 || this.y >= height - this.w) {
      this.velY *= -1;
    }
    if (this.x <= 0 || this.x >= width - this.w) {
      this.velX *= -1;
    }
    if (this.x + this.w > rekt.x && this.y + this.w > rekt.y && this.x < rekt.x + rekt.size && this.y < rekt.y + rekt.size) {
      popSound.rewind();
      popSound.play();
      score++;
      rekt.size+= 0.2;
      this.pickSpot();
      sNum = Math.floor(random(1, maxSNum));
      qNum = Math.floor(random(1, maxNum));
      for (var i = 0; i < enemies.length; i++) {
        enemies[i].pickSpot();
        enemies[i].pickRnd();
      }
    }
  }
  this.pickSpot = function() {
    var newX = random(0, width - this.w);
    var newY = random(0, height - this.w);
    if (dist(newX, newY, rekt.x, rekt.y) < rekt.size + 200) {
      this.pickSpot();
    } else {
      food = new Food(newX, newY, rndDir(), rndDir())
    }
  }
}

function Enemy(x, y, velX, velY) {
  this.x = x;
  this.y = y;
  this.w = 60;
  this.velX = velX;
  this.velY = velY;
  this.rnd = Math.floor(random(10) + 1);
  this.show = function() {
    var red = map(this.x, 0, 600, 150, 255);
    var green = map(this.y, 0, 600, 150, 255);
    fill(red, green, sin(frameCount / 15) * 125 + 125);
    stroke(255);
    strokeWeight(2);
    rect(this.x, this.y, this.w, this.w, 5);
    fill(0);
    text(sNum * qNum + this.rnd, this.x + this.w / 2, this.y + 40);
    this.x+= this.velX;
    this.y+= this.velY;
    if (this.y <= 0 || this.y >= height - this.w) {
      this.velY *= -1;
    }
    if (this.x <= 0 || this.x >= width - this.w) {
      this.velX *= -1;
    }
    if (this.x + this.w > rekt.x && this.y + this.w > rekt.y && this.x < rekt.x + rekt.size && this.y < rekt.y + rekt.size) {
      crashSound.play();
      alert("GAME OVER! Final score: " + score);
      setup();
    }
  }
  this.pickSpot = function() {
    var newX = random(0, width - this.w);
    var newY = random(0, height - this.w);
    if (dist(newX, newY, rekt.x, rekt.y) < rekt.size + 200) {
      this.pickSpot();
    } else {
      this.x = newX;
      this.y = newY;
      this.velX = rndDir();
      this.velY = rndDir();
    }
  }
  this.pickRnd = function() {
    var rand = Math.floor(random(-20, 20));
    if (rand === 0) {
      this.pickRnd();
    } else {
      this.rnd = rand;
    }
  }
}

function draw(){
  background(0, 0, 0);
  for (var i = 0; i < enemies.length; i++) {
    enemies[i].show();
  }
  food.show();
  fill(255);
  textSize(30);
  text(sNum + " x " + qNum, 300, 60);
  textSize(20);
  text("Score: " + score, 300, 540);
  textSize(30);
  if (floor(score/10)+1 > enemies.length) {
    enemies.push(new Enemy(0, 0, rndDir(), rndDir()));
    enemies[enemies.length-1].pickSpot();
  }
  
  if (keyIsDown(RIGHT_ARROW)) {
    rekt.x+= 3 * speed;
  }
  if (keyIsDown(LEFT_ARROW)) {
    rekt.x-= 3 * speed;
  }
  if (keyIsDown(DOWN_ARROW)) {
    rekt.y+= 3 * speed;
  }
  if (keyIsDown(UP_ARROW)) {
    rekt.y-= 3 * speed;
  }
  if (keyIsDown(16)) {
    speed = 3;
  } else {
    speed = 2;
  }
  
  rekt.x = constrain(rekt.x, 0, width-rekt.size);
  rekt.y = constrain(rekt.y, 0, height-rekt.size);
  
  rekt.show();
}

function rndDir() {
  return random(-3, 3);
}
              
            
!
999px

Console