まず、初期化の部分を考えます。
1 2 3 4 5 6 7 8 9 10 |
size = 40 // 自キャラサイズ(縦横共通) len = 5 // 初期連結数 dim dir, len dim posx, len dim posy, len repeat len dir.cnt = 4 // 進行方向:右 posx.cnt = 6 - cnt // 初期X位置 posy.cnt = 2 // 初期Y位置 loop |
1 2 |
color 200, 255, 255 boxf |
1 2 3 4 5 |
color 50, 150, 50 repeat len i = posx.cnt * size, posy.cnt * size boxf i.0 + 1, i.1 + 1, i.0 + size - 1, i.1 + size - 1 loop |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 先頭以外の進行方向・位置を変更 repeat len - 1 dir(len - cnt - 1) = dir(len - cnt - 2) posx(len - cnt - 1) = posx(len - cnt - 2) posy(len - cnt - 1) = posy(len - cnt - 2) loop // 先頭の進行方向を変更 stick key, 5 if key & 1 : dir = dir / 2 + (dir <= 1) * 8 // 1⇒8 2⇒1 4⇒2 8⇒4 if key & 4 : dir = dir * 2 - (dir >= 8) * 15 // 1⇒2 2⇒4 4⇒8 8⇒1 // 先頭の位置を変更 posx += (dir = 4) - (dir = 1) // [←] posx-1 [→] posx+1 posy += (dir = 8) - (dir = 2) // [↑] posy-1 [↓] posy+1 |
1
| 2
| 3
| 4
| 5
| 6
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// [←]キー if key = 1 { if dir = 1 : dir = 8 // 左から下に変更 if dir = 2 : dir = 1 // 上から左に変更 if dir = 4 : dir = 2 // 右から上に変更 if dir = 8 : dir = 4 // 下から右に変更 } // [→]キー if key = 4 { if dir = 1 : dir = 2 // 左から上に変更 if dir = 2 : dir = 4 // 上から右に変更 if dir = 4 : dir = 8 // 右から下に変更 if dir = 8 : dir = 1 // 下から左に変更 } |
1 2 3 4 5 |
// 進行方向へ移動 if dir = 1 : posx-- if dir = 2 : posy-- if dir = 4 : posx++ if dir = 8 : posy++ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// 壁への激突判定 if posx < 0 | posy < 0 | posx >= ginfo_winx / size | posy >= ginfo_winy / size { i = -1 // 激突を示すフラグとして使用 } else { // 自分自身への激突判定 repeat len - 1 i = cnt repeat len - i - 1, i + 1 if posx.i = posx.cnt & posy.i = posy.cnt { i = -2 // 激突を示すフラグとして使用 break } loop if i = -2 : break // 激突した loop } if i < 0 { if i = -1 : s = "壁" : else : s = "自分" dialog s + "に激突!", 1, "ゲームオーバー" gosub *init } return |