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

              
                <!-- 

Visit the full version of the game at:
https://github.com/enesser/retroTanks

-->


<canvas id="canvas" width="800" height="400"></canvas>

<div class="tank info">
  <div><label>Tank x:</label><span class="x"></span></div>
  <div><label>Tank y:</label><span class="y"></span></div>
  <div><label>Tank angle:</label><span class="angle"></span></div>
  <div><label>Tank mod:</label><span class="mod"></span></div>
</div>

<div class="impact info">
  <div><label>Impact with Object:</label><span class="object"></span></div>  
</div>
              
            
!

CSS

              
                body {
  background: #2d2d2d;
  color: white;  
  height: 100%;
  margin: 0;
}

canvas {
  height: 100%;  
  width: 100%;    
}

.info {
  font-size: 1.6em;
  font-families: 'Lucida Console', Monaco, monospace;        
  margin-top: .1em;
  padding: 1em;
  
  label {
    display: inline-block;
    width: 10em;    
  }
}

/*
  CSS scan lines effect adapted from an example by Meduzen on Codepen.
  https://codepen.io/meduzen/pen/zxbwRV
 */

.scanlines {
  overflow: hidden;
  position: relative;
}

.scanlines:before, 
.scanlines:after {
  content: '';
  display: block;
  pointer-events: none;
  position: absolute;
}

.scanlines:before {
  background: rgba(0, 0, 0, 0.3);
  height: 1px;
  opacity: 0.65;
  width: 100%;
  z-index: 2147483649;  
  animation: scanline 8s linear infinite;
}

.scanlines:after {
  background: -webkit-linear-gradient(top, transparent 50%, rgba(0, 0, 0, 0.3) 51%);
  background: linear-gradient(to bottom, transparent 50%, rgba(0, 0, 0, 0.3) 51%);
  background-size: 100% 3px;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 2147483648;
}

/* ANIMATE UNIQUE SCANLINE */
@-webkit-keyframes scanline {
  0% {    
    transform: translate3d(0, 200000%, 0);
  }
}

@keyframes scanline {
  0% {    
    transform: translate3d(0, 200000%, 0);
  }
}

@-webkit-keyframes scanlines {
  0% {
    background-position: 0 50%;
  }
}

@keyframes scanlines {
  0% {
    background-position: 0 50%;
  }
}
              
            
!

JS

              
                /*****************************
/** Graphic Effects***********
/*****************************/
var shadowBlur = true;
var scanlines = true;

/*****************************
/** Color Schemes ************
/*****************************/
var currentScheme = 0;
var colorSchemes = [{
  name: 'experiment 3',
  field: 'rgb(63, 76, 72)',
  walls: 'rgb(149, 151, 122)',
  tank: 'rgb(255, 215, 190)'
}];

/******************************
/** Wall **********************
/******************************/
var Wall = function(x, y, width, height) {
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
};

Wall.prototype.draw = function() {
  context.save();

  if (shadowBlur) {
    context.shadowBlur = 20;
    context.shadowColor = '#000';
  }

  context.fillStyle = colorSchemes[currentScheme].walls;
  context.fillRect(this.x, this.y, this.width, this.height);
  context.restore();
};

/******************************
/** Tank **********************
/******************************/
var Tank = function(x, y) {
  this.height = 33;
  this.width = 47;
  this.x = x;
  this.y = y;
  this.mod = 0;
  this.speed = 5;
  this.angle = 0;
};

Tank.prototype.goForward = function() {
  this.mod = 1;
  this.x = this.x + (this.speed * this.mod) * Math.cos(Math.PI / 180 * this.angle);
  this.y = this.y + (this.speed * this.mod) * Math.sin(Math.PI / 180 * this.angle);
}

Tank.prototype.goBack = function() {
  this.mod = -1;
  this.x = this.x + (this.speed * this.mod) * Math.cos(Math.PI / 180 * this.angle);
  this.y = this.y + (this.speed * this.mod) * Math.sin(Math.PI / 180 * this.angle);
}

Tank.prototype.rotateRight = function() {
  this.angle += 5;
}

Tank.prototype.rotateLeft = function() {
  this.angle -= 5;
}

Tank.prototype.draw = function() {

  var cx = this.x + 0.5 * this.width;
  var cy = this.y + 0.5 * this.height;

  context.save();
  context.translate(cx, cy);
  context.rotate(this.angle * Math.PI / 180);
  context.translate(-cx, -cy);

  if (shadowBlur) {
    context.shadowBlur = 20;
    context.shadowColor = '#000';
  }

  context.fillStyle = colorSchemes[currentScheme].tank;
  context.fillRect(this.x + 20, this.y + 14, 28, 6); //turret
  context.fillRect((this.x + 10), this.y + 6, 18, 24); //body
  context.fillRect((this.x + 0), this.y, 32, 8); //left track
  context.fillRect((this.x + 0), this.y + 26, 32, 8); //right track
  context.restore();
}

var docX = document.querySelector('.tank .x');
var docY = document.querySelector('.tank .y');
var docAngle = document.querySelector('.tank .angle');
var docMod = document.querySelector('.tank .mod');

Tank.prototype.diag = function() {
  docX.innerHTML = this.x;
  docY.innerHTML = this.y;
  docAngle.innerHTML = this.angle;
  docMod.innerHTML = this.mod;
}

/******************************
/** Bullet ********************
/******************************/
var Bullet = function(x, y, angle, mod) {
  this.height = 4;
  this.width = 4;
  this.x = x;
  this.y = y;
  this.mod = mod;
  this.speed = 5;
  this.angle = angle;
  this.impacted = false;
};

Bullet.prototype.goForward = function() {
  this.mod = 1;
  this.x = this.x + (this.speed * this.mod) * Math.cos(Math.PI / 180 * this.angle);
  this.y = this.y + (this.speed * this.mod) * Math.sin(Math.PI / 180 * this.angle);
}

Bullet.prototype.isInHitBox = function(x, y, width, height) {
  return !(
    ((y + height) < (this.y)) ||
    (y > (this.y + this.height)) ||
    ((x + width) < this.x) ||
    (x > (this.x + this.width))
  );
}

Bullet.prototype.draw = function() {

  var cx = this.x + 0.5 * this.width;
  var cy = this.y + 0.5 * this.height;

  context.save();
  context.translate(cx, cy);
  context.rotate(this.angle * Math.PI / 180);
  context.translate(-cx, -cy);

  if (shadowBlur) {
    context.shadowBlur = 4;
    context.shadowColor = 'rgb(255,255,255)';
  }

  context.fillStyle = 'rgb(255,255,255)';
  context.fillRect(this.x + 2, this.y + 2, this.width, this.height);
  context.restore();
}

/******************************
/** Levels ********************
/******************************/
var currentLevel = 0;
var levelHeight = 400;
var levelWidth = 800;

var levels = [{
  name: 'retro tanks classic',
  walls: [
    //interior walls    
    new Wall(680, 100, 14, 200), //right
    new Wall((levelWidth / 2) - 50, (levelHeight / 2) - 40, 100, 80), //middle

    //exterior walls
    new Wall(0, 0, levelWidth, 14), //top
    new Wall(0, 14, 14, levelHeight - 28), //left
    new Wall(levelWidth - 14, 14, 14, levelHeight - 28), //right
    new Wall(0, levelHeight - 14, levelWidth, 14), //bottom	        
  ],
  spawnPoints: [{
    x: 35,
    y: 170
  }]
}];

/******************************
/** Game code *****************
/******************************/

var myTank = new Tank(levels[currentLevel].spawnPoints[0].x,
  levels[currentLevel].spawnPoints[0].y);
var bullets = [];
var keyMap = {};

window.addEventListener('keydown',
  function(event) {
    keyMap[event.keyCode] = true;
    event.stopPropagation();
    event.preventDefault();
  }, false);

window.addEventListener('keyup',
  function(event) {
    keyMap[event.keyCode] = false;

    if (event.keyCode === 32 || event.keyCode === 13) { //space, enter      
      var bullet = new Bullet();
      bullet.x = myTank.x + (myTank.width * 0.5);
      bullet.y = myTank.y + (myTank.height * 0.5);
      bullet.angle = myTank.angle;
      bullet.mod = myTank.mod;
      bullet.impacted = false;
      bullets.push(bullet);
      event.stopPropagation();
      event.preventDefault();
    }
  }, false);

setInterval(function() {
  if (keyMap[87] || keyMap[38]) { //W, up
    myTank.goForward();
  }

  if (keyMap[83] || keyMap[40]) { //S, down
    myTank.goBack();
  }

  if (keyMap[65] || keyMap[37]) { //A, left
    myTank.rotateLeft();
  }

  if (keyMap[68] || keyMap[39]) { //D, right
    myTank.rotateRight();
  }
}, 40);

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

//move bullets 
var moveBullets = function() {
  var bullet = null;
  var wall = null;

  for (var i = 0; i < bullets.length; i++) {
    bullet = bullets[i];

    if (!bullet.impacted) {      
      bullet.goForward();

      for (var i = 0; i < levels[currentLevel].walls.length; i++) {
        wall = levels[currentLevel].walls[i];
        if (bullet.isInHitBox(wall.x, wall.y, wall.width, wall.height)) {
          bullet.impacted = true;          
          document.querySelector('.impact .object').innerHTML = 'wall, x: ' + wall.x.toString() + ', y: ' + ', height: ' + wall.height + ' width: ' + wall.width;          
          break;
        }
      }
    }
  }
};

setInterval(moveBullets, 15);

(function render() {

  context.fillStyle = colorSchemes[currentScheme].field;
  context.fillRect(0, 0, 800, 400);
  context.fillStyle = 'rgb(0, 0, 0)';

  //draw walls
  for (var i = 0; i < levels[currentLevel].walls.length; i++) {
    levels[currentLevel].walls[i].draw();
  }

  //draw bullets
  for (var i = 0; i < bullets.length; i++) {
    bullets[i].draw();
  }

  //draw tank
  myTank.draw();
  myTank.diag();

  window.requestAnimationFrame(render);

})();
              
            
!
999px

Console