Hey, i'm pretty new to visual basic, and we just our first programming assinment for school, we have to make some sort of vb game. Using 2005 express edition and i'm atempting to make a multiplayer snake game. I have the movment controls down and now i'm attempting to make the blue circle i'm using for a player leave a trail of blue color, so that if the player touched it the player dies ect. I've tried spawning invisable picture boxes after it, drawing rectanges and lines and, well i've tried a lot of stuff, so after scouring the internet and local help for about 5 hours and fidling and getting no where i'm deciding to ask for help. Any and all help would be GREATLY appriciated.
Public Class Form1
Dim x, y As Integer
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyData
Case Keys.Up
TimerDown.Stop()
TimerLeft.Stop()
TimerRight.Stop()
TimerUp.Start()
Case Keys.Down
TimerLeft.Stop()
TimerRight.Stop()
TimerUp.Stop()
TimerDown.Start()
Case Keys.Left
TimerRight.Stop()
TimerUp.Stop()
TimerDown.Stop()
TimerLeft.Start()
Case Keys.Right
TimerUp.Stop()
TimerDown.Stop()
TimerLeft.Stop()
TimerRight.Start()
Case Keys.Space
TimerUp.Stop()
TimerDown.Stop()
TimerLeft.Stop()
TimerRight.Stop()
End Select
End Sub
Private Sub Timerup_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerUp.Tick
y += -1
PictureBox1.Top = y
createtrail()
End Sub
Private Sub Timerdown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDown.Tick
y += 1
PictureBox1.Top = y
createtrail()
End Sub
Private Sub TimerLeft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerLeft.Tick
x += -1
PictureBox1.Left = x
createtrail()
End Sub
Private Sub TimerRight_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRight.Tick
x += 1
PictureBox1.Left = x
createtrail()
End Sub
Sub createtrail()
Dim trail As New PictureBox
Dim part As Integer
part += 1
With trail
.Name = .Name & part
.Height = 23
.Width = 22
.Image = My.Resources.Blue_Player
.Left = PictureBox1.Left
.Top = PictureBox1.Top
End With
me.controls.add(trail)
End Sub
End Class
I've searched a lot of online code and tutorials but a lot of the code is umfamiliar and i'm having trouble making sense of it all...
Last edited by BrighTide; Feb 12th, 2010 at 04:26 AM.
Sorry, i looked at the code sample, and a lot of the code, i have no idea what it means or more importantly how to implement it, could you look at my code and tell me what i'm doing wrong, or is it more compicated than that?
What you are trying to do, is quite a simple task, but some of the concepts will appear quite strange to you, if you are used to game programming.
The sample that Lord Orwell put in the code bank is a good one, and from what I can remember, is well documented. If I were you, I would spend some time looking at the code, and stepping into it in the debugger to see what is happening, things will start to become clear.
Sorry, i tried, really, but, i've tried using his code and it didn't really work, would his code being in 2008, and me having 2005 have anything to do with that?
Ummm, soz, i can'y really do big downloads atm, the .zip file with project in it, the unzip went fine, but the project wouldn;t open at all yeah. If it helps the game i;m trying to make is more like Tron than snake.
We'll need a form, a picturebox, called picDisp and a button called btnStart (see pic):
First of all we'll need a game field and its contents:
Code:
Public Enum SquareContents
Empty = 0
Food = 1
Obstacle = 2
Body = 3
End Enum
Public Const FIELD_WIDTH As Integer = 30
Public Const FIELD_HEIGHT As Integer = 30
Public Shared GameField(0 To FIELD_WIDTH - 1, 0 To FIELD_HEIGHT - 1) As SquareContents
Public Enum Directions
Left
Right
Up
Down
End Enum
Now, let's create our snake:
vb.net Code:
Public Class Snake
' His body is simply a queue of points
' A queue works by the FIFO principle - First In - First Out
Public BodyCells As New Queue(Of Point)
' The direction of his movement
Public Direction As Directions
' The location of his head
Public HeadLocation As Point
' We'll need two events
Public Event PainfulDeath() ' Game over
Public Event YumYum() ' Snake eats
Public Sub New()
HeadLocation = New Point(FIELD_WIDTH \ 2, FIELD_HEIGHT \ 2) ' We place him in the center
BodyCells.Enqueue(HeadLocation) ' We add his only cell to his body
Direction = Directions.Right ' ... and make him look to the right
End Sub
' Now, what will our snake do with each heatbeat?
Public Sub Tick()
Dim NextCell As Point = HeadLocation ' Where is the next cell
Select Case Direction
Case Directions.Up
NextCell.Y -= 1
If NextCell.Y < 0 Then
RaiseEvent PainfulDeath() ' ... kill the poor beast.
Exit Sub
End If
Case Directions.Down
NextCell.Y += 1
If NextCell.Y = FIELD_HEIGHT Then
RaiseEvent PainfulDeath()
Exit Sub
End If
Case Directions.Left
NextCell.X -= 1
If NextCell.X < 0 Then
RaiseEvent PainfulDeath()
Exit Sub
End If
Case Directions.Right
NextCell.X += 1
If NextCell.X = FIELD_WIDTH Then
RaiseEvent PainfulDeath()
Exit Sub
End If
End Select
' What do we have in this cell
Select Case GameField(NextCell.X, NextCell.Y)
Case SquareContents.Body, SquareContents.Obstacle ' bumped into an obstacle
RaiseEvent PainfulDeath()
Exit Sub
Case SquareContents.Food ' some food here
BodyCells.Enqueue(NextCell) ' add another cell to body
Oh my goodness thank you all so much, i finally got it, man, its go frustrating pressing that little green triangle like 6 Kazillion times and what your sure was flawless code not working. YOUR ALL AWESOME! =D
But that is part of understanding code. Debugging is an essential tool that you are going to need to become familiar with. You will become more proficient at it as you gain experience.