ねこが歩くデモ
Luarida v1.74Betaのスプライト機能を使って、ねこが歩くデモを作ってみました。
追っかけてくるねこから逃げればいいのですが、白ねこは大きくなって足も速くなります。
とりあえずソースを貼っておきます。ねこ画像は、「漆黒の館」サイトさんからお借りしました。
ドロルアリダに登録しましたので、そちらから一式をダウンロードしてください。/mnt/sdcard/dorokuri/doroluarida/ねこごっこ/以下に展開されるはずです。
--グローバル変数宣言---------------------- Gwidth = 480 Gheight = 320 Cat1={ cnt=0, num=0, x=0, y=0, fr=1/10, sec=0, pich=12, bai =1, dir=6, so=9, walkCnt=0 } Cat2={ cnt=0, num=0, x=0, y=0, fr=1/10, sec=0, pich=10, bai =1, dir=6 , so=29, walkCnt=0 } Tex={ a0=1, b0=0, a1=1, b1=0, sx0=0, sx1=0, sy0=0, sy1=0 }
GwidthとGheightは、後で取得する画面サイズです。Cat1とCat2は表示キャラクタ用の各データです。二体なので特に配列にしていないです。
- cnt キャラのアニメーションパターン0,1,2,3,0,1,2,3と絵が変る感じです。
- num 絵のスプライト番号です。変数に持たさなくても実は都度計算しています。
- x,y 現在キャラがいる座標です。
- fr cntを切り替える、すなわちキャラの絵を更新するタイミングです。1/10だと100msです。
- sec 次にcntを切り替える時間を持っています。
- pich 絵の移動ピッチです。
- bai 表示倍率です。
- dir 進行方向です。0:上移動, 1:右移動, 2:下移動, 3:左移動, 4:上立ち止まり, 5:右立ち止まり, 6:下立ち止まり, 7:左立ち止まりです。
- so キャラによってスプライト番号が違うので、そのオフセット値です。
- walkCnt 歩いた歩数をカウントしています。
タッチを取る
Texというのは、画面のタッチ範囲を取得するために用意した変数です。画面を×に切ったときの上下左右のタッチ位置を取得したかったので、作りました。
- a0 左上から右下にかけて引いた直線の傾きです。
- b0 左上から右下にかけて引いた直線のY切片です。常にゼロですね。
- a1 右上から左下にかけて引いた直線の傾きです。
- b1 右上から左下にかけて引いた直線のY切片です。
- sx0,sy0,sx1,sy1 これは画面の中央エリアを取得したかったので用意した変数です。
下記の初期化時の関数で計算しています。
------------------------------------------ -- いろいろな初期化 ------------------------------------------ function init() Tex.a0 = (0 - Gheight)/(0 - Gwidth ) Tex.b0 = (0*Gheight - Gwidth*0)/(0 - Gwidth) Tex.a1 = (Gheight-0)/(0 - Gwidth ) Tex.b1 = (0*0 - Gwidth*Gheight)/(0 - Gwidth) Tex.sx0 = 4*Gwidth/10 Tex.sx1 = 6*Gwidth/10 Tex.sy0 = 4*Gheight/10 Tex.sy1 = 6*Gheight/10 initCat() -- ネコの初期化 48 x 64 end
gettouchでタッチした場所から上下左右のカーソルキーコードを返しています。62はスペースキーのコードです。画面中央にタッチすると、スペースキーを押したことになります。
function gettouch( key ) local tx local ty local ts tx, ty, ts = touch() if(ts==1)then return key, ts end if(tx>Tex.sx0 and tx<Tex.sx1 and ty>Tex.sy0 and ty<Tex.sy1)then return 62, 0 end local y0 = Tex.a0*tx+Tex.b0 local y1 = Tex.a1*tx+Tex.b1 if(ty<y0)then if( ty<y1)then return 19, 0 else return 22, 0 end else if( ty<y1)then return 21, 0 else return 20, 0 end end return key, 0 end
ネコの初期化 48 x 64
ワーク画面に読み込んだねこの画像からスプライト定義を行います。後、こねの位置の初期化なども行っています。
function initCat() sprite.init() local x = 48 local y = 64 --上 sprite.define( 10, 0*x, 0, 1*x-1, y-1) sprite.define( 11, 1*x, 0, 2*x-1, y-1) sprite.define( 12, 2*x, 0, 3*x-1, y-1) --右 sprite.define( 13, 0*x, 1*y, 1*x-1, 2*y-1) sprite.define( 14, 1*x, 1*y, 2*x-1, 2*y-1) sprite.define( 15, 2*x, 1*y, 3*x-1, 2*y-1) --下 sprite.define( 16, 0*x, 2*y, 1*x-1, 3*y-1) sprite.define( 17, 1*x, 2*y, 2*x-1, 3*y-1) sprite.define( 18, 2*x, 2*y, 3*x-1, 3*y-1) --左 sprite.define( 19, 0*x, 3*y, 1*x-1, 4*y-1) sprite.define( 20, 1*x, 3*y, 2*x-1, 4*y-1) sprite.define( 21, 2*x, 3*y, 3*x-1, 4*y-1) --上 sprite.define( 30, 3*x, 0, 4*x-1, y-1) sprite.define( 31, 4*x, 0, 5*x-1, y-1) sprite.define( 32, 5*x, 0, 6*x-1, y-1) --右 sprite.define( 33, 3*x, 1*y, 4*x-1, 2*y-1) sprite.define( 34, 4*x, 1*y, 5*x-1, 2*y-1) sprite.define( 35, 5*x, 1*y, 6*x-1, 2*y-1) --下 sprite.define( 36, 3*x, 2*y, 4*x-1, 3*y-1) sprite.define( 37, 4*x, 2*y, 5*x-1, 3*y-1) sprite.define( 38, 5*x, 2*y, 6*x-1, 3*y-1) --左 sprite.define( 39, 3*x, 3*y, 4*x-1, 4*y-1) sprite.define( 40, 4*x, 3*y, 5*x-1, 4*y-1) sprite.define( 41, 5*x, 3*y, 6*x-1, 4*y-1) local dir = 6 Cat1.x = Gwidth/2 Cat1.y = Gheight/2 Cat1.so = 9 Cat1.sir = 6 Cat1.walkCnt = 0 drawCat(Cat1) Cat2.x = math.random(Gwidth) Cat2.y = math.random(Gheight) Cat2.so = 29 Cat2.dir = 6 Cat2.walkCnt = 0 drawCat(Cat2) end
ネコの移動
ねこを移動させる関数です。cat変数(テーブル)を引数にもらって、表示画像を決定します。移動するタイミングが来ると、if( cat.sec
function drawCat( cat ) local i if( cat.dir==4 )then --up cat.num = cat.so cat.cnt = 0 elseif( cat.dir==5 )then --right cat.num = cat.so + 5 cat.cnt = 0 elseif( cat.dir==6 )then --down cat.num = cat.so + 8 cat.cnt = 0 elseif( cat.dir==7 )then --left cat.num = cat.so + 11 cat.cnt = 0 end if( cat.sec<system.getSec())then local n = 1 cat.sec = system.getSec() + cat.fr if( cat.cnt==0 )then n = 1 elseif( cat.cnt==1 )then n = 2 elseif( cat.cnt==2 )then n = 3 else n = 2 end cat.cnt = cat.cnt + 1 if(cat.cnt>3)then cat.cnt = 0 end if( cat.dir==0 )then --up cat.y = cat.y - cat.pich if( cat.y<0 )then cat.y = Gheight end cat.num = cat.so + n cat.walkCnt = cat.walkCnt + 1 elseif( cat.dir==1 )then --right cat.x = cat.x + cat.pich if( cat.x>Gwidth )then cat.x = 0 end cat.num = cat.so + 3 + n cat.walkCnt = cat.walkCnt + 1 elseif( cat.dir==2 )then --down cat.y = cat.y + cat.pich if( cat.y>Gheight )then cat.y = 0 end cat.num = cat.so + 6 + n cat.walkCnt = cat.walkCnt + 1 elseif( cat.dir==3 )then --left cat.x = cat.x - cat.pich if( cat.x<0 )then cat.x = Gwidth end cat.num = cat.so + 9 + n cat.walkCnt = cat.walkCnt + 1 end end sprite.move( cat.num, 1, cat.x, cat.y, cat.bai, cat.bai ) end
メインプログラム
手抜きメインプログラムです。Cat1とCat2の移動方向の変更などをメインのwhileループで行っています。カーソルキーの上下左右と画面の上下左右をタッチするとねこが移動します。白ねこに捕まるとゲームオーバーです。Cat2のbreak条件になるとゲームオーバーです。
function main() -- 縦横画面の自動設定 setDirection() Gwidth, Gheight = canvas.getviewSize() --キャラは48×64です。 if( canvas.loadBmp( system.getAppPath().."/cats.png", 0, 0, 287, 255 )==-1)then dialog( system.getAppPath().."/cats.png", "ロードに失敗しました",1 ) return end --いろいろな初期化 init() Cat1.dir = 6 Cat2.dir = 6 local key local stu local bg = color(0,255,0) while(true)do key, stu = inkey() if( stu==1 )then key, stu = gettouch( key ) end if(key==19 and stu==0 )then --上 Cat1.dir = 0 elseif(key==20 and stu==0 )then --下 Cat1.dir = 2 elseif(key==21 and stu==0 )then --left Cat1.dir = 3 elseif(key==22 and stu==0 )then --right Cat1.dir = 1 elseif(key==62)then break else Cat1.dir = 6 end if( math.abs(Cat1.x-Cat2.x)>math.abs(Cat1.y-Cat2.y))then if(Cat1.x>Cat2.x+Cat2.bai*10)then Cat2.dir = 1 elseif(Cat1.x<Cat2.x-Cat2.bai*10)then Cat2.dir = 3 else Cat2.dir = 6 break end else if(Cat1.y>Cat2.y+Cat2.bai*10)then Cat2.dir = 2 elseif(Cat1.y<Cat2.y-Cat2.bai*10)then Cat2.dir = 0 else Cat2.dir = 6 break end end if(Cat2.walkCnt>50)then Cat2.bai = Cat2.bai + 1 Cat2.walkCnt = 0 Cat2.pich = Cat2.pich + 4 end drawCat(Cat1) drawCat(Cat2) canvas.putCls(bg) sprite.put() canvas.putflush() end dialog("", "ゲームオーバー", 1) end main() system.exit()
以上、ゲームになっていないですが、キャラを動かす関数を書いてみましたという感じです。