近来发生了一些事情,已经好几天没学习,从今天开始要进入正轨了。
这里是VB实现一个小方块模拟俄罗斯方块,除了还不能变形
Option Explicit
Const MAX_X = 9
Const MAX_Y = 14
__________________________________________________________________
Sub new_block() '生成新的block
block.Top = 0
block.Left = 5 * block.Width
block.Visible = True
End Sub
__________________________________________________________________
Function can_move(fx As Integer) As Boolean '判断block是否能移动
Dim x, y As Integer
x = block.Left / block.Width
y = block.Top / block.Height
can_move = True
Select Case fx
Case 0 'left
x = x - 1
Case 1 'up
y = y - 1
Case 2 'right
x = x + 1
Case 3 'down
y = y + 1
End Select
If x < 0 Or y < 0 Or x > MAX_X Or y > MAX_Y Then
can_move = False
Exit Function
End If
Dim i As Integer
For i = 0 To deadblock.UBound
If deadblock(i).Visible = True And _
x = deadblock(i).Left / deadblock(i).Width And _
y = deadblock(i).Top / deadblock(i).Height Then
can_move = False
Exit Function
End If
Next
End Function
__________________________________________________________________
Sub block_move(fx As Integer) '判断block的移动方向
If Not can_move(fx) Then
Exit Sub
End If
Dim x, y As Integer
x = block.Left / block.Width
y = block.Top / block.Height
Select Case fx
Case 0 'left
x = x - 1
Case 1 'up
'y = y - 1
Case 2 'right
x = x + 1
Case 3 'down
y = y + 1
End Select
block.Move x * block.Width, y * block.Height
End Sub
__________________________________________________________________
Sub finish_down() 'block是否停止下落
Dim i As Integer
For i = 0 To deadblock.UBound
If deadblock(i).Visible = False Then
deadblock(i).Top = block.Top
deadblock(i).Left = block.Left
deadblock(i).Visible = True
block.Visible = False
Exit Sub
End If
Next
Load deadblock(i)
deadblock(i).Top = block.Top
deadblock(i).Left = block.Left
deadblock(i).Visible = True
block.Visible = False
End Sub
__________________________________________________________________
Function can_clear_line() As Boolean '判断是否消行
Dim i As Integer
Dim count As Integer
For i = 0 To deadblock.UBound
If deadblock(i).Visible = True And _
deadblock(i).Top = block.Top Then
count = count + 1
End If
Next
If count > MAX_X Then
can_clear_line = True
Else
can_clear_line = False
End If
End Function
__________________________________________________________________
Sub clear_line() '消行
Dim i As Integer
Dim count As Integer
For i = 0 To deadblock.UBound
If deadblock(i).Visible = True And _
deadblock(i).Top = block.Top Then
deadblock(i).Visible = False
End If
Next
For i = 0 To deadblock.UBound
If deadblock(i).Visible = True Then
deadblock(i).Top = deadblock(i).Top + deadblock(i).Height
End If
Next
End Sub
__________________________________________________________________
Private Sub back_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
__________________________________________________________________
Private Sub back_KeyDown(KeyCode As Integer, Shift As Integer)
block_move KeyCode - 37
End Sub
__________________________________________________________________
Private Sub Timer1_Timer()
If block.Visible = True Then
If can_move(3) = True Then
block_move 3
Else
finish_down
If can_clear_line = True Then
clear_line
End If
End If
Else
new_block
End If
End Sub