混
DO IT YOURSELF !
'VB6 贪吃蛇 Option Explicit Dim aspect Dim Score As Integer Private Sub Begin_Click() Lbl_start_Click End Sub Private Sub Exit_Click() If MsgBox("Í˳öÓÎÏ·£¿", vbYesNo + vbInformation, "ÐÅÏ¢Ìáʾ") = vbYes Then End End Sub Private Sub Form_Activate() Dim i As Integer Pic(0).Left = 20 Pic(0).Top = 50 For i = 1 To Pic.UBound Pic(i).Left = Pic(i - 1).Left - 5 Pic(i).Top = Pic(i - 1).Top Next i aspect = 2 End Sub Private Sub Form_Resize() PicRect.Cls Dim i As Integer PicRect.ScaleWidth = 150 PicRect.ScaleHeight = 125 For i = 0 To PicRect.ScaleWidth Step 5 ' PicRect.Line (i, 0)-(i, PicRect.ScaleHeight), vbYellow ' PicRect.Line (0, i)-(PicRect.ScaleWidth, i), vbYellow Next i For i = 0 To Pic.UBound Pic(i).Width = Int(PicRect.ScaleWidth / 30) Pic(i).Height = Int(PicRect.ScaleHeight / 25) Next i End Sub Private Sub Label2_Click() End Sub Private Sub Lbl_start_Click() Dim i As Integer PicAdd.Visible = False Picmore.Visible = False Pic(0).Left = 15 Pic(0).Top = 50 For i = Pic.UBound To 4 Step -1 Unload Pic(i) Next i For i = 1 To Pic.UBound Pic(i).Left = Pic(i - 1).Left - Pic(i).Width Pic(i).Top = Pic(i - 1).Top Next i ' Command2.Caption = "ÔÝÍ£" aspect = 2 Label1.Caption = 0 Timer1.Enabled = True Timer2.Enabled = True End Sub Private Sub Lbl_stop_Click() If Lbl_stop.Tag = "ÔÝÍ£" Then Lbl_stop.Tag = "¼ÌÐø" Timer1.Enabled = False Timer2.Enabled = False Else Lbl_stop.Tag = "ÔÝÍ£" Timer1.Enabled = True Timer2.Enabled = True End If End Sub Private Sub Pause_Click() Timer1.Enabled = False Timer2.Enabled = False End Sub Private Sub PicRect_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 37 And Pic(0).Top <> Pic(1).Top Then aspect = 1 'Ïò×ó±ê¼ÇΪ1 ElseIf KeyCode = 40 And Pic(0).Left <> Pic(1).Left Then aspect = 4 'Ïòϱê¼ÇΪ4 ElseIf KeyCode = 39 And Pic(0).Top <> Pic(1).Top Then aspect = 2 'ÏòÓÒ±ê¼ÇΪ2 ElseIf KeyCode = 38 And Pic(0).Left <> Pic(1).Left Then aspect = 3 'ÏòÉϱê¼ÇΪ3 End If End Sub Private Sub posthaste_Click() Timer1.Interval = 100 End Sub Private Sub rapid_Click() Timer1.Interval = 300 End Sub Private Sub slow_Click() Timer1.Interval = 500 End Sub Private Sub Timer1_Timer() Dim PreX, PreY Dim PreX1, PreY1 Dim i As Integer For i = 0 To Pic.UBound If i = 0 Then PreX1 = Pic(0).Left PreY1 = Pic(0).Top If aspect = 2 Then Pic(0).Left = Pic(0).Left + 5 ElseIf aspect = 1 Then Pic(0).Left = Pic(0).Left - 5 ElseIf aspect = 4 Then Pic(0).Top = Pic(0).Top + 5 ElseIf aspect = 3 Then Pic(0).Top = Pic(0).Top - 5 End If Else If Pic(0).Left = Pic(i).Left And Pic(0).Top = Pic(i).Top Then Timer1.Enabled = False Timer2.Enabled = False End If PreX = Pic(i).Left PreY = Pic(i).Top Pic(i).Left = PreX1 Pic(i).Top = PreY1 PreX1 = PreX PreY1 = PreY End If Next i If Pic(0).Left < 0 Or Pic(0).Left + Pic(0).Width > PicRect.ScaleWidth _ Or Pic(0).Top < 0 Or Pic(0).Top + Pic(0).Height > PicRect.ScaleHeight Then Timer1.Enabled = False Timer2.Enabled = False End If PicRect.SetFocus If Pic(0).Left = PicAdd.Left And Pic(0).Top = PicAdd.Top Then Load Pic(Pic.Count) Set Pic(Pic.UBound).Container = PicRect Pic(Pic.UBound).Picture = Pic(1).Picture Pic(Pic.UBound).Visible = True PicAdd.Visible = False Score = Score + 1 Label1.Caption = Score End If If Pic(0).Left = Picmore.Left And Pic(0).Top = Picmore.Top Then Score = Score + 5 Label1.Caption = Score For i = 1 To 3 Load Pic(Pic.Count) Set Pic(Pic.UBound).Container = PicRect Pic(Pic.UBound).Picture = Pic(1).Picture Pic(Pic.UBound).Visible = True Picmore.Visible = False Next i End If End Sub Private Sub Timer2_Timer() Dim i As Integer Static n As Integer n = n + 1 X: If n Mod 5 = 0 Then PicAdd.Visible = False PicAdd.Left = 100 Picmore.Left = Int(Rnd * 10) * 10 Picmore.Top = Int(Rnd * 10) * 10 For i = 0 To Pic.UBound If Picmore.Left = Pic(i).Left And Picmore.Top = Pic(i).Top Then GoTo X Next i Picmore.Visible = True Else Picmore.Visible = False Picmore.Left = 100 PicAdd.Left = Int(Rnd * 10) * 10 PicAdd.Top = Int(Rnd * 10) * 10 For i = 0 To Pic.UBound If PicAdd.Left = Pic(i).Left And PicAdd.Top = Pic(i).Top Then GoTo X Next i PicAdd.Visible = True End If End Sub