|
- 'VB 生命游戏
- Const Size = 100
- Const Dis = 80
- Dim A(0 To Size + 1, 0 To Size + 1) As Boolean
- Private Sub Form_Load()
- Dim I As Long, J As Long
- Picture1.Width = Size * Dis + 300
- Picture1.Height = Size * Dis + 300
- Picture1.DrawWidth = Dis / 20 + 1
- Randomize
- 'For I = 1 To Size
- 'For J = 1 To Size
- ' A(I, J) = (Rnd() < 0.4)
- 'Next J
- 'Next I
- For I = Size / 2 - 15 To Size / 2 + 15
- For J = Size / 2 - 15 To Size / 2 + 15
- A(I, J) = (Rnd() < 0.6)
- Next J
- Next I
- 'For I = Size / 2 - 35 To Size / 2 + 35
- ' A(I, Size / 2) = True
- 'Next I
- 'For I = 0 To Size + 1
- ' A(I, Size / 2) = True
- 'Next I
- End Sub
- Private Sub Timer1_Timer()
- Dim I As Long, J As Long, M As Long, N As Long, Num As Long
- Dim B(1 To Size, 1 To Size) As Boolean
- DoEvents
- For I = 1 To Size
- For J = 1 To Size
- If A(I, J) Then Num = -1 Else Num = 0
- For M = I - 1 To I + 1
- For N = J - 1 To J + 1
- If A(M, N) Then Num = Num + 1
- Next N
- Next M
- Select Case Num
- Case 2
- B(I, J) = A(I, J)
- Case 3
- B(I, J) = True
- Case Else
- B(I, J) = False
- End Select
- Next J
- Next I
- For I = 1 To Size
- For J = 1 To Size
- If B(I, J) Then Picture1.PSet (Dis * (I + 1), Dis * (J + 1)), vbBlue Else Picture1.PSet (Dis * (I + 1), Dis * (J + 1)), vbWhite
- A(I, J) = B(I, J)
- Next J
- Next I
-
- For I = 1 To Size
- A(0, I) = B(Size, I)
- A(Size + 1, I) = B(1, I)
- A(I, 0) = B(I, Size)
- A(I, Size + 1) = B(I, 1)
- Next I
- End Sub
复制代码 |
|