昔あったQIXという陣取りゲームに出てくるあの南京玉すだれみたいなやつを書いてみることにする。
例によって基本方針。
- 2匹のタートルをエリアの壁に反射させながら動かす。
- 3匹目が両タートルの間に線を描画する。
- 一本ごとに図形化し配列に格納する。古くなった線は消していく。
こんな感じでそれっぽくなるだろうか。
LINENUM = 20.
//Screen ! (BLACK) paint.
t1 = Turtle ! create.
// fill array of lines.
lines = Array ! create.
[ lines ! ( t1 ! makeFigure ) add] ! (LINENUM) repeat.
// make wall
t1 ! penup 20 linewidth -200 200 moveto pendown 400 4 polygon.
wall = t1 ! makeFigure.
t2 = Turtle ! create.
t1 ! penup (random(360) -190 ) (random(360) -190 ) moveto (random(360)) direction 0.1 scale.
t2 ! penup (random(360) -190 ) (random(360) -190 ) moveto (random(360)) direction 0.1 scale.
t1:collision = [ | target moveself |
[ target == wall ] ! then [ ! (target) (moveself) bounce ] execute. ].
t2:collision = [ | target moveself |
[ target == wall ] ! then [ ! (target) (moveself) bounce ] execute. ].
// create 3rd turtle for drawing.
t3 = Turtle ! create.
t3 ! "" setShape.
t3 ! 1 linewidth (blue) linecolor.
timer1 = Timer ! create.
timer1 ! 0.05 interval 600 duration.
timer1 ! [
t1 ! 10 forward.
t2 ! 10 forward.
t3 ! penup (t1!xposition?) (t1!yposition?) moveto.
t3 ! pendown (t2!xposition?) (t2!yposition?) moveto.
// t3 ! (Color ! (random(127)+128) (random(127)+128) (random(127)+128) create) lineColor.
lines ! (t3 ! makeFigure) add.
(lines ! 1 get) ! hide.
lines ! 1 removePos.
] execute.
それっぽくなった。
衝突判定は、タートル同士が干渉しないように壁にのみに反応するようにしてある。 反射して動き回る両端タートルは存在しない画像をロードして消すと衝突判定がなくなってしまうので小さくしてある。よく見ると見えている。タートルが小さくて衝突判定のマージンがないので壁を十分厚くしておかないとすり抜けることがあるので注意。
... //Screen ! (BLACK) paint. ... // t3 ! (Color ! (random(127)+128) (random(127)+128) (random(127)+128) create) lineColor.のコメントアウトを外すと線の色がランダムに変わる。順次色が遷移するようにしたほうがきれいだけどそこは手抜き。背景が白いままだと見にくくなるので黒にしてある。
線は1本ごとに、makeFigure して配列に入れている。最大線数を超えたものは配列から除外して hide しているのだが、見えなくなっているだけで実は消えていないのではないだろうか?
ドリトルでこの辺のメモリ管理がどうなっているのかは不明。

0 件のコメント:
コメントを投稿