资源说明:Vb写的俄罗斯方块 基本功能全'声明一个整数常量Width,表示游戏界面横向的小正方形数目,初始化为16
Public Const Width As Integer = 16
'声明一个整数常量Height,表示游戏界面横向的小正方形数目,初始化为30
Public Const Height As Integer = 30
'游戏界面的背景色
Public Shared BackColor As Color
'小正方形的大小
Public Shared SquareSize As Size = New Size(10, 10)
Public Shared g As Graphics
'声明一个Graphics变量,用于绘制下一个方块
Public Shared gNext As Graphics
'声明一个数组,用于表示游戏界面上的所有小正方形
Private Shared ArrGameField(Width - 1, Height - 1) As CSquare
'函数功能:判断ArrGameField(x, y)是否有正方形,有则返回False,无则返回True
Public Shared Function IsEmpty(ByVal x As Integer, ByVal y As Integer) As Boolean
If x >= 0 And x < Width And y >= 0 And y < Height Then
Return ArrGameField(x, y) Is Nothing
End If
End Function
'函数功能:检查游戏区域中,有哪些行是满的,返回满足条件的行数
Public Shared Function CheckLines() As Integer
'定义整数变量result表示满行的数目
Dim result As Integer
'定义整数变量Y,表示第Y行,初始化为Height-1
Dim Y As Integer = Height - 1
'编写循环语句,循环条件Y>=0
'在循环体中,先判断第y行是否是满的,是的话result增1,并把第y行上的所有小正方形下降一行.最后y递减1
'如果实现把第y行的所有小正方形下降一行:从第y行扫描至第0行,每扫描一行,把上一行的每个小正方形赋值给该行,并修改每一个小正方形的Location属性的值
While Y >= 0
If IsLineFull(Y) Then
result += 1
For i As Integer = Y To 0 Step -1
If i > 0 Then
For x As Integer = 0 To Width - 1
ArrGameField(x, i) = ArrGameField(x, i - 1)
If Not ArrGameField(x, i) Is Nothing Then
ArrGameField(x, i).Location = New Point(ArrGameField(x, i).Location.X, ArrGameField(x, i).Location.Y + SquareSize.Height)
End If
Next
Else
For x As Integer = 0 To Width - 1
ArrGameField(x, i) = Nothing
Next
End If
Next
Else
Y = Y - 1
End If
End While
Return result
End Function
'函数功能:判断第y行是否是满的,是则返回True,否则返回Flase
Private Shared Function IsLineFull(ByVal y As Integer) As Boolean
'定义变量i,通过循环语句让i从0逐步增长到Width-1
'在循环体中,检查ArrGameField(i, y)是否是空值,如果有一个是空值,说明这行还没满,返回False
'如果没有一个是空值,则说明是满的,返回True
For i As Integer = 0 To Width - 1
If ArrGameField(i, y) Is Nothing Then
Return False
End If
Next
Return True
End Function
'函数功能:当方块下降时,如果目标位置非空,则要停止下降.通过该函数可以在数组ArrGameField记录4个小正方形在位置
Public Shared Function StopSquare(ByVal square As CSquare, ByVal x As Integer, ByVal y As Integer)
ArrGameField(x, y) = square
End Function
'函数功能:重新绘制整个游戏中的所有小正方形
Public Shared Sub Redraw()
'通过嵌套循环,检查ArrGameField(x,y)是否为空,非空则需要重新显示
Dim y As Integer
For y = Height - 1 To 0 Step -1
For x As Integer = Width - 1 To 0 Step -1
If Not ArrGameField(x, y) Is Nothing Then
ArrGameField(x, y).Show(g)
End If
Next
Next
End Sub
'函数功能: 清除游戏画面
Public Shared Sub Reset()
'通过嵌套循环,设置数组ArrGameField(x,y)的每个元素都是空的
Dim y As Integer
For y = Height - 1 To 0 Step -1
For x As Integer = Width - 1 To 0 Step -1
If Not ArrGameField(x, y) Is Nothing Then
ArrGameField(x, y) = Nothing
End If
Next
Next
End Sub
End Class
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。