⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 贪吃蛇
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BackColor       =   &H00C0E0FF&
   Caption         =   "贪吃蛇"
   ClientHeight    =   7545
   ClientLeft      =   2970
   ClientTop       =   2670
   ClientWidth     =   10905
   LinkTopic       =   "Form1"
   ScaleHeight     =   7545
   ScaleWidth      =   10905
   Begin VB.CommandButton Command1 
      Caption         =   "开始"
      Height          =   315
      Left            =   315
      TabIndex        =   1
      Top             =   6930
      Width           =   1215
   End
   Begin VB.Timer Timer1 
      Interval        =   200
      Left            =   10080
      Top             =   0
   End
   Begin VB.PictureBox Picture1 
      Height          =   840
      Left            =   9970
      Picture         =   "Form1.frx":0000
      ScaleHeight     =   780
      ScaleWidth      =   780
      TabIndex        =   0
      TabStop         =   0   'False
      Top             =   6615
      Width           =   840
   End
   Begin VB.Label Label4 
      Alignment       =   2  'Center
      BackColor       =   &H00C0E0FF&
      Caption         =   "1"
      Height          =   330
      Left            =   7560
      TabIndex        =   5
      Top             =   6930
      Width           =   960
   End
   Begin VB.Label Label3 
      Alignment       =   1  'Right Justify
      BackColor       =   &H00C0E0FF&
      Caption         =   "关数:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   5670
      TabIndex        =   4
      Top             =   6930
      Width           =   960
   End
   Begin VB.Label Label2 
      BackColor       =   &H00C0E0FF&
      Caption         =   "0"
      Height          =   330
      Left            =   4095
      TabIndex        =   3
      Top             =   6930
      Width           =   960
   End
   Begin VB.Label Label1 
      Alignment       =   1  'Right Justify
      BackColor       =   &H00C0E0FF&
      Caption         =   "分数:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   2520
      TabIndex        =   2
      Top             =   6930
      Width           =   1275
   End
   Begin VB.Line Line1 
      X1              =   9765
      X2              =   9765
      Y1              =   0
      Y2              =   6615
   End
   Begin VB.Shape food 
      BackStyle       =   1  'Opaque
      BorderColor     =   &H00000000&
      FillStyle       =   6  'Cross
      Height          =   315
      Left            =   1260
      Shape           =   2  'Oval
      Top             =   630
      Visible         =   0   'False
      Width           =   315
   End
   Begin VB.Line Line2 
      X1              =   9756
      X2              =   0
      Y1              =   6615
      Y2              =   6615
   End
   Begin VB.Shape snake 
      BackColor       =   &H00FFFF00&
      BackStyle       =   1  'Opaque
      BorderColor     =   &H00000000&
      FillColor       =   &H00FFFF00&
      Height          =   315
      Index           =   0
      Left            =   630
      Shape           =   1  'Square
      Top             =   630
      Visible         =   0   'False
      Width           =   315
   End
   Begin VB.Menu yx 
      Caption         =   "游戏"
      Begin VB.Menu dj 
         Caption         =   "等级"
         Begin VB.Menu jd 
            Caption         =   "简单"
            Shortcut        =   ^A
         End
         Begin VB.Menu kn 
            Caption         =   "困难"
            Shortcut        =   ^S
         End
      End
   End
   Begin VB.Menu xx 
      Caption         =   "选项"
      Begin VB.Menu aj 
         Caption         =   "按键设定"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim X(31) As Integer   '声明界面X坐标
Dim Y(22) As Integer   '声明界面Y坐标
Dim slong As Integer
Dim fx As Integer
Dim head As Integer
Dim eat As Boolean
Dim base As Integer
Dim guanshu As Integer
Dim prefen As Integer '记录上一关的分值

Private Sub aj_Click()
Form2.Show
End Sub

Private Sub Command1_Click()
If Command1.Caption = "开始" Then
fx = 2
Timer1.Enabled = True
Command1.Caption = "暂停"
Picture1.Enabled = True
Picture1.SetFocus
Else
Command1.Caption = "开始"
Timer1.Enabled = False
Picture1.Enabled = False
End If
End Sub

Private Sub Form_Load()

Dim i, j As Integer
j = 0
For i = 0 To 315 * 31 Step 315
X(j) = i
j = j + 1
Next i
j = 0
For i = 0 To 315 * 22 Step 315
Y(j) = i
j = j + 1
Next i
Fx_up = 38
Fx_down = 40
Fx_left = 37
Fx_right = 39
head = 1
slong = 1
Timer1.Enabled = False
eat = True
base = 10
End Sub

Private Sub jd_Click()
Timer1.Interval = 200
End Sub

Private Sub kn_Click()
Timer1.Interval = 60
End Sub

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = Fx_up And fx <> 2 Then
fx = 1
ElseIf KeyCode = Fx_down And fx <> 1 Then
fx = 2
ElseIf KeyCode = Fx_left And fx <> 4 Then
fx = 3
ElseIf KeyCode = Fx_right And fx <> 3 Then
fx = 4
End If
End Sub

Private Sub Timer1_Timer()
Load snake(head) '加载新的头部
snake(head).Width = 315
snake(head).Height = 315
snake(head).Visible = True
If fx = 1 And fx <> 2 Then
snake(head).Top = snake(head - 1).Top - 315
snake(head).Left = snake(head - 1).Left
ElseIf fx = 2 And fx <> 1 Then
snake(head).Top = snake(head - 1).Top + 315
snake(head).Left = snake(head - 1).Left
ElseIf fx = 3 And fx <> 4 Then
snake(head).Left = snake(head - 1).Left - 315
snake(head).Top = snake(head - 1).Top
ElseIf fx = 4 And fx <> 3 Then
snake(head).Left = snake(head - 1).Left + 315
snake(head).Top = snake(head - 1).Top
End If
'随机分布食物
Dim i, foodx, foody As Integer
1 Randomize
 
 If eat = True Then
 foodx = Int(Rnd * 30 + 0.5)
 foody = Int(Rnd * 21 + 0.5)
 For i = head To head - slong + 1 Step -1
 If X(foodx) = snake(i).Left And Y(foody) = snake(i).Top Or X(foodx) < 0 Or Y(foody) < 0 Or X(foodx) = Line1.X1 Or Y(foody) = Line2.Y1 Then
 GoTo 1
 End If
 Next i
 food.Visible = True
 food.Left = X(foodx)
 food.Top = Y(foody)
 eat = False
 End If
'卸载蛇尾部分
If head > slong Then
'进食
If snake(head).Left = food.Left And snake(head).Top = food.Top Then
SUCCESS = sndPlaySound(App.Path + "\sound\eat.wav", &H1)
food.Visible = False
slong = slong + 1
eat = True
Else
Unload snake(head - slong)
End If
End If
'撞墙
If snake(head).Left < 0 Or snake(head).Top < 0 Or snake(head).Left = Line1.X1 Or snake(head).Top = Line2.Y1 Then
MsgBox "装进墙里去了" & Chr(13) & "您的分数:" & Label2.Caption
End
End If
'咬到自己
If slong > 4 Then
For i = head - 1 To head - slong + 1 Step -1
If snake(head).Left = snake(i).Left And snake(head).Top = snake(i).Top Then
MsgBox "咬到自己了" & Chr(13) & "您的分数:" & Label2.Caption
End
End If
Next
End If
Label2.Caption = prefen + base * (slong - 1)
'过关
If slong > 9 Then
Label2.Caption = prefen + base * slong
'过关
Label4.Caption = Label4.Caption + 1
MsgBox "恭喜您过关!下面进入第" & Label4.Caption & "关速度和分数都会增加"
Timer1.Interval = Timer1.Interval * 0.8
base = base + 1
For i = head To head - slong + 1 Step -1
Unload snake(i)
Next
Load snake(1)
head = 1
slong = 1
Timer1.Enabled = False
eat = True
prefen = Label2.Caption
Command1_Click
End If
head = head + 1

End Sub

Private Sub zd_Click()
Timer1.Interval = 100
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -