-
Jun 6th, 2010, 03:25 PM
#1
Thread Starter
New Member
Visual BAsic 6 Snake Game
Can someone tell me how i can detect if my snake hits itself besides searching if its coordinates match any of its body parts' coordinates unless u can show me how to do that in a loop?
-
Jun 6th, 2010, 03:38 PM
#2
Re: Visual BAsic 6 Snake Game
MC
Welcome to the Forums ..
However, you can help us help you ... if you
- provide a more detailed description
- provide some of the code you already have
- provide a screenshot.
And, FWIW, the short answer: yes, you'll need a loop.
Spoo
-
Jun 6th, 2010, 03:39 PM
#3
Re: Visual BAsic 6 Snake Game
One way could be, assuming you are using a grid type field, is to keep a two dimensional array that represents the gird, holding the contents of the grid fields.
For example your grid is 20x20 and at the start of the game your snake is 3 fields long and occupies fields [1,1], [1,2], [1,3] (it's in the top left corner, horizontally). So, for those indexes your array will have a value, lets say "s", meaning that the snake is occupying it. Similarly, you can, at random load fruit/food items in the empty fields of the grid. As the snake moves to the next field, you simply check the contents of that field in the array, if it's empty nothing happens (snake just moves), if it has a fruit, elongate the snake, if it has the "s" marker, the snake hit itself = game over.
-
Jun 7th, 2010, 06:11 AM
#4
Re: Visual BAsic 6 Snake Game
Moved To Games And Graphics Programming
-
Jun 7th, 2010, 05:32 PM
#5
Thread Starter
New Member
Re: Visual BAsic 6 Snake Game
ok so here is my code and some screens. the game is pretty simple and im not using any grids . also each body part is 100x100 twips while the playing area is 7800x6000 for the guy who was talking about grids.
Option Explicit
Dim Score As Integer
Dim Length As Integer
Dim Direction As Integer
Dim Coordinate As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then
Direction = 1
tmrLoop.Enabled = True
ElseIf KeyCode = 38 Then
Direction = 2
tmrLoop.Enabled = True
ElseIf KeyCode = 39 Then
Direction = 3
tmrLoop.Enabled = True
ElseIf KeyCode = 40 Then
Direction = 4
tmrLoop.Enabled = True
End If
End Sub
Private Sub Form_Load()
Randomize
Call PlaceFood
Length = 5
Score = 0
End Sub
Private Sub tmrLoop_Timer()
Call FindFood
Call MoveSnake
End Sub
Public Sub PlaceFood()
Do
Coordinate = Int(Rnd * (7800 - imgFood.Width))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Left = Coordinate
Do
Coordinate = Int(Rnd * (6000 - imgFood.Height))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Top = Coordinate
End Sub
Public Sub MoveSnake()
Dim Count As Integer
For Count = (Length - 1) To 1 Step (-1)
imgSnake(Count).Left = imgSnake(Count - 1).Left
imgSnake(Count).Top = imgSnake(Count - 1).Top
Next Count
If Direction = 1 Then
imgSnake(0).Left = imgSnake(0).Left - 100
ElseIf Direction = 2 Then
imgSnake(0).Top = imgSnake(0).Top - 100
ElseIf Direction = 3 Then
imgSnake(0).Left = imgSnake(0).Left + 100
ElseIf Direction = 4 Then
imgSnake(0).Top = imgSnake(0).Top + 100
End If
If imgSnake(0).Left = 0 Or imgSnake(0).Left = 7800 Or imgSnake(0).Top = 0 Or imgSnake(0).Top = 6000 Then
If Score < 10000 Then
MsgBox "FAIL! You didn't beat the high score."
Else: MsgBox "You Win!"
End If
Unload Me
End If
End Sub
Public Sub FindFood()
If imgSnake(0).Left = imgFood.Left And imgSnake(0).Top = imgFood.Top Then
Load imgSnake(Length)
If Direction = 1 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 2 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Top = imgSnake(Length - 1).Top + 100
imgSnake(Length).Visible = True
ElseIf Direction = 3 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 4 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Left = imgSnake(Length - 1).Top - 100
imgSnake(Length).Visible = True
End If
Length = Length + 1
Score = Score + 100
If Score = 500 Then
tmrLoop.Interval = 40
ElseIf Score = 1000 Then
tmrLoop.Interval = 30
ElseIf Score = 1500 Then
tmrLoop.Interval = 20
ElseIf Score = 2000 Then
tmrLoop.Interval = 10
ElseIf Score = 2500 Then
tmrLoop.Interval = 5
ElseIf Score = 3000 Then
tmrLoop.Interval = 1
End If
Label1.Caption = "Score = " & Score
Call PlaceFood
End If
End Sub
-
Jun 7th, 2010, 05:48 PM
#6
Thread Starter
New Member
Re: Visual BAsic 6 Snake Game
my code also im not using grids
[code]
Option Explicit
Dim Score As Integer
Dim Length As Integer
Dim Direction As Integer
Dim Coordinate As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then
Direction = 1
tmrLoop.Enabled = True
ElseIf KeyCode = 38 Then
Direction = 2
tmrLoop.Enabled = True
ElseIf KeyCode = 39 Then
Direction = 3
tmrLoop.Enabled = True
ElseIf KeyCode = 40 Then
Direction = 4
tmrLoop.Enabled = True
End If
End Sub
Private Sub Form_Load()
Randomize
Call PlaceFood
Length = 5
Score = 0
End Sub
Private Sub tmrLoop_Timer()
Call FindFood
Call MoveSnake
End Sub
Public Sub PlaceFood()
Do
Coordinate = Int(Rnd * (7800 - imgFood.Width))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Left = Coordinate
Do
Coordinate = Int(Rnd * (6000 - imgFood.Height))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Top = Coordinate
End Sub
Public Sub MoveSnake()
Dim Count As Integer
For Count = (Length - 1) To 1 Step (-1)
imgSnake(Count).Left = imgSnake(Count - 1).Left
imgSnake(Count).Top = imgSnake(Count - 1).Top
Next Count
If Direction = 1 Then
imgSnake(0).Left = imgSnake(0).Left - 100
ElseIf Direction = 2 Then
imgSnake(0).Top = imgSnake(0).Top - 100
ElseIf Direction = 3 Then
imgSnake(0).Left = imgSnake(0).Left + 100
ElseIf Direction = 4 Then
imgSnake(0).Top = imgSnake(0).Top + 100
End If
If imgSnake(0).Left = 0 Or imgSnake(0).Left = 7800 Or imgSnake(0).Top = 0 Or imgSnake(0).Top = 6000 Then
If Score < 10000 Then
MsgBox "FAIL! You didn't beat the high score."
Else: MsgBox "You Win!"
End If
Unload Me
End If
End Sub
Public Sub FindFood()
If imgSnake(0).Left = imgFood.Left And imgSnake(0).Top = imgFood.Top Then
Load imgSnake(Length)
If Direction = 1 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 2 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Top = imgSnake(Length - 1).Top + 100
imgSnake(Length).Visible = True
ElseIf Direction = 3 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 4 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Left = imgSnake(Length - 1).Top - 100
imgSnake(Length).Visible = True
End If
Length = Length + 1
Score = Score + 100
If Score = 500 Then
tmrLoop.Interval = 40
ElseIf Score = 1000 Then
tmrLoop.Interval = 30
ElseIf Score = 1500 Then
tmrLoop.Interval = 20
ElseIf Score = 2000 Then
tmrLoop.Interval = 10
ElseIf Score = 2500 Then
tmrLoop.Interval = 5
ElseIf Score = 3000 Then
tmrLoop.Interval = 1
End If
Label1.Caption = "Score = " & Score
Call PlaceFood
End If
End Sub
-
Jun 7th, 2010, 05:50 PM
#7
Thread Starter
New Member
Re: Visual BAsic 6 Snake Game
-
Jun 7th, 2010, 08:45 PM
#8
Thread Starter
New Member
Re: Visual BAsic 6 Snake Game
2 things
how do i make it so if the snake is going left it cant go right and if its going up it cant go down because right now it goes right through itself.
and also im getting a run time error 340. im trying to make a loop to see if the snake head hits any of its parts but then when i die it says something about the control arrays. heres my code
Code:
Option Explicit
Private Score As Integer
Private Length As Integer
Private Direction As Integer
Private Coordinate As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then
Direction = 1
tmrLoop.Enabled = True
ElseIf KeyCode = 38 Then
Direction = 2
tmrLoop.Enabled = True
ElseIf KeyCode = 39 Then
Direction = 3
tmrLoop.Enabled = True
ElseIf KeyCode = 40 Then
Direction = 4
tmrLoop.Enabled = True
End If
End Sub
Private Sub Form_Load()
Randomize
Call PlaceFood
Length = 5
Score = 0
End Sub
Private Sub tmrLoop_Timer()
Call FindFood
Call MoveSnake
End Sub
Public Sub PlaceFood()
Do
Coordinate = Int(Rnd * (7800 - imgFood.Width))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Left = Coordinate
Do
Coordinate = Int(Rnd * (6000 - imgFood.Height))
Loop While (Coordinate Mod 100 <> 0)
imgFood.Top = Coordinate
End Sub
Public Sub MoveSnake()
Dim Count, Count2 As Integer
For Count = (Length - 1) To 1 Step (-1)
imgSnake(Count).Left = imgSnake(Count - 1).Left
imgSnake(Count).Top = imgSnake(Count - 1).Top
Next Count
If Direction = 1 Then
imgSnake(0).Left = imgSnake(0).Left - 100
ElseIf Direction = 2 Then
imgSnake(0).Top = imgSnake(0).Top - 100
ElseIf Direction = 3 Then
imgSnake(0).Left = imgSnake(0).Left + 100
ElseIf Direction = 4 Then
imgSnake(0).Top = imgSnake(0).Top + 100
End If
If imgSnake(0).Left = 0 Or imgSnake(0).Left = 7800 Or imgSnake(0).Top = 0 Or imgSnake(0).Top = 6000 Then
If Score < 10000 Then
MsgBox "FAIL! You didn't beat the high score."
Else: MsgBox "You Win!"
End If
Unload Me
End If
For Count2 = 1 To (Length - 1)
If imgSnake(0).Left = imgSnake(Count2).Left And imgSnake(0).Top = imgSnake(Count2).Top Then
Do While Count2 > 4
Unload imgSnake(Count)
Count2 = Count2 - 1
Loop
If Score < 10000 Then
MsgBox "FAIL! You didn't beat the high score."
Else: MsgBox "You Win!"
End If
Unload Me
End If
Next Count2
End Sub
Public Sub FindFood()
If imgSnake(0).Left = imgFood.Left And imgSnake(0).Top = imgFood.Top Then
Load imgSnake(Length)
If Direction = 1 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 2 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Top = imgSnake(Length - 1).Top + 100
imgSnake(Length).Visible = True
ElseIf Direction = 3 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left - 100
imgSnake(Length).Top = imgSnake(Length - 1).Top
imgSnake(Length).Visible = True
ElseIf Direction = 4 Then
imgSnake(Length).Left = imgSnake(Length - 1).Left
imgSnake(Length).Left = imgSnake(Length - 1).Top - 100
imgSnake(Length).Visible = True
End If
Length = Length + 1
Score = Score + 100
If Score = 500 Then
tmrLoop.Interval = 40
ElseIf Score = 1000 Then
tmrLoop.Interval = 30
ElseIf Score = 1500 Then
tmrLoop.Interval = 20
ElseIf Score = 2000 Then
tmrLoop.Interval = 10
ElseIf Score = 2500 Then
tmrLoop.Interval = 5
ElseIf Score = 3000 Then
tmrLoop.Interval = 1
End If
Label1.Caption = "Score = " & Score
Call PlaceFood
End If
End Sub
and how can i upload my project?
-
Jun 7th, 2010, 08:46 PM
#9
Thread Starter
New Member
Re: Visual BAsic 6 Snake Game
the problem is in the move snake function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|