More ... | HSPコンテスト:ショートプログラム:HSP基礎知識編:二次元配列を使う
16 個の四角形をランダムな場所にランダムな色で書いてください。 ただし四角形の座標や色情報は変更できるようにすべて変数に入れてから描画すること。 いちばん簡単に思いつくのはコレです。 #const MainWindowId 0
#const WindowWidth 640
#const WindowHeight 480
#const MaxColor 256
#const RectCount 16
repeat RectCount
rnd reds.cnt, MaxColor
rnd greens.cnt, MaxColor
rnd blues.cnt, MaxColor
rnd lefts.cnt, WindowWidth
rnd tops.cnt, WindowHeight
rnd rights.cnt, WindowWidth
rnd bottoms.cnt, WindowHeight
loop
;-------------------------------------------------------
repeat RectCount
color reds.cnt, greens.cnt, blues.cnt
boxf lefts.cnt, tops.cnt, rights.cnt, bottoms.cnt
loop
stop
コレで問題ありません。 が、やっぱり四角形は16個じゃ足りなくなりました。100個描画してください。 こうなりました。 #const MainWindowId 0
#const WindowWidth 640
#const WindowHeight 480
#const MaxColor 256
#const RectCount 100
dim reds, RectCount
dim greens, RectCount
dim blues, RectCount
dim lefts, RectCount
dim tops, RectCount
dim rights, RectCount
dim bottoms, RectCount
repeat RectCount
rnd reds.cnt, MaxColor
rnd greens.cnt, MaxColor
rnd blues.cnt, MaxColor
rnd lefts.cnt, WindowWidth
rnd tops.cnt, WindowHeight
rnd rights.cnt, WindowWidth
rnd bottoms.cnt, WindowHeight
loop
;-------------------------------------------------------
repeat RectCount
color reds.cnt, greens.cnt, blues.cnt
boxf lefts.cnt, tops.cnt, rights.cnt, bottoms.cnt
loop
stop
HSP の変数は初期状態で 16 個しか要素を持っていないので、それ以上使うためには dim してやらなければいけません。 そこでこうします。 #const MainWindowId 0
#const WindowWidth 640
#const WindowHeight 480
#const MaxColor 256
#const RectCount 160
#const Red 0
#const Green 1
#const Blue 2
#const Left 3
#const Top 4
#const Right 5
#const Bottom 6
#const FieldCount 7
dim rects, FieldCount, RectCount
repeat RectCount
dup rect, rects.0.cnt
rnd rect.Red, MaxColor
rnd rect.Green, MaxColor
rnd rect.Blue, MaxColor
rnd rect.Left, WindowWidth
rnd rect.Top, WindowHeight
rnd rect.Right, WindowWidth
rnd rect.Bottom, WindowHeight
loop
;-------------------------------------------------------
repeat RectCount
dup rect, rects.0.cnt
color rect.Red, rect.Green, rect.Blue
boxf rect.Left, rect.Top, rect.Right, rect.Bottom
loop
stop
今までばらばらに持っていた座標や色の情報を、すべて同じ rects という変数に保存しています。 |