贪吃蛇游戏
贪吃蛇是一款经典的休闲游戏,同时也是一款经典的益智游戏,有PC和手机等多平台版本。既简单又耐玩。
该游戏通过控制蛇头方向吃蛋,从而使得蛇变得越来越长。
游戏运行图贪吃蛇游戏程序代码
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