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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <canvas id='world'></canvas>
            
          
!

CSS

            
              body { background-color: #111111; }
            
          
!

JS

            
              // forked from siouxcitizen's "canvasでloopで繰り返し矩形領域ぬりつぶし" http://jsdo.it/siouxcitizen/7EOc
//canvas上の移動する矩形領域の当たり判定を追加してみました
var ctx;
var xPos = 20;
var yPos = 37;
var jikiWidth = 30;
var jikiHeight = 30;
var tekiWidth = 30;
var tekiHeight = 30;
var tekiXPos = 20;
var tekiYPos = 0;
var gameEnd = false;
var jikiSpeed = 10;
var tekiSpeed = 5;

init();
setInterval(loop, 100);

function init() {
  var canvas = document.getElementById('world');
  canvas.width = 450;
  canvas.height = 440;
  if ( ! canvas || ! canvas.getContext ) { return false; }
  //キープレスイベントを処理
  document.addEventListener('keypress', handleKeyPress, false);
  ctx = canvas.getContext('2d');
  //矩形領域をぬりつぶし
  //自機
  ctx.fillStyle = "#ff0000";
  ctx.fillRect(200, 370, jikiWidth , jikiHeight);
  //敵機
  ctx.fillStyle = "#0000ff";
  ctx.fillRect(200, 0, tekiWidth , tekiHeight);
}

//↑↓←→ボタンで自機操作
function handleKeyPress(event) {
  if (event.keyCode==38) {//↑ボタン押下
    if (yPos > 0) {yPos--;}
  } else if (event.keyCode==40) {//↓ボタン押下
    if (yPos < 37) {yPos++;}
  } else if (event.keyCode==39) {//→ボタン押下
    if (xPos < 40) {xPos++;}
  } else if (event.keyCode==37) {//←ボタン押下
    if (xPos > 0) {xPos--;}
  }
}

function loop() {
  if(gameEnd === true) {return;}
  ctx.clearRect(0,0,450,440);
  //矩形領域をぬりつぶし(自機用)
  ctx.fillStyle = "#ff0000";
  ctx.fillRect(jikiSpeed *xPos, jikiSpeed *yPos, jikiWidth, jikiHeight);
  //tekiXPos
  tekiYPos++;
  //矩形領域をぬりつぶし(敵用)
  ctx.fillStyle = "#0000ff";
  ctx.fillRect(200, tekiSpeed *tekiYPos, tekiWidth, tekiHeight);
  if (checkCollision()) {
    gameEnd = true;
    ctx.font = "20px 'Monotype Corsiva'";
    ctx.fillStyle = "#00ff00";
    ctx.fillText("矩形領域が衝突しました", 130, 40);
  }
  if (tekiYPos > 88) {tekiYPos =0;}
}

function checkCollision() {
  var jikiCenterXPos = 10*xPos+jikiWidth/2;
  var jikiCenterYPos = 10*yPos+jikiHeight/2;
  var tekiCenterXPos = 200+tekiWidth/2;
  var tekiCenterYPos = 5*tekiYPos+tekiHeight/2;
  var dist = Math.sqrt(Math.pow((jikiCenterXPos-tekiCenterXPos),2) + Math.pow((jikiCenterYPos -tekiCenterYPos),2));
  if (dist<30) {return true;}
}

            
          
!
999px

Console