動物将棋 (4)
タッチ処理で駒を操作するときのやり方を変更しました。駒を指で動かすときに元画像をワークに退避させていたのですが、それはやめて、移動駒を選んだときに、全体画像をワークにキャッシュして、それを描き戻すやり方に代えました。
グローバル変数の変更
方法変更にともなって、グローバル変数も変更しました。ViewWideとViewHeightをグローバルで持つようにしました。
--グローバル変数宣言---------------------- Version = "0.05" --バージョン Ban={} Ban[1] = { 23,0,0,14 } Ban[2] = { 25,21,11,15 } Ban[3] = { 24,0,0,13 } Mochi = {} Mochi[1] = {0,0,0,0,0,0} Mochi[2] = {0,0,0,0,0,0} BGColor = color(253,227,202) --背景色 ViewWide, ViewHeight = canvas.getviewSize() --画面サイズ Center={ x, y } --画面中心座標 Center.x = ViewWide/2 Center.y = ViewHeight/2
駒の絵を描画する
駒の描画関数が簡単になりました。
------------------------------------------ -- (gx,gy)座標を中心に指定駒を描画する ------------------------------------------ function drawKoma( gx, gy, koma ) local plyer = math.floor(koma/10) local wx = 48*(koma-plyer*10 - 1) local wy = 48*(2-plyer) + 192 --駒を描画 canvas.putg( gx-24, gy-24, gx+23, gy+23, wx, wy, wx+47, wy+47 ) canvas.putflush() end
タッチによる駒移動
移動駒を画面から外した状態でキャッシュしているので、こちらの処理も簡単になりました。
--現在の画面をワークにキャッシュする canvas.getg( 0, 0, ViewWide-1, ViewHeight-1, 260, 0, 259+ViewWide, ViewHeight-1 ) while(true)do --駒移動ループ drawKoma( x, y, ret ) x,y,s = touch() --ワークから描き戻し canvas.putg( 0, 0, ViewWide-1, ViewHeight-1, 260, 0, 259+ViewWide, ViewHeight-1 ) if( s==1 )then break end --指が離れたら抜ける end
タッチされた駒を画面から描画から外す
処理の説明が前後しますが、以下が、タッチされた駒を画面の描画から外すプログラムです。dput()で駒番号0を指定しているだけです。持ち駒を描画するmput()も実装済みです。
while(true)do --駒にタッチするまで待つ x,y = touch(1) ret, hx, hy = getKomaNum() if( ret~=0 and ret~=-1 )then break end end if( hy~=-1 )then --盤上の駒を消す dput( hx, hy, 0 ) else --持ち駒の絵を消す mput( math.floor(ret/10), hx, 0 ) end
タッチ場所情報の取得
getKomaNum()も仕様変更しており、盤上と持ち駒の両方のタッチ情報を取れるようにしています。盤上の条件は以前のプログラムと同じですが、番外のとき、持ち駒をタッチした状態をgetMochiNum()で取れるようにしました。持ち駒をタッチした場合、retには駒番号、hxには持ち駒の番号(Mochi[][hx])、そしてhyは-1となります。
------------------------------------------ -- タッチしたの駒番号を返す。駒が無ければ0を返す。盤外であれば-1を返す -- komanum, hx, hy hy==-1のとき、手ごまの場所だった。 ------------------------------------------ function getKomaNum() local x,y local ret = -1 local hx = 1 local hy = 1 x,y = touch() --将棋盤の範囲内かどうか調べる if( x>=Center.x-108 and y>=Center.y-81 and x<=Center.x+107 and y<=Center.y+80 )then hx = math.floor( (y-(Center.y-81))/55 ) + 1 hy = math.floor( (x-(Center.x-108))/55 ) + 1 ret = Ban[hx][hy] else ret, hx = getMochiNum() hy = -1 end return ret, hx, hy end
持ち駒のタッチ状態取得
持ち駒は最大6個、2列に並べるようにしました。しかも、先手と後手で場所が違うため、ちょっと、複雑な条件文になっています。
------------------------------------------ -- タッチした持ち駒番号を返す。駒が無ければ0を返す。盤外であれば-1を返す -- あれば持ち駒の番号を返す ------------------------------------------ function getMochiNum() local x,y local ret = -1 local num = 0 local i x,y = touch() --持ち駒の場所かどうか調べる for i=1,3 do if( x>=Center.x+128 and x<=Center.x+128+48 and y>=Center.y-96+15+(i-1)*55 and y<=Center.y-96+15+(i-1)*55+48 )then ret = Mochi[1][i] num = i elseif( x>=Center.x-129-48 and x<=Center.x-129 and y>=Center.y-96+15+(i-1)*55 and y<=Center.y-96+15+(i-1)*55+48 )then ret = Mochi[2][i] num = i end end for i=4,6 do if( x>=Center.x+128+55 and x<=Center.x+128+55+48 and y>=Center.y-96+15+(i-4)*55 and y<=Center.y-96+15+(i-4)*55+48 )then ret = Mochi[1][i] num = i elseif( x>=Center.x-129-55-48 and x<=Center.x-129-55 and y>=Center.y-96+15+(i-4)*55 and y<=Center.y-96+15+(i-4)*55+48 )then ret = Mochi[2][i] num = i end end return ret, num end
指が離れた後の処理
指が離れた後の処理は、先ず、指が離れた場所の状態を取得します。これも、getKomaNum()を用いて取得します。
ret1, hx1, hy1 = getKomaNum() --盤面から指が離れたときの状態が返ってくる
駒を進める処理
駒を進める処理は下記のようです。ret1が-1だと、正常位置で指を離していないので、ret1が-1でないときに処理に入ります。さらに、hy1が-1で無いときが盤上で指が離れたときなので、hy1が-1で無いときに、駒を動かす処理にはいります。
さらに、hyが-1のときが、持ち駒を打とうとしているときなので、持ち駒を打つ処理を、それ以外は、盤上の駒を動かした処理を行います。
if( ret1~=-1 )then if( hy==-1 and hy1~=-1 )then --持ち駒を打った if(ret1==0)then --空マスなので、駒を打つ処理 end elseif( hy1~=-1 )then --盤上の駒を動かした if( chkMove( hx, hy, hx1, hy1, ret )==1 )then --動かせる場所だったので、駒を動かす処理 end end end
駒を進める処理は次のブログで書きたいと思います。