03 - BackGrounds.lua

Last-modified: 2009-02-05 (木) 22:01:04

ここのページを読む前に8ビット背景の項を参照することをオススメします。


(scripts/03 - BackGrounds.lua)

01:SCREEN_BOTTOM   = 0
02:SCREEN_TOP      = 1
03:
04:
05:BGTopText = Screen.LoadTextBG()
06:BGTop8Bit = Screen.Load8BitBG()
07:BGBotText = Screen.LoadTextBG()
08:BGBot8Bit = Screen.Load8BitBG()
09:
10:Screen.Initialize( SCREEN_TOP, BGTopText, BGTop8Bit )
11:Screen.Initialize( SCREEN_BOTTOM, BGBot8Bit, BGBotText )
12:
13:BGTop8Bit:SetPaletteColor( 0,  0,  0,  0 )
14:BGTop8Bit:SetPaletteColor( 1, 16,  0,  0 )
15:BGTop8Bit:SetPaletteColor( 2,  0, 16,  0 )
16:BGTop8Bit:SetPaletteColor( 3, 16, 16,  0 )
17:BGBot8Bit:SetPaletteColor( 0,  0,  0,  0 )
18:BGBot8Bit:SetPaletteColor( 1, 16,  0,  0 )
19:BGBot8Bit:SetPaletteColor( 2,  0, 16,  0 )
20:BGBot8Bit:SetPaletteColor( 3, 16, 16,  0 )
21:
22:nCount  = 0
23:nColor  = 0
24:while true do
25:  nCount  = nCount + 1
26:
27:  BGTopText:SetColor( 5 );
28:  BGTopText:PrintXY( 0, 0, "BGTopText-" .. tostring( BGTopText ) )
29:  BGTopText:PrintXY( 0, 1, "BGTop8Bit-" .. tostring( BGTop8Bit ) )
30:  BGTopText:PrintXY( 0, 3, tostring( nCount ) )
31:  BGTopText:PrintXY( 4, 10, "Text background in front" )
32:  BGTopText:PrintXY( 7, 12, "Press START to exit" )
33:
34:  BGBotText:SetColor( 5 );
35:  BGBotText:PrintXY( 0, 0, "BGBotText-" .. tostring( BGBotText ) )
36:  BGBotText:PrintXY( 0, 1, "BGBot8Bit-" .. tostring( BGBot8Bit ) )
37:  BGBotText:PrintXY( 0, 3, tostring( nCount ) )
38:  BGBotText:PrintXY( 5, 10, "Text background behind" )
39:  BGBotText:PrintXY( 7, 12, "Press START to exit" )
40:
41:  for i=0,63 do
42:    nColor  = math.mod( i, 4 )
43:    x0      = ( 63 - i ) * 4
44:    y1      = i * 3
45:    nColor  = nColor + 1
46:    BGTop8Bit:Line( x0, 0, 0, y1, nColor )
47:    BGTop8Bit:Line( 255-x0, 191, 255, 191-y1, nColor )
48:
49:    BGBot8Bit:Line( x0, 0, 0, y1, nColor )
50:    BGBot8Bit:Line( 255-x0, 191, 255, 191-y1, nColor )
51:  end
52:
53:  if Pads.Start() then
54:    break
55:  end
56:
57:  Screen.WaitForVBL()
58:end

プログラムの解説

01:SCREEN_BOTTOM   = 0
02:SCREEN_TOP      = 1
05:BGTopText = Screen.LoadTextBG()
06:BGTop8Bit = Screen.Load8BitBG()
07:BGBotText = Screen.LoadTextBG()
08:BGBot8Bit = Screen.Load8BitBG()
10:Screen.Initialize( SCREEN_TOP, BGTopText, BGTop8Bit )
11:Screen.Initialize( SCREEN_BOTTOM, BGBot8Bit, BGBotText )

いつもの初期化処理ですが、今回はScreen.Load8BitBG()で8ビットのイメージファイルを扱います。
また、Screen.Initialize()時に第3引数を指定することで画面に複数のオブジェクトを設定しています。

 
13:BGTop8Bit:SetPaletteColor( 0,  0,  0,  0 )
14:BGTop8Bit:SetPaletteColor( 1, 16,  0,  0 )
15:BGTop8Bit:SetPaletteColor( 2,  0, 16,  0 )
16:BGTop8Bit:SetPaletteColor( 3, 16, 16,  0 )
17:BGBot8Bit:SetPaletteColor( 0,  0,  0,  0 )
18:BGBot8Bit:SetPaletteColor( 1, 16,  0,  0 )
19:BGBot8Bit:SetPaletteColor( 2,  0, 16,  0 )
20:BGBot8Bit:SetPaletteColor( 3, 16, 16,  0 )

パレットに色を設定しています。
上から順に黒、赤、緑、黄。

 
22:nCount  = 0
23:nColor  = 0

変数の宣言と初期化処理

 
24:while true do

無限ループ。終端は58行目

 
25:  nCount  = nCount + 1

ループ1回ごとにnCountを増やしていきます。

for nCount=1,true do
  :
end

上記のように書き換えることも出来ます(たぶん)

 
27:  BGTopText:SetColor( 5 );
28:  BGTopText:PrintXY( 0, 0, "BGTopText-" .. tostring( BGTopText ) )
29:  BGTopText:PrintXY( 0, 1, "BGTop8Bit-" .. tostring( BGTop8Bit ) )
30:  BGTopText:PrintXY( 0, 3, tostring( nCount ) )
31:  BGTopText:PrintXY( 4, 10, "Text background in front" )
32:  BGTopText:PrintXY( 7, 12, "Press START to exit" )

画面に文字を表示しています。
上から順にオブジェクト名×2、ループ回数、「テキストは背景の前」、「STARTで終了」と書かれています。
しかし、SetColor()で黒色を設定してしまっているので文字が非常に見づらいです。

 
34:  BGBotText:SetColor( 5 );
35:  BGBotText:PrintXY( 0, 0, "BGBotText-" .. tostring( BGBotText ) )
36:  BGBotText:PrintXY( 0, 1, "BGBot8Bit-" .. tostring( BGBot8Bit ) )
37:  BGBotText:PrintXY( 0, 3, tostring( nCount ) )
38:  BGBotText:PrintXY( 5, 10, "Text background behind" )
39:  BGBotText:PrintXY( 7, 12, "Press START to exit" )

こちらは上の処理を下画面に表示しているだけです。
やはり文字列が見えないので適当な色に変更した方がいいでしょう。

 
41:  for i=0,63 do

forループ。
iは0から始まり63まで(64回)処理を続ける。増分未設定なので1

 
42:    nColor  = math.mod( i, 4 )

iを4で割って余りを取得。
nColorは後でパレット番号として使います。
設定したパレットは4つなので1ループごとに交互になります。

 
43:    x0      = ( 63 - i ) * 4
44:    y1      = i * 3

このあたりの説明は大事じゃないので省略。
簡単に説明するとX座標は4ドットごと、Y座標は3ドットごとに線を引くということになります。

 
45:    nColor  = nColor + 1

42行目より、nColorは0~3なのですが、ここで+1しているので値は1~4になります。
パレットは0~3までしか指定していないので設定いない場所を参照する事になっています……。
(50行目の後に加算した方がいいような……?)

 
46:    BGTop8Bit:Line( x0, 0, 0, y1, nColor )
47:    BGTop8Bit:Line( 255-x0, 191, 255, 191-y1, nColor )
49:    BGBot8Bit:Line( x0, 0, 0, y1, nColor )
50:    BGBot8Bit:Line( 255-x0, 191, 255, 191-y1, nColor )

上で計算した座標に線を引いています。

 
51:  end

41行目のforに対応するend

 
53:  if Pads.Start() then
54:    break
55:  end

STARTボタンでプログラムの終了

 
57:  Screen.WaitForVBL()

いつものFPS固定

 
58:end

24行目のwhileに対応するend

 

コメント