博主大二,最近刚完成vb.net的期末大作业,做的是一个简单的俄罗斯方块小游戏,运行效果如下:
(本来是画了网格线的,但是闪的厉害,双缓冲也没用,最后干脆就删了)
以下是源代码:
Public Class Form1
Dim curCube As FatherCube '当前方块
Dim nextCube As FatherCube '下一个方块
Dim time As Integer = 1000 '时间间隔
Dim map(11, 16) As Integer '网格
Dim g As Graphics 'panel1画布
Dim g2 As Graphics 'panel2画布
Dim myPen As New Pen(Color.Black, 0.5F)
Dim myBrush As New SolidBrush(Color.Yellow)
Dim s As New Size(30, 30)
Dim myScore As Integer = 0
Dim myClear As Integer = 0
Dim myLevel As Integer = 1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Left = 500
Me.Top = 50
Me.DoubleBuffered = True
Me.KeyPreview = True
Timer1.Enabled = True
Call Init()
End Sub
Private Sub Init()
Call RaiseCube()
Call RaiseNextCube()
g = Me.Panel1.CreateGraphics
g2 = Me.Panel2.CreateGraphics
TextBox1.Text = Str(myScore)
TextBox2.Text = Str(myClear)
TextBox3.Text = Str(myLevel)
End Sub '初始化
Private Sub RaiseCube()
Dim Temps() As FatherCube = {New Cube_I, New Cube_J, New Cube_L, New Cube_O, New Cube_S, New Cube_T, New Cube_Z}
Randomize()
Dim n As Integer = Int(Rnd() * 7)
curCube = Temps(n)
curCube.Reset()
curCube.Setmap(map)
End Sub '随机创建一个方块
Private Sub RaiseNextCube()
Dim Temps() As FatherCube = {New Cube_I, New Cube_J, New Cube_L, New Cube_O, New Cube_S, New Cube_T, New Cube_Z}
Randomize()
Dim n As Integer = Int(Rnd() * 7)
nextCube = Temps(n)
nextCube.Reset()
End Sub '创建下一个方块
Private Sub PaintCube()
For i = 0 To 3
Dim x As Integer = curCube.小方块(i).左坐标
Dim y As Integer = curCube.小方块(i).上坐标
Dim pt As New Point(x * 30, y * 30)
Dim rec As New Rectangle(pt, s)
g.FillRectangle(myBrush, rec)
Next
End Sub '将方块涂上颜色
Private Sub PaintNextCube()
For i = 0 To 3
Dim x As Integer = nextCube.小方块(i).左坐标 - 3
Dim y As Integer = nextCube.小方块(i).上坐标 + 1
Dim pt As New Point(x * 30, y * 30)
Dim rec As New Rectangle(pt, s)
g2.FillRectangle(myBrush, rec)
Next
End Sub '给下一个方块涂色
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
g.Clear(Color.Cyan)
g2.Clear(Color.Cyan)
Call PaintCube()
Call PaintNextCube()
'For i = 0 To 12
' g.DrawLine(myPen, i * 30, 0, i * 30, 18 * 30)
'Next
'For i = 0 To 16
' g.DrawLine(myPen, 0, i * 30, 12 * 30, i * 30)
'Next '画网格线,不过闪烁比较厉害
For i = 0 To 11
For j = 0 To 16
If map(i, j) = 1 Then
g.FillRectangle(myBrush, i * 30, j * 30, 30, 30)
End If
Next '重绘前面落下固定的方块
Next
End Sub '在此处实现所有涂色的代码
Private Sub ClearCube()
Dim curScore As Integer = 0 '本次得分
Dim clearRow As Integer = 0 '本次消行
Dim Occupy(16) As Integer
For i = 0 To 16
Occupy(i) = 0
Next
For j = 0 To 16
For i = 0 To 11
If map(i, j) = 1 Then
Occupy(j) = Occupy(j) + 1
End If
Next
Next '计算每行已有格子总数
For i = 16 To 0 Step -1
If Occupy(i) = 12 Then
clearRow = clearRow + 1
For j = i To 1 Step -1
For k = 0 To 11
map(k, j) = map(k, j - 1) '上面的放格下落
Next
Next
'i = i + 1 '再检查一遍当前行
End If '行满则消
Next
For i = 0 To 11
map(i, 0) = 0
Next
curScore = clearRow * clearRow * 10
myScore = myScore + curScore
myClear = myClear + clearRow
myLevel = Int(myScore / 1000) + 1
TextBox1.Text = Str(myScore)
TextBox2.Text = Str(myClear)
TextBox3.Text = Str(myLevel)
time = time * (11 - myLevel) / 10
End Sub '消除满行,更新数据
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Interval = time
If curCube.canMoveDown() Then
curCube.moveDown()
Call Form1_Paint(sender, New PaintEventArgs(g, New Rectangle))
Else
Dim flag As Boolean = 1
For i = 0 To 3
Dim n As Integer = curCube.小方块(i).上坐标
If n <= 0 Then
flag = 0
Exit For
End If
Next
If flag = True Then
For i = 0 To 3
Dim m As Integer = curCube.小方块(i).左坐标
Dim n As Integer = curCube.小方块(i).上坐标
map(m, n) = 1
Next
Call ClearCube()
Call Form1_Paint(sender, New PaintEventArgs(g, New Rectangle))
curCube = nextCube
curCube.Setmap(map)
Call RaiseNextCube()
Else
Call GameOver()
End If
End If
End Sub '游戏运行
Private Sub GameOver()
Timer1.Enabled = False
MsgBox("游戏结束!", 0, "提示")
For i = 0 To 11
For j = 0 To 16
map(i, j) = 0
Next
Next
curCube.Reset()
Label9.Text = "开始(S)"
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left And curCube.canMoveLeft Then
curCube.moveLeft()
ElseIf e.KeyCode = Keys.Right And curCube.canMoveRight Then
curCube.moveRight()
ElseIf e.KeyCode = Keys.Down And curCube.canMoveDown Then
curCube.moveDown()
ElseIf e.KeyCode = Keys.Up And curCube.canTurn Then
curCube.Turn()
ElseIf e.KeyCode = Keys.P And Timer1.Enabled = True Then
Timer1.Enabled = False
Label9.Text = "开始(S)"
ElseIf e.KeyCode = Keys.S And Timer1.Enabled = False Then
Timer1.Enabled = True
Label9.Text = "暂停(P)"
ElseIf e.KeyCode = Keys.R Then
myClear = 0
myScore = 0
myLevel = 1
For i = 0 To 11
For j = 0 To 16
map(i, j) = 0
Next
Next
curCube.Reset()
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub '键盘操纵方块移动
Private Sub Label9_MouseDown(sender As Object, e As MouseEventArgs) Handles Label9.MouseDown
Label9.BorderStyle = BorderStyle.Fixed3D
End Sub
Private Sub Label9_MouseUp(sender As Object, e As MouseEventArgs) Handles Label9.MouseUp
If Timer1.Enabled = True Then
Timer1.Enabled = False
Label9.BorderStyle = BorderStyle.FixedSingle
Label9.Text = "开始(S)"
Else
Timer1.Enabled = True
Label9.BorderStyle = BorderStyle.FixedSingle
Label9.Text = "暂停(P)"
End If
End Sub '暂停与开始按钮
Private Sub Label11_MouseDown(sender As Object, e As MouseEventArgs) Handles Label11.MouseDown
Label11.BorderStyle = BorderStyle.Fixed3D
End Sub
Private Sub Label11_MouseUp(sender As Object, e As MouseEventArgs) Handles Label11.MouseUp
Label11.BorderStyle = BorderStyle.FixedSingle
Me.Close()
End Sub '退出游戏按钮
Private Sub Label10_MouseDown(sender As Object, e As MouseEventArgs) Handles Label10.MouseDown
Label10.BorderStyle = BorderStyle.Fixed3D
End Sub
Private Sub Label10_MouseUp(sender As Object, e As MouseEventArgs) Handles Label10.MouseUp
myClear = 0
myScore = 0
myLevel = 1
TextBox1.Text = Str(myScore)
TextBox2.Text = Str(myClear)
TextBox3.Text = Str(myLevel)
For i = 0 To 11
For j = 0 To 16
map(i, j) = 0
Next
Next
curCube.Reset()
Label10.BorderStyle = BorderStyle.FixedSingle
End Sub '重玩按钮
End Class
Class Cube
Private x As Integer
Private y As Integer
Public Property 左坐标 As Integer
Get
Return x
End Get
Set(value As Integer)
x = value
End Set
End Property
Public Property 上坐标 As Integer
Get
Return y
End Get
Set(value As Integer)
y = value
End Set
End Property
End Class '小方格类
Class FatherCube
Private cube(4) As Cube
Private state As Integer
Private map As Integer(,)
Public Property 小方块() As Cube()
Get
Return cube
End Get
Set(value As Cube())
cube = value
End Set
End Property
Public Property 状态() As Integer
Get
Return state
End Get
Set(value As Integer)
state = value
End Set
End Property
Public Property 地图 As Integer(,)
Get
Return map
End Get
Set(value As Integer(,))
map = value
End Set
End Property
Public Sub New()
For i = 0 To 3
cube(i) = New Cube
Next
End Sub '构造函数
Public Sub Setmap(ByVal map As Integer(,))
Me.map = map
End Sub
Public Overridable Sub Reset()
End Sub
Public Overridable Sub Turn()
End Sub
Public Overridable Function canTurn() As Boolean
Return True
End Function
Public Function canMoveLeft() As Boolean
For i = 0 To 3
Dim m As Integer = Me.小方块(i).左坐标 - 1
Dim n As Integer = Me.小方块(i).上坐标
If (m < 0 Or m > 11 Or n < 0 Or n > 16) Then
Return False
ElseIf map(m, n) = 1 Then
Return False
End If
Next
Return True
End Function
Public Function canMoveRight() As Boolean
For i = 0 To 3
Dim m As Integer = Me.小方块(i).左坐标 + 1
Dim n As Integer = Me.小方块(i).上坐标
If (m < 0 Or m > 11 Or n < 0 Or n > 16) Then
Return False
ElseIf map(m, n) = 1 Then
Return False
End If
Next
Return True
End Function
Public Function canMoveDown() As Boolean
For i = 0 To 3
Dim m As Integer = Me.小方块(i).左坐标
Dim n As Integer = Me.小方块(i).上坐标 + 1
If (m < 0 Or m > 11 Or n < 0 Or n > 16) Then
Return False
ElseIf map(m, n) = 1 Then
Return False
End If
Next
Return True
End Function
Public Sub moveLeft()
For i = 0 To 3
Me.小方块(i).左坐标 = Me.小方块(i).左坐标 - 1
Next
End Sub
Public Sub moveRight()
For i = 0 To 3
Me.小方块(i).左坐标 = Me.小方块(i).左坐标 + 1
Next
End Sub
Public Sub moveDown()
For i = 0 To 3
Me.小方块(i).上坐标 = Me.小方块(i).上坐标 + 1
Next
End Sub
End Class '方块父类
Class Cube_I
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 2)
If Me.状态 = 0 Then
For i = 0 To 3
Me.小方块(i).左坐标 = 4 + i
Me.小方块(i).上坐标 = 0
Next
Else
For i = 0 To 3
Me.小方块(i).左坐标 = 5
Me.小方块(i).上坐标 = i
Next
End If
End Sub
Public Overrides Sub Turn()
If Me.状态() = 0 Then
Dim m As Integer = Me.小方块(0).左坐标 + 1
Dim n As Integer = Me.小方块(0).上坐标 - 1
For i = 0 To 3
Me.小方块(i).左坐标 = m
Me.小方块(i).上坐标 = n + i
Next
Me.状态() = 1
Else
Dim m As Integer = Me.小方块(0).左坐标 - 1
Dim n As Integer = Me.小方块(0).上坐标 + 1
For i = 0 To 3
Me.小方块(i).左坐标 = m + i
Me.小方块(i).上坐标 = n
Next
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(1).左坐标 - 1
Dim n As Integer = Me.小方块(1).上坐标 - 1
For i = m To m + 3
For j = n To n + 3
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'I型
Class Cube_O
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = 0
Me.小方块(0).左坐标 = 4
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 0
Me.小方块(2).左坐标 = 4
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 1
End Sub
Public Overrides Function canTurn() As Boolean
Return False
End Function
End Class 'O型
Class Cube_S
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 2)
If Me.状态 = 0 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 6
Me.小方块(1).上坐标 = 0
Me.小方块(2).左坐标 = 4
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 1
Else
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 0
Me.小方块(2).左坐标 = 6
Me.小方块(2).上坐标 = 2
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 1
End If
End Sub
Public Overrides Sub Turn()
If Me.状态() = 0 Then
Dim m As Integer = Me.小方块(0).左坐标
Dim n As Integer = Me.小方块(0).上坐标
Me.小方块(1).左坐标 = m
Me.小方块(1).上坐标 = n - 1
Me.小方块(2).左坐标 = m + 1
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n
Me.状态() = 1
Else
Dim m As Integer = Me.小方块(0).左坐标
Dim n As Integer = Me.小方块(0).上坐标
Me.小方块(1).左坐标 = m + 1
Me.小方块(1).上坐标 = n
Me.小方块(2).左坐标 = m - 1
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m
Me.小方块(3).上坐标 = n + 1
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(0).左坐标 - 1
Dim n As Integer = Me.小方块(0).上坐标 - 1
For i = m To m + 2
For j = n To n + 3
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'S型
Class Cube_Z
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 2)
If Me.状态 = 0 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 4
Me.小方块(1).上坐标 = 0
Me.小方块(2).左坐标 = 6
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 1
Else
Me.小方块(0).左坐标 = 6
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 6
Me.小方块(1).上坐标 = 0
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 2
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 1
End If
End Sub
Public Overrides Sub Turn()
If Me.状态() = 0 Then
Dim m As Integer = Me.小方块(0).左坐标
Dim n As Integer = Me.小方块(0).上坐标
Me.小方块(1).左坐标 = m
Me.小方块(1).上坐标 = n - 1
Me.小方块(2).左坐标 = m - 1
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n
Me.状态() = 1
Else
Dim m As Integer = Me.小方块(0).左坐标
Dim n As Integer = Me.小方块(0).上坐标
Me.小方块(1).左坐标 = m - 1
Me.小方块(1).上坐标 = n
Me.小方块(2).左坐标 = m + 1
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m
Me.小方块(3).上坐标 = n + 1
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(1).左坐标 - 1
Dim n As Integer = Me.小方块(1).上坐标 - 1
For i = m To m + 2
For j = n To n + 3
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'Z型
Class Cube_L
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 4)
If Me.状态 = 0 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 2
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 2
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = 4
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 6
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 0
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 2
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 0
Me.小方块(3).左坐标 = 4
Me.小方块(3).上坐标 = 0
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = 6
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 4
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 4
Me.小方块(3).上坐标 = 2
End If
End Sub
Public Overrides Sub Turn()
Dim m As Integer = Me.小方块(1).左坐标
Dim n As Integer = Me.小方块(1).上坐标
If Me.状态() = 0 Then
Me.小方块(0).左坐标 = m - 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m + 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n - 1
Me.状态() = 1
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n + 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n - 1
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n - 1
Me.状态() = 2
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = m + 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m - 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n + 1
Me.状态() = 3
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n - 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n + 1
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(1).左坐标 - 1
Dim n As Integer = Me.小方块(1).上坐标 - 1
For i = m To m + 2
For j = n To n + 2
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'L型
Class Cube_J
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 4)
If Me.状态 = 0 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 2
Me.小方块(3).左坐标 = 4
Me.小方块(3).上坐标 = 2
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = 4
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 6
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 2
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 2
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 0
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 0
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = 6
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 4
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 4
Me.小方块(3).上坐标 = 0
End If
End Sub
Public Overrides Sub Turn()
Dim m As Integer = Me.小方块(1).左坐标
Dim n As Integer = Me.小方块(1).上坐标
If Me.状态() = 0 Then
Me.小方块(0).左坐标 = m - 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m + 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n + 1
Me.状态() = 1
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n + 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n - 1
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n - 1
Me.状态() = 2
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = m + 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m - 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n - 1
Me.状态() = 3
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n - 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n + 1
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(1).左坐标 - 1
Dim n As Integer = Me.小方块(1).上坐标 - 1
For i = m To m + 2
For j = n To n + 2
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'J型
Class Cube_T
Inherits FatherCube
Public Overrides Sub Reset()
Me.状态() = Int(Rnd() * 4)
If Me.状态 = 0 Then
Me.小方块(0).左坐标 = 4
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 6
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 0
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 2
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 0
Me.小方块(3).左坐标 = 4
Me.小方块(3).上坐标 = 1
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = 6
Me.小方块(0).上坐标 = 1
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 4
Me.小方块(2).上坐标 = 1
Me.小方块(3).左坐标 = 5
Me.小方块(3).上坐标 = 2
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = 5
Me.小方块(0).上坐标 = 0
Me.小方块(1).左坐标 = 5
Me.小方块(1).上坐标 = 1
Me.小方块(2).左坐标 = 5
Me.小方块(2).上坐标 = 2
Me.小方块(3).左坐标 = 6
Me.小方块(3).上坐标 = 1
End If
End Sub
Public Overrides Sub Turn()
Dim m As Integer = Me.小方块(1).左坐标
Dim n As Integer = Me.小方块(1).上坐标
If Me.状态() = 0 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n + 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n - 1
Me.小方块(3).左坐标 = m - 1
Me.小方块(3).上坐标 = n
Me.状态() = 1
ElseIf Me.状态() = 1 Then
Me.小方块(0).左坐标 = m + 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m - 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m
Me.小方块(3).上坐标 = n + 1
Me.状态() = 2
ElseIf Me.状态() = 2 Then
Me.小方块(0).左坐标 = m
Me.小方块(0).上坐标 = n - 1
Me.小方块(2).左坐标 = m
Me.小方块(2).上坐标 = n + 1
Me.小方块(3).左坐标 = m + 1
Me.小方块(3).上坐标 = n
Me.状态() = 3
ElseIf Me.状态() = 3 Then
Me.小方块(0).左坐标 = m - 1
Me.小方块(0).上坐标 = n
Me.小方块(2).左坐标 = m + 1
Me.小方块(2).上坐标 = n
Me.小方块(3).左坐标 = m
Me.小方块(3).上坐标 = n - 1
Me.状态() = 0
End If
End Sub
Public Overrides Function canTurn() As Boolean
Dim m As Integer = Me.小方块(1).左坐标 - 1
Dim n As Integer = Me.小方块(1).上坐标 - 1
For i = m To m + 2
For j = n To n + 2
If (i < 0 Or i > 11 Or j < 0 Or j > 16) Then
Return False
ElseIf Me.地图(i, j) = 1 Then
Return False
End If
Next
Next
Return True
End Function
End Class 'T型