贪吃蛇游戏编程代码_用VB编程制作的贪吃蛇游戏!

本文分享了一篇使用VB编程语言实现的经典贪吃蛇游戏代码。游戏包含基本的移动、转弯、碰撞检测和分数计算等功能,同时提供了暂停、调整速度等操作。适合初学者学习和理解VB编程。
摘要由CSDN通过智能技术生成

贪吃蛇游戏

265b17f59f37048ef4e764fd75ddc36e.png

贪吃蛇是一款经典的休闲游戏,同时也是一款经典的益智游戏,有PC和手机等多平台版本。既简单又耐玩。

该游戏通过控制蛇头方向吃蛋,从而使得蛇变得越来越长。

游戏运行图

ce23b64a1c676806839b7b5ebd50c0e9.png

贪吃蛇游戏程序代码

Option Explicit

Option Base 1

Dim intEat As Integer

Dim intNum As Integer       '节数

Dim intDirect() As Integer  ' 每一节的运动方向

Const GRID As Integer = 20

Const GRID_NUM  As Integer = 20

Dim AppleX(5) As Integer, AppleY(5) As Integer

Dim time_past As Integer

Public restart As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode

        Case 37    'left

            If intDirect(1) <> 0 Then intDirect(1) = enmLeft

        Case 38     'up

            If intDirect(1) <> 1 Then intDirect(1) = enmUp

        Case 39     'right

            If intDirect(1) <> 2 Then intDirect(1) = enmRight

        Case 40     'down

            If intDirect(1) <> 3 Then intDirect(1) = enmDown

        Case 13     '回车可以暂停

            Timer1.Enabled = Not Timer1.Enabled

            If Not Timer1.Enabled Then

                Me.Caption = "贪吃蛇(暂停)"

                Timer2.Enabled = False

            Else

                Me.Caption = "贪吃蛇(运行)"

                Timer2.Enabled = True

            End If

        Case 33

                Timer1.Interval = Timer1.Interval - 20

        Case 34

                Timer1.Interval = Timer1.Interval + 20

        End Select

        Call DrawEye            '显示眼睛转弯

    'Print Height, Width

End Sub

Private Sub Form_Load()

    Dim i As Integer

    Call OpenMaze

    Call OpenRecord

    Pic.BackColor = lngBackColor

    '调整大小与位置

    Pic.Left = 20

    Pic.Top = 20

    Pic.Width = GRID * GRID_NUM + 6

    Pic.Height = GRID * GRID_NUM + 6

    Me.Width = (Pic.Left + Pic.Width + 20) * (Screen.TwipsPerPixelX)

    Me.Height = (Pic.Top + Pic.Height + 20 + 40) * (Screen.TwipsPerPixelY)

    '绘制格线

    linHor(1).X1 = 0

    linHor(1).X2 = GRID * GRID_NUM

    linHor(1).Y1 = 0

    linHor(1).Y2 = 0

    linVer(1).X1 = 0

    linVer(1).X2 = 0

    linVer(1).Y1 = 0

    linVer(1).Y2 = GRID * GRID_NUM

    For i = 2 To 21

        Load linHor(i)

        linHor(i).Y1 = (i - 1) * GRID

        linHor(i).Y2 = (i - 1) * GRID

        Load linVer(i)

        linVer(i).X1 = (i - 1) * GRID

        linVer(i).X2 = (i - 1) * GRID

        linHor(i).Visible = True

        linVer(i).Visible = True

    Next

    Call DrawMaze

    Call DrawSnake

    Call ShowNumberAll

End Sub

Private Sub mnuAbout_Click()

       MsgBox "贪吃蛇 Ver2.0" & Chr(13) & "CopyRight By ABC." & Chr(13) & "2003-07", 64, "版本说明"

End Sub

Private Sub mnuExit_Click()

    Unload Me

End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值