Advertisement
Tony041010

貪食蛇Snake

Jul 15th, 2021
418
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dim snk As String '記錄蛇身體所有的位置
  2. Dim sdir As Integer  '行進方向
  3. Dim shead As Integer  '頭的座標
  4. Dim apple As Integer   '蘋果座標
  5. Dim obstacle As Integer
  6.  
  7. Private Sub Form_Load()
  8.     For i = 0 To 360
  9.         If i > 0 Then
  10.             Load Command1(i)  'load可以動態產生物件
  11.            If i Mod 19 = 0 Then
  12.                 Command1(i).Left = Command1(0).Left
  13.                 Command1(i).Top = Command1(i - 9).Top + Command1(i).Height
  14.             Else
  15.                 Command1(i).Left = Command1(i - 1).Left + Command1(i - 1).Width
  16.                 Command1(i).Top = Command1(i - 1).Top
  17.  
  18.             End If
  19.         End If
  20.         If i Mod 2 = 0 Then
  21.             Command1(i).BackColor = RGB(115, 167, 12)
  22.         Else
  23.             Command1(i).BackColor = RGB(162, 209, 73)
  24.         End If
  25.        ' Command1(i).Caption = i
  26.        Command1(i).Visible = True
  27.     Next
  28.     For i = 0 To 18
  29.         Command1(i).BackColor = vbBlack
  30.     Next
  31.     For i = 342 To 360
  32.         Command1(i).BackColor = vbBlack
  33.     Next
  34.     For i = 0 To 342 Step 19
  35.         Command1(i).BackColor = vbBlack
  36.     Next
  37.     For i = 18 To 360 Step 19
  38.         Command1(i).BackColor = vbBlack
  39.     Next
  40.    
  41. End Sub
  42.  
  43. Private Sub NewApple()
  44.     apple = -1
  45.     Do
  46.         x = Int(Rnd * 361)
  47.         If Command1(x).BackColor <> vbBlack And Command1(x).BackColor <> vbBlue Then
  48.             apple = x
  49.         End If
  50.     Loop Until apple <> -1
  51.     Command1(apple).BackColor = vbRed
  52.     Command1(apple).Picture = Image2
  53. End Sub
  54. Private Sub NewObstacle()
  55.     obstacle = -1
  56.     Do
  57.         y = Int(Rnd * 361)
  58.         If Command1(y).BackColor <> vbBlack And Command1(y).BackColor <> vbBlue And y <> apple And Command1(y).BackColor <> RGB(30, 30, 30) Then
  59.             obstacle = y
  60.         End If
  61.     Loop Until obstacle <> -1
  62.     Command1(obstacle).BackColor = RGB(30, 30, 30)
  63. End Sub
  64.  
  65. Private Sub Label1_Click()
  66.     Command1(175).BackColor = vbBlue
  67.     Command1(176).BackColor = vbBlue
  68.     Command1(177).BackColor = vbBlue
  69.     snk = ChrW(175) & ChrW(176) & ChrW(177)
  70.     sdir = 1            '一開始預設向右
  71.    shead = 177         '一開始預設蛇首117
  72.    NewApple
  73.    
  74.     Timer1.Interval = 400
  75. End Sub
  76.  
  77. '第一題,完成方向鍵
  78. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  79.     If KeyCode = vbKeyUp Then
  80.         sdir = -19
  81.     ElseIf KeyCode = vbKeyDown Then
  82.         sdir = 19
  83.     ElseIf KeyCode = vbKeyLeft Then
  84.         sdir = -1
  85.     ElseIf KeyCode = vbKeyRight Then
  86.         sdir = 1
  87.     End If
  88.  
  89. End Sub
  90. Private Sub Timer1_Timer()
  91.     shead = shead + sdir
  92.    
  93.     '第二題,撞牆
  94.    If Command1(shead).BackColor = vbBlack Then
  95.         MsgBox "hit wall, game over"
  96.         Timer1.Interval = 0
  97.     End If
  98.     '第二題,咬到自己
  99.    If Command1(shead).BackColor = vbBlue Then
  100.         MsgBox ("bite self")
  101.         Timer1.Interval = 0
  102.     End If
  103.    
  104.     If Command1(shead).BackColor = RGB(30, 30, 30) Then
  105.         MsgBox ("hit obstacle, game over")
  106.         Timer1.Interval = 0
  107.     End If
  108.  
  109.     '第三題,吃蘋果len+1
  110.    '更新snk座標,
  111.    '如果吃到蘋果,長度+1,留住最後一截尾巴,
  112.    '若沒吃到,消掉最後一截尾巴
  113.    If shead = apple Then
  114.         Command1(apple).Picture = LoadPicture()
  115.         If apple Mod 2 = 0 Then
  116.             Command1(apple).BackColor = RGB(115, 167, 12)
  117.         Else
  118.             Command1(apple).BackColor = RGB(162, 209, 73)
  119.         End If
  120.         snk = snk & ChrW(apple)
  121.         NewApple
  122.         NewObstacle
  123.     Else
  124.         '先記錄尾巴座標
  125.        stail = AscW(Mid(snk, 1, 1))
  126.         snk = Mid(snk, 2, Len(snk)) & ChrW(shead)  'snk削掉尾巴最後一格
  127.        '繪圖,snk削掉尾巴最後一格,還是要符合馬賽克磚
  128.        i = stail
  129.         If i Mod 2 = 0 Then
  130.             Command1(i).BackColor = RGB(115, 167, 12)
  131.         Else
  132.             Command1(i).BackColor = RGB(162, 209, 73)
  133.         End If
  134.    
  135.     End If
  136.    
  137.     '繪圖,頭前進一格
  138.    Command1(shead).BackColor = vbBlue
  139.    
  140.     Label2 = Len(snk) '目前蛇的長度
  141.    
  142.     If Label2 >= 15 Then
  143.         Timer1.Interval = 150
  144.     ElseIf Label2 >= 10 Then
  145.         Timer1.Interval = 200
  146.     ElseIf Label2 >= 5 Then
  147.     Timer1.Interval = 300
  148.     End If
  149. End Sub
  150.  
  151.  
Advertisement
RAW Paste Data Copied
Advertisement