none
請教各位高手幫小弟看一下程式碼 RRS feed

解答

  • '用你的程式碼稍微改一下,變成如下,請參考:
    '修改:改成使用一個Timer就好,改以蛇陣列(0)當做是蛇頭


    Public Class Form1
    Const U As Integer = 0
    Const D As Integer = 1
    Const L As Integer = 2
    Const R As Integer = 3

    Dim 蛇身數 As Integer = 0
    Dim 蛇陣列(蛇身數) As Label
    Dim 蛇頭(3) As String
    Dim direction As Integer
    Dim NotDoing As Boolean

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    direction = L
    Timer1.Interval = 1
    蛇頭(U) = "↑"
    蛇頭(D) = "↓"
    蛇頭(L) = "←"
    蛇頭(R) = "→"
    NotDoing = True
    蛇身數 = 0 '以蛇陣列(0)當做是蛇頭
    Call 蛇增加身體()
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Right Then
    direction = R
    ElseIf e.KeyCode = Keys.Left Then
    direction = L
    ElseIf e.KeyCode = Keys.Up Then
    direction = U
    ElseIf e.KeyCode = Keys.Down Then
    direction = D
    ElseIf e.KeyCode = Keys.Space Then ' 按Space啟動或暫停
    Timer1.Enabled = Not Timer1.Enabled
    End If
    蛇陣列(0).Text = 蛇頭(direction)
    End Sub

    Sub 蛇增加身體()
    ReDim Preserve 蛇陣列(蛇身數)
    蛇陣列(蛇身數) = New Label
    蛇陣列(蛇身數).Name = "蛇身數" & 蛇身數
    蛇陣列(蛇身數).AutoSize = True
    If 蛇身數 = 0 Then
    蛇陣列(蛇身數).Text = 蛇頭(direction)
     蛇陣列(蛇身數).Top = 300
     蛇陣列(蛇身數).Left = 300
     Else 蛇陣列(蛇身數).Text = "@" 蛇陣列(蛇身數).BackColor = Color.Red End If Me.Controls.Add(蛇陣列(蛇身數)) End Sub Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim i As Integer, HH As Long, LL As Long, K(2) As Integer Select Case direction Case U 蛇陣列(0).Top -= 1 HH = 蛇陣列(0).Height LL = 0 Case D 蛇陣列(0).Top += 1 HH = 0 - 蛇陣列(0).Height LL = 0 Case L 蛇陣列(0).Left -= 1 HH = 0 LL = 蛇陣列(0).Width - 4 Case R 蛇陣列(0).Left += 1 HH = 0 LL = 0 - 蛇陣列(0).Width + 4 End Select If EatFood() Then 蛇身數 += 1 蛇增加身體() Else For i = 1 To 蛇身數 蛇陣列(i).Location = New Point(蛇陣列(i - 1).Left + LL, 蛇陣列(i - 1).Top + HH) Next End If End Sub Private Function EatFood() As Boolean '判斷有沒有吃到食物(以蛇頭的正中間點來判斷) Dim PP(1) As Long, PS(3) As Long PP(0) = 蛇陣列(0).Left + (蛇陣列(0).Width / 2) PP(1) = 蛇陣列(0).Top + (蛇陣列(0).Height / 2) PS(0) = lblfood.Left PS(1) = lblfood.Top PS(2) = lblfood.Left + lblfood.Width PS(3) = lblfood.Top + lblfood.Height If (PP(0) > PS(0) And PP(0) < PS(2)) And (PP(1) > PS(1) And PP(1) < PS(3)) Then If NotDoing = False Then EatFood = True NotDoing = True End If Else EatFood = False NotDoing = False End If End Function End Class

    Jones
    2009年7月19日 上午 07:47

所有回覆

  • Q1:先判斷視窗的大小假如蛇從右邊跑出去就寫一個左邊進來依此類推
    • 已提議為解答 SUPBF 2009年7月17日 上午 08:37
    • 已取消提議為解答 bauannModerator 2009年7月17日 上午 11:48
    2009年7月17日 上午 08:36
  • Q2:跑迴圈加亂數值就可以了
    • 已提議為解答 SUPBF 2009年7月17日 上午 08:38
    • 已取消提議為解答 bauannModerator 2009年7月17日 上午 11:48
    2009年7月17日 上午 08:38
  •  如果你有空閒時間的話 可以下載程式碼看一下嗎....
    不好意思喔 因為你兩個解答有點不是我想發問的問題....
    真的很不好意思喔.....真的很謝謝你
    2009年7月17日 下午 02:39
  • '用你的程式碼稍微改一下,變成如下,請參考:
    '修改:改成使用一個Timer就好,改以蛇陣列(0)當做是蛇頭


    Public Class Form1
    Const U As Integer = 0
    Const D As Integer = 1
    Const L As Integer = 2
    Const R As Integer = 3

    Dim 蛇身數 As Integer = 0
    Dim 蛇陣列(蛇身數) As Label
    Dim 蛇頭(3) As String
    Dim direction As Integer
    Dim NotDoing As Boolean

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    direction = L
    Timer1.Interval = 1
    蛇頭(U) = "↑"
    蛇頭(D) = "↓"
    蛇頭(L) = "←"
    蛇頭(R) = "→"
    NotDoing = True
    蛇身數 = 0 '以蛇陣列(0)當做是蛇頭
    Call 蛇增加身體()
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Right Then
    direction = R
    ElseIf e.KeyCode = Keys.Left Then
    direction = L
    ElseIf e.KeyCode = Keys.Up Then
    direction = U
    ElseIf e.KeyCode = Keys.Down Then
    direction = D
    ElseIf e.KeyCode = Keys.Space Then ' 按Space啟動或暫停
    Timer1.Enabled = Not Timer1.Enabled
    End If
    蛇陣列(0).Text = 蛇頭(direction)
    End Sub

    Sub 蛇增加身體()
    ReDim Preserve 蛇陣列(蛇身數)
    蛇陣列(蛇身數) = New Label
    蛇陣列(蛇身數).Name = "蛇身數" & 蛇身數
    蛇陣列(蛇身數).AutoSize = True
    If 蛇身數 = 0 Then
    蛇陣列(蛇身數).Text = 蛇頭(direction)
     蛇陣列(蛇身數).Top = 300
     蛇陣列(蛇身數).Left = 300
     Else 蛇陣列(蛇身數).Text = "@" 蛇陣列(蛇身數).BackColor = Color.Red End If Me.Controls.Add(蛇陣列(蛇身數)) End Sub Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim i As Integer, HH As Long, LL As Long, K(2) As Integer Select Case direction Case U 蛇陣列(0).Top -= 1 HH = 蛇陣列(0).Height LL = 0 Case D 蛇陣列(0).Top += 1 HH = 0 - 蛇陣列(0).Height LL = 0 Case L 蛇陣列(0).Left -= 1 HH = 0 LL = 蛇陣列(0).Width - 4 Case R 蛇陣列(0).Left += 1 HH = 0 LL = 0 - 蛇陣列(0).Width + 4 End Select If EatFood() Then 蛇身數 += 1 蛇增加身體() Else For i = 1 To 蛇身數 蛇陣列(i).Location = New Point(蛇陣列(i - 1).Left + LL, 蛇陣列(i - 1).Top + HH) Next End If End Sub Private Function EatFood() As Boolean '判斷有沒有吃到食物(以蛇頭的正中間點來判斷) Dim PP(1) As Long, PS(3) As Long PP(0) = 蛇陣列(0).Left + (蛇陣列(0).Width / 2) PP(1) = 蛇陣列(0).Top + (蛇陣列(0).Height / 2) PS(0) = lblfood.Left PS(1) = lblfood.Top PS(2) = lblfood.Left + lblfood.Width PS(3) = lblfood.Top + lblfood.Height If (PP(0) > PS(0) And PP(0) < PS(2)) And (PP(1) > PS(1) And PP(1) < PS(3)) Then If NotDoing = False Then EatFood = True NotDoing = True End If Else EatFood = False NotDoing = False End If End Function End Class

    Jones
    2009年7月19日 上午 07:47
  • 謝謝你 非常厲害的大大..... 真的很感謝你唷
    剛剛看了一下 有點難度= =
    我好好研究一下

     

    2009年7月25日 下午 05:04