今回は TextWindow 画面で動きを持たせるプログラムを書いてみました。プログラムを DFZ772-0 として発行しました。所々コメントのみがあってコードがない部分がありますが、そこは未完の部分です。
メインループ
2重のループになっています。外側はゲームの繰り返しです。内側のループはプレイヤー交互に駒を進めるためのループです。
‘ Monopoly Like Game 0.2a
‘ Program ID DFZ772-0
nextGame = “True”
‘ Declare arrays for the squares, chance and community chest
InitBoard()
While nextGame
’ Prepare the game
playerPos[1] = 1 ‘ start position for player 1
playerPos[2] = 1 ‘ start position for player 2
DrawBoard()
’ Start the game
continue = “True”
While continue
’ Play by player 1
player = 1
Play()
If continue Then
’ Play by player 2
player = 2
Play()
EndIf
EndWhile
’ Game ending
’ Ask next game
EndWhile
盤の初期化
すごろくの盤(ボード)を初期化します。各マス目の TextWindow 内での位置(行と桁)を計算しています。
Sub InitBoard
colMax = 11
rowMax = 11
playerColor[1] = “Yellow”
playerColor[2] = “Cyan”
posMax = (colMax + rowMax – 2) * 2
For i = 1 To posMax
If (1 <= i) And (i < rowMax) Then
colPos[i] = 1
rowPos[i] = rowMax + 1 – i
ElseIf (rowMax <= i) And (i <= rowMax + colMax – 2) Then
colPos[i] = i – rowMax + 1
rowPos[i] = 1
ElseIf (colMax + rowMax – 1 <= i) And (i <= 2 * rowMax + colMax – 2) Then
colPos[i] = colMax
rowPos[i] = i – rowMax – colMax + 2
ElseIf (2 * colMax + rowMax – 1 <= i) And (i <= posMax) Then
colPos[i] = posMax – i + 2
rowPos[i] = rowMax
EndIf
EndFor
EndSub
盤の描画
マス目を全て描画します。
Sub DrawBoard
For pos = 1 To posMax
DrawSquare()
EndFor
EndSub
マス目の描画
一つのマス目を描画します。TextWindow で自由な位置に描画するには、TextWindow.CursorLeft と TextWindow.CursorTop に画面上の位置(桁, 行)を指定します。(0, 0) が左上の桁と行です。
Sub DrawSquare
’ param pos
col = colPos[pos]
row = rowPos[pos]
left = (col – 1) * 7
top = (row – 1) * 2
’ Draw top row
TextWindow.CursorLeft = left
TextWindow.CursorTop = top
TextWindow.Write(“+——+”)
top = top + 1
’ Draw center row
TextWindow.CursorLeft = left
TextWindow.CursorTop = top
TextWindow.Write(“|”)
If pos = 1 Then
TextWindow.Write(“GO”)
Else
If pos < 10 Then
TextWindow.Write(" ")
EndIf
TextWindow.Write(pos)
EndIf
TextWindow.ForegroundColor = playerColor[1]
TextWindow.Write(" ") ' reserved for player 1
If playerPos[1] = pos Then
TextWindow.Write("o")
Else
TextWindow.Write(" ")
EndIf
TextWindow.ForegroundColor = playerColor[2]
TextWindow.Write(" ") ' reserved for player 2
If playerPos[2] = pos Then
TextWindow.Write("o")
Else
TextWindow.Write(" ")
EndIf
TextWindow.ForegroundColor = "Gray"
TextWindow.Write("|")
top = top + 1
' Draw bottom row
TextWindow.CursorLeft = left
TextWindow.CursorTop = top
TextWindow.Write("+——+")
top = top + 1
EndSub
プレイヤーの1手
プレイヤーの1手を行います。サイコロを振り、出た目の分だけ駒を進めます。本来は止まったマス目で指示が出たり、いろいろなイベントが起きますが、コメントのみで実際のコードは省略しています。
Sub Play
’ Input some key to roll the dice
TextWindow.CursorLeft = 0
TextWindow.CursorTop = rowMax * 2 + 1
TextWindow.ForegroundColor = playerColor[player]
TextWindow.Write(“Player ” + player +” –> “)
TextWindow.ForegroundColor = “Gray”
TextWindow.Write(“Press enter…”)
TextWindow.Read()
TextWindow.CursorLeft = 13
TextWindow.CursorTop = TextWindow.CursorTop – 1
’ Role the dice
d = Math.GetRandomNumber(6)
TextWindow.ForegroundColor = playerColor[player]
TextWindow.WriteLine(d + ” “)
TextWindow.ForegroundColor = “Gray”
For i = 1 To d
MoveToNext()
DrawBoard()
Sound.PlayClickAndWait()
EndFor
’ Show the player’s tactics
’ If the new position is owned by the other player
’ Try to deduct the rent
’ If player has no rent
’ Sell one of his properties
’ Calculate the remainder
’ Judge of game end
EndSub
駒の動き
駒の位置を playerPos という配列に入れて表しています。
Sub MoveToNext
p = playerPos[player]
p = p + 1
If p > posMax Then
p = 1
EndIf
playerPos[player] = p
EndSub
今回のプログラムは以上です。今回のポイントは TextWindow.CursorLeft と TextWindow.CursorTop を利用して TextWindow のプログラムに動きを持たせたところです。なお、この手のプログラムはブラウザ上(Silverlight の環境)では動作しません。Small Basic IDE でダウンロードして実行してください。
このような方法で TextWindow を使ったシューティングやスクロールゲームも作れると思います。次回からは GraphicsWindow を使ったゲームに挑戦してみたいと思います。
(つづく)
