- Const WidthCount As Integer = 10
- Const HeightCount As Integer = 20
- '"ttttffffffffffff"
- '"ttffttffffffffff"
- '"tttfftffffffffff"
- '"ttfffttfffffffff"
- '"tttftftfffffffff"
- 'Dim BlockS(1 To 10) As String
- Const BlockSCount As Integer = 8
- Dim Block(1 To 10, 1 To 4, 1 To 4, 1 To 4) As Boolean
- Dim P As PictureBox
- Dim B() As Integer
- Dim PosX As Integer
- Dim PosY As Integer
- Dim CurrentBlock As Integer
- Dim CurrentTurn As Integer
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
- If KeyCode = vbKeyReturn Then
- ReStart
- Exit Sub
- End If
- If Timer1.Enabled Then
- If KeyCode = vbKeyLeft And PosX > 0 Then
- If Not CheckSet(PosX - 1, PosY) Then
- PosX = PosX - 1
- End If
- ElseIf KeyCode = vbKeyRight And PosX < WidthCount - GetBlockWidth(CurrentBlock) Then
- If Not CheckSet(PosX + 1, PosY) Then
- PosX = PosX + 1
- End If
- ElseIf KeyCode = vbKeyUp Then
- TurnBlock
- ElseIf KeyCode = vbKeyDown Then
- ' Do
- ' If CheckSet(PosX, PosY + 1) Then
- ' SetBlock
- ' CheckDecrease
- ' NextBlock
- ' Exit Do
- ' Else
- ' PosY = PosY + 1
- ' Draw
- ' End If
- ' Loop
- Timer1_Timer
- Timer1.Interval = 20
- End If
- Draw
- End If
- End Sub
- Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
- Timer1.Interval = 200
- End Sub
- Private Sub Form_Load()
- Set P = Picture1
- P.AutoRedraw = True
- Inti
- Me.Show
- Draw
- End Sub
- Private Sub TurnBlock()
- CurrentTurn = CurrentTurn + 1
- If CurrentTurn = 5 Then CurrentTurn = 1
- If PosX + GetBlockWidth(CurrentBlock) > WidthCount Then
- CurrentTurn = CurrentTurn - 1
- If CurrentTurn = 0 Then CurrentTurn = 4
- ElseIf CheckSet(PosX, PosY) Then
- CurrentTurn = CurrentTurn - 1
- If CurrentTurn = 0 Then CurrentTurn = 4
- End If
- End Sub
- Private Sub CheckDecrease()
- Dim I As Integer, J As Integer, M As Integer
- For J = 1 To HeightCount
- For I = 1 To WidthCount
- If B(I, J) = 0 Then GoTo Lab1
- Next
- For M = J To 2 Step -1
- For I = 1 To WidthCount
- B(I, M) = B(I, M - 1)
- Next
- Next
- For I = 1 To WidthCount
- B(I, 1) = 0
- Next
- Lab1:
- Next
- End Sub
- Private Function GetBlockWidth(BlockID As Integer) As Integer
- Dim I As Integer, J As Integer
- Dim Temp As Integer
- Temp = 0
- For J = 1 To 4
- For I = 1 To 4
- If Block(BlockID, I, J, CurrentTurn) Then
- If I > Temp Then Temp = I
- End If
- Next
- Next
- GetBlockWidth = Temp
- End Function
- Private Function CheckSet(pX As Integer, pY As Integer) As Boolean
- Dim I As Integer, J As Integer
- For J = 1 To 4
- For I = 1 To 4
- If Block(CurrentBlock, I, J, CurrentTurn) Then
- If pY + J > HeightCount Then
- CheckSet = True
- Exit Function
- ElseIf B(pX + I, pY + J) <> 0 Then
- CheckSet = True
- Exit Function
- End If
- End If
- Next
- Next
- CheckSet = False
- End Function
- Private Sub SetBlock()
- On Error Resume Next
- Dim I As Integer, J As Integer
- For J = 1 To 4
- For I = 1 To 4
- If Block(CurrentBlock, I, J, CurrentTurn) Then
- B(PosX + I, PosY + J) = 2
- End If
- Next
- Next
- End Sub
- Private Sub NextBlock()
- Randomize
- CurrentBlock = Int(Rnd() * BlockSCount + 1)
- CurrentTurn = Int(Rnd() * 4 + 1)
- PosX = Int((WidthCount - GetBlockWidth(CurrentBlock)) / 2)
- PosY = 0
- If CheckSet(PosX, PosY) Then
- 'Game Over
- Timer1.Enabled = False
- Draw
- Else
- Draw
- End If
- End Sub
- Private Sub Inti()
- Dim BlockS()
- ReDim BlockS(1 To BlockSCount)
- BlockS(1) = "tttt............t...t...t...t...tttt............t...t...t...t..."
- BlockS(2) = "tt...tt..........t..tt..t.......tt...tt..........t..tt..t......."
- BlockS(3) = "ttt..t..........t...tt..t........t..ttt..........t..tt...t......"
- BlockS(4) = "tt..tt..........tt..tt..........tt..tt..........tt..tt.........."
- BlockS(5) = "t...tt...t.......tt.tt..........t...tt...t.......tt.tt.........."
- BlockS(6) = "ttt.t.t.........tt..t...tt......t.t.ttt.........tt...t..tt......"
- BlockS(7) = "t...ttt...t......tt..t..tt......t...ttt...t......tt..t..tt......"
- BlockS(8) = "t.t.....t.t.....t.t..t..t.t..........t..t.t......t..ttt..t......"
- ReDim B(1 To WidthCount, 1 To HeightCount)
- P.ScaleWidth = WidthCount
- P.ScaleHeight = HeightCount
- Dim I As Integer, J As Integer, H As Integer, G As Integer
- For G = 1 To BlockSCount
- For H = 1 To 4
- For I = 1 To 4
- For J = 1 To 4
- Debug.Print (H - 1) * 16 + (J - 1) * 4 + I
- If Mid(BlockS(G), (H - 1) * 16 + (J - 1) * 4 + I, 1) = "t" Then
- Block(G, I, J, H) = True
- Else
- Block(G, I, J, H) = False
- End If
- Next
- Next
- Next
- Next
- ReStart
- End Sub
- Private Sub ReStart()
- Dim I As Integer, J As Integer
- For J = 1 To HeightCount
- For I = 1 To WidthCount
- B(I, J) = 0
- Next
- Next
- Draw
- Timer1.Enabled = True
- NextBlock
- End Sub
- Private Sub Draw()
- On Error Resume Next
- Dim I As Integer, J As Integer
- For J = 1 To HeightCount
- For I = 1 To WidthCount
- Select Case B(I, J)
- Case 0:
- P.Line (I - 1, J - 1)-(I, J), vbBlack, BF
- Case 1:
- P.Line (I - 1, J - 1)-(I, J), vbGreen, BF
- Case 2:
- P.Line (I - 1, J - 1)-(I, J), vbRed, BF
- End Select
- Next
- Next
- For J = 1 To 4
- For I = 1 To 4
- If Block(CurrentBlock, I, J, CurrentTurn) Then
- P.Line (PosX + I - 1, PosY + J - 1)-(PosX + I, PosY + J), vbGreen, BF
- End If
- Next
- Next
- End Sub
- Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
- Form_KeyDown KeyCode, Shift
- End Sub
- Private Sub Picture1_KeyUp(KeyCode As Integer, Shift As Integer)
- Form_KeyUp KeyCode, Shift
- End Sub
- Private Sub Timer1_Timer()
- If CheckSet(PosX, PosY + 1) Then
- SetBlock
- CheckDecrease
- NextBlock
- Else
- PosY = PosY + 1
- Draw
- End If
- End Sub
欢迎访问infoheader的Blog——年华似水浪淘沙 E-mail:infoheader@gmail.com来自fanfou:
GTalk:infoheader@gmail.com
2009年3月31日星期二
VB俄罗斯方块
订阅:
博文评论 (Atom)
2 条评论:
说明:
1、使用了ChiChou的代码染色。
2、不知道数据存储怎样比较好。
3、个别地方没有优化,比如那个GetBlockWidth()是不该经常调用的。
似乎那个HighLight看起来有点问题……
犀牛可以试试notepad2
ps:不知道Blogger是否对HTML解析有问题,我在FX下,HL的代码中间都多了一行
IE没问题~
发表评论