俄罗斯方块的VB实现,部分代码!

本文档展示了使用Visual Basic(VB)实现俄罗斯方块的部分代码,包括方块移动、下落、计时等功能。通过Form_KeyPress事件处理键盘输入,Timer_speed定时器控制方块下落,show系列子程序用于显示不同形状的方块。代码详细解释了如何在窗体上定位和操作图像块,实现游戏基本逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Option Explicit
Dim i As Integer
Dim time_hour, time_minute, time_second As Integer

Private Sub Form_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 105
            Call turn(next_block_kind)
        Case 106
            Call move_left(next_block_kind)
        Case 108
            Call move_right(next_block_kind)
        Case 107
            Call down_block(next_block_kind)
    End Select
End Sub

Public Sub Form_Load()
    Timer_speed.Enabled = False
    Call image_position '排列方块
    Call face '设置窗体属性
    bx = 3
    by = 0
    Label_hour.Caption = 0
    Label_minute.Caption = 0
    'Call next_block
    next_block_kind = 7
    Timer_speed.Enabled = True
    Me.KeyPreview = True
End Sub

Private Sub menu_about_Click()
    Load about
    about.Visible = True
End Sub

Private Sub timer_now_Timer() '计时
    time_second = time_second + 1
    If time_second = 60 Then
        time_minute = time_minute + 1
        time_second = 0
        Label_minute.Caption = time_minute
    End If
    If time_minute = 60 Then
        time_hour = time_hour + 1
        time_minute = 0
        Label_hour.Caption = time_hour
    End If
    Label_second.Caption = time_second
End Sub
Private Sub Timer_speed_Timer()
    Call down_block(next_block_kind)
End Sub
Option Explicit
Public bx, by As Integer
Public next_block_kind As Integer
Public p As Integer
Public q As Integer
Public a As Integer
Public n As Integer
Public m As Integer

'Public turn_kind As Integer

Sub show1(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X).Visible = True
        Form_main.Image_block(Y * 10 + X + 1).Visible = True
        Form_main.Image_block(Y * 10 + X + 10).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show2(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X).Visible = True
        Form_main.Image_block(Y * 10 + X + 1).Visible = True
        Form_main.Image_block(Y * 10 + X + 12).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show3(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X + 2).Visible = True
        Form_main.Image_block(Y * 10 + X + 1).Visible = True
        Form_main.Image_block(Y * 10 + X + 10).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show4(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X + 1).Visible = True
        Form_main.Image_block(Y * 10 + X + 12).Visible = True
        Form_main.Image_block(Y * 10 + X + 10).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show5(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X).Visible = True
        Form_main.Image_block(Y * 10 + X + 12).Visible = True
        Form_main.Image_block(Y * 10 + X + 10).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show6(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X + 2).Visible = True
        Form_main.Image_block(Y * 10 + X + 12).Visible = True
        Form_main.Image_block(Y * 10 + X + 10).Visible = True
        Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show7(X, Y As Integer)
        Form_main.Image_block(Y * 10 + X).Visible = True
        Form_main.Image_block(Y * 10 + X + 1).Visible = True
        Form_main.Image_block(Y * 10 + X + 2).Visible = True
        Form_main.Image_block(Y * 10 + X + 3).Visible = True
End Sub
Sub show8(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
End Sub
Sub show9(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 21).Visible = True
End Sub
Sub show10(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 21).Visible = True
End Sub
Sub show11(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
End Sub
Sub show12(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 2).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
End Sub
Sub show13(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
End Sub
Sub show14(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 2).Visible = True
    Form_main.Image_block(Y * 10 + X + 12).Visible = True
End Sub
Sub show15(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 21).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
End Sub
Sub show16(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 11).Visible = True
    Form_main.Image_block(Y * 10 + X + 21).Visible = True
End Sub
Sub show17(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 1).Visible = True
    Form_main.Image_block(Y * 10 + X + 2).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
End Sub
Sub show18(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
    Form_main.Image_block(Y * 10 + X + 21).Visible = True
End Sub
Sub show19(X, Y As Integer)
    Form_main.Image_block(Y * 10 + X).Visible = True
    Form_main.Image_block(Y * 10 + X + 10).Visible = True
    Form_main.Image_block(Y * 10 + X + 20).Visible = True
    Form_main.Image_block(Y * 10 + X + 30).Visible = True
End Sub
Sub next_block() '产生下一个方块(random)
        Randomize
        next_block_kind = Int((19 * Rnd) + 1)
        Call is_full(next_block_kind)
End Sub
Sub down_block(block_k As Integer)  '方块下落,慢
    Call fall_buttom(block_k)
    Select Case block_k
        Case 1
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            by = by + 1
            Call show1(bx, by)
        Case 2
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 12).Visible = False
            by = by + 1
            Call show2(bx, by)
        Case 3
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            Form_main.Image_block(by * 10 + bx + 10).Visible = False
            by = by + 1
            Call show3(bx, by)
        Case 4
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 10).Visible = False
            Form_main.Image_block(by * 10 + bx + 12).Visible = False
            by = by + 1
            Call show4(bx, by)
        Case 5
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 11).Visible = False
            Form_main.Image_block(by * 10 + bx + 12).Visible = False
            by = by + 1
            Call show5(bx, by)
        Case 6
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            Form_main.Image_block(by * 10 + bx + 10).Visible = False
            Form_main.Image_block(by * 10 + bx + 11).Visible = False
            by = by + 1
            Call show6(bx, by)
        Case 7
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            Form_main.Image_block(by * 10 + bx + 3).Visible = False
            by = by + 1
            Call show7(bx, by)
        Case 8
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 10).Visible = False
            by = by + 1
            Call show8(bx, by)
        Case 9
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 11).Visible = False
            by = by + 1
            Call show9(bx, by)
        Case 10
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 10).Visible = False
            by = by + 1
            Call show10(bx, by)
        Case 11
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 11).Visible = False
            by = by + 1
            Call show11(bx, by)
        Case 12
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            by = by + 1
            Call show12(bx, by)
        Case 13
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            by = by + 1
            Call show13(bx, by)
        Case 14
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            by = by + 1
            Call show14(bx, by)
        Case 15
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 20).Visible = False
            by = by + 1
            Call show15(bx, by)
        Case 16
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            by = by + 1
            Call show16(bx, by)
        Case 17
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 1).Visible = False
            Form_main.Image_block(by * 10 + bx + 2).Visible = False
            by = by + 1
            Call show17(bx, by)
        Case 18
            Form_main.Image_block(by * 10 + bx).Visible = False
            Form_main.Image_block(by * 10 + bx + 21).Visible = False
            by = by + 1
            Call show18(bx, by)
        Case 19
            Form_main.Image_block(by * 10 + bx).Visible = False
            by = by + 1
            Call show19(bx, by)
    End Select
End Sub
Sub clear(b_kinds As Integer, now_y As Integer) '满行后清行
   ' Dim i As Integer
    'Dim q As Integer
    Select Case b_kinds
        Case 1, 2, 3, 4, 5, 6, 12, 14, 17
            p = 1
        Case 7
            p = 0
        Case 8, 9, 10, 11, 13, 16, 15, 18
            p = 2
        Case 19
            p = 3
    End Select
    For q = 0 To p
        If canClear(now_y + q) Then
            Call clearOneLine(now_y + q)
        End If
    Next
End Sub
Function canClear(l As Integer) As Boolean
    'Dim a As Integer
    For a = (l * 10 + 0) To (l * 10 + 9)
        If Form_main.Image_block(a).Visible = False Then
            canClear = False
            Exit Function
        End If
    Next
    canClear = True
End Function
Sub clearOneLine(lineNum As Integer)
    'Dim n As Integer
    'Dim m As Integer
    For n = lineNum To 1 Step -1
        For m = 0 To 9
            Form_main.Image_block(n * 10 + m).Visible = Form_main.Image_block((n - 1) * 10 + m).Visible
        Next
    Next
End Sub

Sub is_full(block_end As Integer)  '是否游戏结束
    Select Case block_end
        Case 1 '田字型
            If Form_main.Image_block(by * 10 + 3).Visible = True Or Form_main.Image_block(by * 10 + 4).Visible = True Or Form_main.Image_block(by * 10 + 13).Visible = True Or Form_main.Image_block(by * 10 + 14).Visible = True Then
                Form_main.Timer_speed.Enabled = False
                Form_main.timer_now.Enabled = False
                MsgBox "游戏结束!^_^"
            Else
                Form_main.Timer_speed = True
            End If
        Case 2 'Z字型
            If Form_main.Image_block(by * 10 + 3).Visible = True Or Form_main.Image_block(by * 10 + 4).Visible = True Or Form_main.Image_block(by * 10 + 14).Visible = True Or Form_main.Image_block(by * 10 + 15).Visible = True Then
                Form_main.Timer_speed.Enabled = False
                Form_main.timer_now.Enabled = False
                MsgBox "游戏结束!^_^"
                            Else
                Form_main.Timer_speed = True
            End If
        Case 3 '反Z字型
            If Form_main.Image_block(by * 10 + 3).Visible = True Or Form_main.Image_block(by * 10 + 4).Visible = True Or Form_main.Image_block(by * 10 + 13).Visible = True Or Form_main.Image_block(by * 10 + 14).Visible = True Then
                Form_main.Timer_speed.Enabled = False
                Form_main.timer_now.Enabled = False
                MsgBox "游戏结束!^_^"
                            Else
                Form_main.Timer_speed = True
            End If
        Case 4 '土字型

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值