by ゲスト » 2007年6月21日(木) 20:22
初めまして、Activebaseicの学習用にライフゲームを作ろうとしています。
ActivebasicでWindowsのプログラムをするのは初めてです。
グローバル変数に50x50の配列を用意して、この中にセルの状態値を書き込み、
描画の際に配列の中身を読んで、ウインドウ上に四角を描くようにしたいのです。
試しにボタンクリックごとに、乱数を配列に書き込んで描画することを繰り返しさせた
ところ四回目の書き込みから、指定外の色(白)で描画が起きてしまいます。
何が悪いのでしょうか?アドバイスよろしくお願いします。
' TODO: この位置にグローバルな変数、構造体、定数、関数を定義します。
dim hDC As DWord
const ScreenLeft = 10
const ScreenTop = 10
const ScreenWidthNum = 49
const ScreenHeightNum = 49
const cellSize = 10
dim cells[ScreenWidthNum, ScreenHeightNum] As Integer
'-----------------------------------------------------------------------------
' ウィンドウメッセージを処理するためのコールバック関数
Function MainWndProc(hWnd As HWND, dwMsg As DWord, wParam As WPARAM, lParam As LPARAM) As DWord
' TODO: この位置にウィンドウメッセージを処理するためのコードを記述します。
' イベントプロシージャの呼び出しを行います。
MainWndProc=EventCall_MainWnd(hWnd,dwMsg,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ここから下は、イベントプロシージャを記述するための領域になります。
Sub MainWnd_Destroy()
life_game_DestroyObjects()
PostQuitMessage(0)
End Sub
'初期化コード 配列とスレッド、デバイスコンテキストの取得など
Sub MainWnd_Create(ByRef CreateStruct As CREATESTRUCT)
dim i As Integer
dim j As Integer
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
cells[i, j] = 0
Next j
Next i
End Sub
Sub makeRandom()
dim i As Integer
dim j As Integer
Randomize
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
cells[i, j] = Rnd() * 2
Next j
Next i
End Sub
Sub MainWnd_CommandButtonStart_Click()
makeRandom()
InvalidateRect(hMainWnd, ByVal 0, 0)
End Sub
Sub MainWnd_Paint(hDC As HDC)
dim hBrush As DWord
dim cell As RECT
dim i As Integer
dim j As Integer
dim hWDC As HDC
hWDC =GetDC(hMainWnd)
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
If cells[i,j] = 1 Then
'生きてたら緑
hBrush = CreateSolidBrush(RGB(0, 255, 0))
Else
'死んでたら黒
hBrush = CreateSolidBrush(RGB(0, 0, 0))
End If
cell.left = i * cellSize + ScreenLeft
cell.top = j * cellSize + ScreenTop
cell.right = (i + 1) * cellSize + ScreenLeft
cell.bottom = (j + 1) * cellSize + ScreenTop
FillRect(hWDC, cell, hBrush)
Next j
Next i
ReleaseDC(hWDC, hMainWnd)
End Sub
初めまして、Activebaseicの学習用にライフゲームを作ろうとしています。
ActivebasicでWindowsのプログラムをするのは初めてです。
グローバル変数に50x50の配列を用意して、この中にセルの状態値を書き込み、
描画の際に配列の中身を読んで、ウインドウ上に四角を描くようにしたいのです。
試しにボタンクリックごとに、乱数を配列に書き込んで描画することを繰り返しさせた
ところ四回目の書き込みから、指定外の色(白)で描画が起きてしまいます。
何が悪いのでしょうか?アドバイスよろしくお願いします。
' TODO: この位置にグローバルな変数、構造体、定数、関数を定義します。
dim hDC As DWord
const ScreenLeft = 10
const ScreenTop = 10
const ScreenWidthNum = 49
const ScreenHeightNum = 49
const cellSize = 10
dim cells[ScreenWidthNum, ScreenHeightNum] As Integer
'-----------------------------------------------------------------------------
' ウィンドウメッセージを処理するためのコールバック関数
Function MainWndProc(hWnd As HWND, dwMsg As DWord, wParam As WPARAM, lParam As LPARAM) As DWord
' TODO: この位置にウィンドウメッセージを処理するためのコードを記述します。
' イベントプロシージャの呼び出しを行います。
MainWndProc=EventCall_MainWnd(hWnd,dwMsg,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ここから下は、イベントプロシージャを記述するための領域になります。
Sub MainWnd_Destroy()
life_game_DestroyObjects()
PostQuitMessage(0)
End Sub
'初期化コード 配列とスレッド、デバイスコンテキストの取得など
Sub MainWnd_Create(ByRef CreateStruct As CREATESTRUCT)
dim i As Integer
dim j As Integer
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
cells[i, j] = 0
Next j
Next i
End Sub
Sub makeRandom()
dim i As Integer
dim j As Integer
Randomize
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
cells[i, j] = Rnd() * 2
Next j
Next i
End Sub
Sub MainWnd_CommandButtonStart_Click()
makeRandom()
InvalidateRect(hMainWnd, ByVal 0, 0)
End Sub
Sub MainWnd_Paint(hDC As HDC)
dim hBrush As DWord
dim cell As RECT
dim i As Integer
dim j As Integer
dim hWDC As HDC
hWDC =GetDC(hMainWnd)
For i = 0 to (ScreenWidthNum)
For j = 0 to (ScreenHeightNum)
If cells[i,j] = 1 Then
'生きてたら緑
hBrush = CreateSolidBrush(RGB(0, 255, 0))
Else
'死んでたら黒
hBrush = CreateSolidBrush(RGB(0, 0, 0))
End If
cell.left = i * cellSize + ScreenLeft
cell.top = j * cellSize + ScreenTop
cell.right = (i + 1) * cellSize + ScreenLeft
cell.bottom = (j + 1) * cellSize + ScreenTop
FillRect(hWDC, cell, hBrush)
Next j
Next i
ReleaseDC(hWDC, hMainWnd)
End Sub