1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[enchant.js]格子(グリッド,grid)を描画する関数[javascript]

Last updated at Posted at 2019-09-10

オセロや将棋のゲームを作りたい!
と思ったら、まずは格子を用意しなければならない。
簡単に作るために関数(メソッド)化してみた

格子を描画するメソッド

左上の座標が(lx, ly)
横のマスの数が x, 縦のマスの数が y
マスの幅が len
である格子(グリッド)を描画するメソッド

avascript:main.js

function draw_grid(lx, ly, x, y, len){
    sprite = new Sprite(320, 320);
    surface = new Surface(320, 320);
    sprite.image = surface;
    context = surface.context;
    for(var i=0; i<=x; i++){
        context.beginPath();
        context.moveTo(lx, ly + (len * i));
        context.lineTo(lx + (len * y), ly + (len * i));
        context.closePath();
        context.stroke();
    }
    for(var i=0; i<=y; i++){
        context.beginPath();
        context.moveTo(lx + (len * i), ly);
        context.lineTo(lx + (len * i), ly + (len * x));
        context.closePath();
        context.stroke();
    }
}

ブラウザでの見た目

因みに、

main.js
draw_grid(10, 30, 4, 3, 30);

と呼び出すと、

bandicam 2019-09-10 14-06-53-015.jpg

このような表示になる

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?