Computer Science Canada

Niffty Snake game(work in progress)istillneedtomakesnakegrow

Author:  MihaiG [ Tue Nov 16, 2004 3:47 pm ]
Post subject:  Niffty Snake game(work in progress)istillneedtomakesnakegrow

the snake wont grow....you can increase speed by adjusting timer Wink

heres the code long....
multi Razz multi
Const MaxSnakeLength As Integer = 100
Dim Snake(MaxSnakeLength) As Integer
Const SquareHeight As Integer = 240
Const SquareWidth As Integer = 240
Const BannerHeight As Integer = 480
Dim Direction As Integer
Const DirLeft As Integer = 1
Const DirRight As Integer = 2
Const DirUp As Integer = 3
Const DirDown As Integer = 4
Dim TimeCounter As Integer
Dim Score As Integer
Const Bonus As Integer = 100
Const TimeHidden As Integer = 70
Const TimeShown As Integer = TimeHidden + 50
Const Topinc = 240
Const Leftinc = 240

Private Sub CheckCrash()
Dim CrashedIntoItself As Boolean
CrashedIntoItself = False

' Now checking if snake crashed into itself
' By verifying if the head position equals the position of any other body segment
For i = 2 To Shape1.Count Step 1
If Shape1(Snake(1)).Left = Shape1(Snake(i)).Left And Shape1(Snake(1)).Top = Shape1(Snake(i)).Top Then
If Shape1(Snake(i)).Visible Then
CrashedIntoItself = True
End If
End If
Next i

If Shape1(Snake(1)).Top < 0 Or _
Shape1(Snake(1)).Left < 0 Or _
Shape1(Snake(1)).Left + Shape1(Snake(1)).Width > FormSnake.Width Or _
Shape1(Snake(1)).Top + Shape1(Snake(1)).Height > Line1.Y1 Or _
CrashedIntoItself Then
Timer1.Enabled = False
FormCrash.Show
End If
End Sub

Private Sub CheckGoodie()
If Shape1(Snake(1)).Left = Shape2.Left And Shape1(Snake(1)).Top = Shape2.Top And Shape2.Visible Then
Shape2.Visible = False
TimeCounter = 0
Score = Score + Bonus
End If
End Sub


Private Sub MoveDown()
' Move tail (last square) into new position
' Tail is always at Snake(Shape1.Count)
' Head is always at Snake(1)
Shape1(Snake(Shape1.Count)).Left = Shape1(Snake(1)).Left
Shape1(Snake(Shape1.Count)).Top = Shape1(Snake(1)).Top + SquareHeight

' Now the tail becomes the new head
tmp = Snake(Shape1.Count)
For i = Shape1.Count To 2 Step -1
Snake(i) = Snake(i - 1)
Next i
Snake(1) = tmp
CheckCrash
CheckGoodie
End Sub


Private Sub MoveLeft()
' Move tail (last square) into new position
' Tail is always at Snake(Shape1.Count)
' Head is always at Snake(1)
Shape1(Snake(Shape1.Count)).Top = Shape1(Snake(1)).Top
Shape1(Snake(Shape1.Count)).Left = Shape1(Snake(1)).Left - SquareWidth

' Now the tail becomes the new head
tmp = Snake(Shape1.Count)
For i = Shape1.Count To 2 Step -1
Snake(i) = Snake(i - 1)
Next i
Snake(1) = tmp
CheckCrash
CheckGoodie
End Sub


Private Sub MoveRight()
' Move tail (last square) into new position
' Tail is always at Snake(Shape1.Count)
' Head is always at Snake(1)
Shape1(Snake(Shape1.Count)).Top = Shape1(Snake(1)).Top
Shape1(Snake(Shape1.Count)).Left = Shape1(Snake(1)).Left + SquareWidth

' Now the tail becomes the new head
tmp = Snake(Shape1.Count)
For i = Shape1.Count To 2 Step -1
Snake(i) = Snake(i - 1)
Next i
Snake(1) = tmp
CheckCrash
CheckGoodie
End Sub


Private Sub MoveUp()
' Move tail (last square) into new position
' Tail is always at Snake(Shape1.Count)
' Head is always at Snake(1)
Shape1(Snake(Shape1.Count)).Left = Shape1(Snake(1)).Left
Shape1(Snake(Shape1.Count)).Top = Shape1(Snake(1)).Top - SquareHeight

' Now the tail becomes the new head
tmp = Snake(Shape1.Count)
For i = Shape1.Count To 2 Step -1
Snake(i) = Snake(i - 1)
Next i
Snake(1) = tmp
CheckCrash
CheckGoodie
End Sub


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 38 Then
Direction = DirUp
ElseIf KeyCode = 40 Then
Direction = DirDown
ElseIf KeyCode = 37 Then
Direction = DirLeft
ElseIf KeyCode = 39 Then
Direction = DirRight
ElseIf KeyCode = 27 Then
End
ElseIf KeyCode = 80 Then
Timer1.Enabled = Not Timer1.Enabled
End If
End Sub

Private Sub Form_Load()
For i = 1 To Shape1.Count
Snake(i) = Shape1.Count - i
Next i
Direction = DirRight
TimeCounter = 0
Score = 1
End Sub

Private Sub Timer1_Timer()
If Direction = DirLeft Then
MoveLeft
ElseIf Direction = DirRight Then
MoveRight
ElseIf Direction = DirUp Then
MoveUp
ElseIf Direction = DirDown Then
MoveDown
End If

TimeCounter = TimeCounter + 1
If Not Shape2.Visible And TimeCounter = TimeHidden Then
Shape2.Left = RndX()
Shape2.Top = RndY()
Shape2.Visible = True
ElseIf Shape2.Visible And TimeCounter = TimeShown Then
Shape2.Visible = False
TimeCounter = 0
End If

Score = Score + 1
Label1.Caption = Score
End Sub

Private Function RndX()
RndX = (Int(33 * Rnd() + 1) - 1) * 240
End Function

Private Function RndY()
RndY = (Int(24 * Rnd() + 1) - 1) * 240
End Function

Author:  MihaiG [ Tue Nov 16, 2004 3:51 pm ]
Post subject:  part 2

oh by the way you need to make the shapes....(figure it out)i need to make you do some of the work Laughing p.s. there are two forms... (snkae form, and crash form Wink good luck Sniper Scatter Blowing up

Author:  Martin [ Wed Jan 05, 2005 1:46 am ]
Post subject: 

Please, use code tags.


: