Qbasicnews.com

Full Version: Visual Basic Snake
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ok. although its not that hard of a game to make, snake can give you programmers block in an instance, especially with VB6's crappy debuging routines. but i finally got this thing working. so here it is, snake in VB. you just need to make a label(label1) and a maxed out form. (change the delay if you have to.[very unstable code]):
Code:
Dim Snakex As Integer
Dim delay As Double
Dim Snakey As Integer
Dim SnakexMov As Integer
Dim SnakeyMov As Integer
Dim applex As Integer
Dim appley As Integer
Dim score As Integer
Dim applecolor As Long
Private Sub Form_Load()
Snakex = 350
Snakey = 350
delay = 80000
SnakexMov = 1
SnakeyMov = 0
applecolor = vbRed
Form1.BackColor = vbGreen
Randomize Timer
    applex = Int(600 * Rnd) + 10
    appley = Int(600 * Rnd) + 10
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
    If SnakeyMov <> 1 Then
        SnakeyMov = -1
        SnakexMov = 0
    End If
End If
If KeyCode = vbKeyDown Then
    If SnakeyMov <> -1 Then
        SnakeyMov = 1
        SnakexMov = 0
    End If
End If
If KeyCode = vbKeyLeft Then
    If SnakexMov <> 1 Then
        SnakeyMov = 0
        SnakexMov = -1
    End If
End If
If KeyCode = vbKeyRight Then
    If SnakexMov <> -1 Then
        SnakeyMov = 0
        SnakexMov = 1
    End If
End If
If KeyCode = vbKeyEscape Then
    End
End If
If KeyCode = vbKeyReturn Then
Unload Me
Form1.Show
score = 0
Randomize Timer
Label1.BackColor = vbWhite
Label1.ForeColor = vbWhite
Line (10, 10)-(750, 750), vbWhite, BF
Do
    DoEvents
    Form1.SetFocus
    PSet (Snakex, Snakey), vbGreen
    Line (applex - 3, appley - 5)-(applex + 3, appley), vbGreen + 1, BF
    Line (applex, appley)-(applex + 10, appley + 10), applecolor, BF
        For i = 1 To delay: Next
        Snakex = Snakex + SnakexMov
        Snakey = Snakey + SnakeyMov
        colortest = Point(Snakex + SnakexMov, Snakey + SnakeyMov)
        If colortest = vbGreen + 1 Or colortest = applecolor Then
            Line (applex - 3, appley - 5)-(applex + 10, appley + 10), vbWhite, BF
            'Line (10, 10)-(750, 750), vbWhite, BF
            score = score + 10
            delay = delay - 10000
            If score > 30 And score < 60 Then delay = delay + 5000
            If score > 60 Then delay = delay + 7500
            applex = Int(600 * Rnd) + 10
            appley = Int(600 * Rnd) + 10
        ElseIf colortest <> vbWhite Then
        Label1.Left = 250
            Label1.ForeColor = vbBlack
            Label1.BackColor = vbWhite
            Label1.Caption = "                         You Lose                  " & Chr(10) & "                 Your Score was:  " & score & "" & Chr(10) & "Press 'Enter' to play again and 'Esc' to exit"
            Label1.AutoSize = True
            Exit Do
            Exit Sub
            End
        End If
Loop
End If
End Sub
Er...set up the project, maybe, and email it to me? I'd like to see this layed out, but I don't have the time to make it.