Snake game code you all wanted

Questions about programming languages and debugging
Post Reply
User avatar
floodhound2
∑lectronic counselor
∑lectronic counselor
Posts: 2117
Joined: 03 Sep 2006, 16:00
17
Location: 127.0.0.1
Contact:

Snake game code you all wanted

Post by floodhound2 »

Let me know if you need help
it is in VB

Code: Select all

Private Declare Function GetTickCount Lib "kernel32" () As Long 'this function lets us not use timer
'timers are bad :)
Dim levela As Long
Dim r As Long

Private Type Part
X As Integer
Y As Integer
End Type

'Dynamic array to store part coordinates
Dim Part() As Part

'Velocity in X and Y direction of the snake
Dim vX As Integer, vY As Integer
Dim i As Integer 'for loops
Dim CS As Single 'cell size

Dim FX As Integer, FY As Integer 'food coordinates
Dim X As Integer, Y As Integer

Dim bRunning As Boolean, died As Boolean

Private Sub a_Click()
MsgBox "Use arrow keys to move the snake and try to make it as long a possible by eating the food."
End Sub

Private Sub about_Click()
MsgBox "Programmed by Floodhound for Suck-o users to practice thinking outside the box"

End Sub

Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()

levela = 100
r = 0

Randomize 'random generation

'Initialize controls******************
Picture1.BackColor = vbWhite
Picture1.ScaleMode = 3 'pixels

CS = 20 'cell size in pixels
X = Int(Picture1.ScaleWidth / CS)
Y = Int(Picture1.ScaleHeight / CS)


Picture1.AutoRedraw = True
Picture1.ScaleWidth = X * CS
Picture1.ScaleHeight = Y * CS

Me.WindowState = 0
Me.Show
Form2.Show


DrawGrid Picture1, CS
'*************************************

died = False
'set up the game
ReDim Part(0)
Part(0).X = 0
Part(0).Y = 0

FX = Int(Rnd * X)
FY = Int(Rnd * Y)
'go to main loop
bRunning = True
MainLoop

End Sub

Sub MainLoop()
Do While bRunning = True
    Update
    Draw
    WAIT (levela)
    'WAIT (100) 'increasing this number makes game slower
Loop

Unload Me
End Sub

Sub Update()
'MOVE PARTS
For i = UBound(Part) To 1 Step -1
    Part(i).X = Part(i - 1).X
    Part(i).Y = Part(i - 1).Y
Next i

'MOVE HEAD
Part(0).X = Part(0).X + vX
Part(0).Y = Part(0).Y + vY

'HAS HE GONE OUT OF BOUNDS ?
If Part(0).X < 0 Or Part(0).X >= X Or Part(0).Y < 0 Or Part(0).Y >= Y Then
died = True
End If

'HAS HE CRASHED INTO HIMSELF ?
For i = 1 To UBound(Part)
If Part(i).X = Part(0).X And Part(i).Y = Part(0).Y Then
died = True
End If
Next i

'DID HE EAT FOOD ?
If Part(0).X = FX And Part(0).Y = FY Then
    ReDim Preserve Part(UBound(Part) + 1)
    Part(UBound(Part)).X = -CS
    Part(UBound(Part)).Y = -CS
    FX = Int(Rnd * X)
    FY = Int(Rnd * Y)
    
    Label1.Caption = "Score :  " & UBound(Part)
End If

'IS HE DEAD ?
If died = True Then NewGame
End Sub

Sub Draw()
    'DRAW WHITENESS
    Rectangle 0, 0, X * CS, Y * CS, vbBlack
    'DRAW SNAKE. PARTS IN BLUE, HEAD IN GREEN
    For i = 1 To UBound(Part)
    Rectangle Part(i).X * CS, Part(i).Y * CS, Part(i).X * CS + CS, Part(i).Y * CS + CS, vbBlue
    Next i
    Rectangle Part(0).X * CS, Part(0).Y * CS, Part(0).X * CS + CS, Part(0).Y * CS + CS, vbGreen
    'DRAW FOOD
    Rectangle FX * CS, FY * CS, FX * CS + CS, FY * CS + CS, vbRed
    
    DrawGrid Picture1, CS
End Sub

Sub Rectangle(X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer, color As Long)
    Picture1.Line (X1, Y1)-(X2, Y2), color, BF
End Sub

Sub NewGame()
'SET UP NEW GAME
died = False

ReDim Part(0)
Part(0).X = 0
Part(0).Y = 0

vX = 0
vY = 0

FX = Int(Rnd * X)
FY = Int(Rnd * Y)
End Sub

Sub DrawGrid(Pic As Control, CS As Single)
    '**************************************************************************
    'DRAW GRID
    '**************************************************************************
    Dim i As Integer, Across As Single, Up As Single
    
    Across = Pic.ScaleWidth / CS
    Up = Pic.ScaleHeight / CS
    
    For i = 0 To Across
        Pic.Line (i * CS, 0)-(i * CS, Up * CS)
    Next i
    
    For i = 0 To Up
        Pic.Line (0, i * CS)-(Across * CS, i * CS)
    Next i
End Sub

Sub WAIT(Tim As Integer)
    '**************************************************************************
    'WAIT FUNCTION
    '**************************************************************************
    Dim LastWait As Long
    LastWait = GetTickCount
    
    Do While Tim > GetTickCount - LastWait
    DoEvents
    Loop
End Sub



Private Sub level_Click()
levela = 20

End Sub

Private Sub levelmed_Click()
levela = 50

End Sub

Private Sub levelslow_Click()
levela = 100

End Sub

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
'USER KEYPRESSES HANDLED HERE
Select Case KeyCode
Case vbKeyRight
vX = 1
vY = 0
Case vbKeyLeft
vX = -1
vY = 0
Case vbKeyUp
vX = 0
vY = -1
Case vbKeyDown
vX = 0
vY = 1
End Select
End Sub

Private Sub Picture1_KeyPress(KeyAscii As Integer)
'27 is ESC. IF user presses ESC, QUIT
If KeyAscii = 27 Then r = r + 1
Label2.Caption = r
    If Label2.Caption = "5" Then
    Form2.Show
    bRunning = False
    Form1.Visible = False
    
    r = 0
    Label1.Caption = ""
    End If
    


'If KeyAscii = 27 Then bRunning = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
'This function can be left out

bRunning = False
Form1.Visible = False


End Sub
Have fun
₣£ΘΘĐĦΘŮŇĐ

User avatar
floodhound2
∑lectronic counselor
∑lectronic counselor
Posts: 2117
Joined: 03 Sep 2006, 16:00
17
Location: 127.0.0.1
Contact:

Post by floodhound2 »

Forgot to mention that you can remove the form2.show in the form_load function.

It was used for a password input during the crack it challange. Just comment it out or remove it.
₣£ΘΘĐĦΘŮŇĐ

shamir
Computer Manager
Computer Manager
Posts: 853
Joined: 01 Mar 2007, 17:00
17
Location: NY
Contact:

Post by shamir »

looks like a nice code not bad for a math dude :lol:

Post Reply