alibi jackson
Member
- Joined
- Jan 17, 2006
- Messages
- 6
- Programming Experience
- Beginner
Hi
I'm new to VB.Net and have been trying some low-level coding. I'd like
to improve my knowlege and skill and am just about to attempt to code a
hide and seek type game for my daughter. Might anyone be able to spend
a few minutes reading thru some code I downloaded and describe the
function of each line so that I might understand the principle a little
better. I have VB books but am struggling to decipher them in relation
to this. Any help would be much appreciated.
Thanks in advance
Andy
I'm new to VB.Net and have been trying some low-level coding. I'd like
to improve my knowlege and skill and am just about to attempt to code a
hide and seek type game for my daughter. Might anyone be able to spend
a few minutes reading thru some code I downloaded and describe the
function of each line so that I might understand the principle a little
better. I have VB books but am struggling to decipher them in relation
to this. Any help would be much appreciated.
Thanks in advance
Andy
VB.NET:
Public Class Game
Inherits System.Windows.Forms.Form
Dim GameOver As Boolean
Dim P(4, 2) As Integer, Map(9, 9) As Integer
Dim Tries As Integer
Dim Remaining As Integer
Dim MyMap As Graphics
Dim GridBorder As Integer
Dim GridDelta As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyMap = pnlGrid.CreateGraphics
Randomize()
'find grid dimensions
GridBorder = 10
GridDelta = CInt((pnlGrid.ClientSize.Width - 2 * GridBorder) / 10)
lblMessage.Text = "Find the four Ninjas hidden on a 10 x 10 grid. After each guess, you are told how far you are from each Ninja."
lblMessage.Text = lblMessage.Text + ControlChars.CrLf +
ControlChars.CrLf + ">Click Play To Start<" 'label text
btnPlay.Focus() 'display game start text
End Sub
Private Sub pnlGrid_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlGrid.Paint
Dim I As Integer, J As Integer
For I = 0 To 10 'draw grid
MyMap.DrawLine(Pens.White, GridBorder, GridBorder + I *
GridDelta, GridBorder + 10 * GridDelta, GridBorder + I * GridDelta)
MyMap.DrawLine(Pens.White, GridBorder + I * GridDelta,
GridBorder, GridBorder + I * GridDelta, GridBorder + 10 * GridDelta)
Next I
'draw guesses and ninjas
For I = 0 To 9
For J = 0 To 9
If Map(I, J) = 1 Then
MyMap.FillRectangle(Brushes.White, GridBorder + I *
GridDelta + 1, GridBorder + (9 - J) * GridDelta + 1, GridDelta - 1, GridDelta - 1)
ElseIf Map(I, J) = -1 Then
MyMap.DrawImage(picninja.Image, GridBorder + I * GridDelta, GridBorder + (9 - J) * GridDelta, GridDelta, GridDelta)
End If
Next J
Next I
End Sub
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
Dim I As Integer, J As Integer
'Start new game
GameOver = False
lblMessage.Text = ""
btnPlay.Enabled = False
Remaining = 4
Tries = 0
lblNinjas.Text = "4"
lblGuesses.Text = "0"
MyMap.Clear(pnlGrid.BackColor)
For I = 0 To 9
For J = 0 To 9
Map(I, J) = 0
Next J
Next I
For I = 1 To 4
Do
For J = 1 To 2
P(I, J) = Int(10 * Rnd())
Next J
Loop Until Map(P(I, 1), P(I, 2)) = 0
Map(P(I, 1), P(I, 2)) = 1
Next I
For I = 1 To 4
Map(P(I, 1), P(I, 2)) = 0
Next I
pnlGrid_Paint(Nothing, Nothing)
lblMessage.Text = "Four Ninjas are now in hiding." +
ControlChars.CrLf + "Click where you think they are."
lblMessage.Refresh()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Dim I As Integer
'Either exit or stop current game
If btnStop.Text = "Exit" Then
Me.Close()
Else
btnPlay.Enabled = True
If Not (GameOver) Then lblMessage.Text = "Game Stopped"
btnPlay.Focus()
End If
End Sub
Private Sub pnlGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlGrid.MouseDown
Dim I As Integer
Dim M As Integer, N As Integer
Dim D As Single
Dim Found As Boolean
If btnStop.Text = "Exit" Then Exit Sub
If e.X < GridBorder Or e.X > GridBorder + 10 * GridDelta Then Exit Sub
If e.Y < GridBorder Or e.Y > GridBorder + 10 * GridDelta Then Exit Sub
'Find column
For M = 0 To 9
If e.X >= GridBorder + M * GridDelta And e.X <= GridBorder + (M + 1) * GridDelta Then Exit For
Next M
'Find row
For N = 9 To 0 Step -1
If e.Y >= GridBorder + (9 - N) * GridDelta And e.Y <= GridBorder + (10 - N) * GridDelta Then Exit For
Next N
If Map(M, N) <> 0 Then Exit Sub
Map(M, N) = 1
Tries = Tries + 1
lblGuesses.Text = Format(Tries, "0")
lblGuesses.Refresh()
lblMessage.Text = ""
Found = False
For I = 1 To 4
If P(I, 1) <> -1 Then
If P(I, 1) <> M Or P(I, 2) <> N Then
D = Math.Sqrt((P(I, 1) - M) ^ 2 + (P(I, 2) - N) ^ 2)
lblMessage.Text = lblMessage.Text + "You are " +
Format(D, "0.0") + " units from Ninja" + Str(I) + ControlChars.CrLf
Else
Found = True
P(I, 1) = -1
Map(M, N) = -1
lblMessage.Text = lblMessage.Text + "You found Ninja" + Str(I) + ControlChars.CrLf
Remaining = Remaining - 1
lblNinjas.Text = Format(Remaining, "0")
lblNinjas.Refresh()
End If
End If
Next I
If Found Then
'draw ninja in column M, row N - use picture
MyMap.DrawImage(picninja.Image, GridBorder + M * GridDelta, GridBorder + (9 - N) * GridDelta, GridDelta, GridDelta)
Else
MyMap.FillRectangle(Brushes.White, GridBorder + M * GridDelta + 1, GridBorder + (9 - N) * GridDelta + 1, GridDelta - 1, GridDelta - 1)
End If
If Remaining = 0 Then
GameOver = True
lblMessage.Text = lblMessage.Text + ControlChars.CrLf + "You found all 4 Ninjas!" + ControlChars.CrLf + "It took you" + Str(Tries) + " guesses."
btnStop.PerformClick()
End If
End Sub
End Class
Last edited by a moderator: