Simple grid based game - TheVBNetwork - Jan 17, 2015
Just spent 10 minuets creating a simple grid based game. Its not the most efficient way of doing it but its in .NET so yeah.
Nothing special but might come in handy for some novice .NET game devs.
[img]http://s15.postimg.org/drsw8mciz/example.png[/img]
Code Imports System.Threading
Public Class Form1
Dim GR As New List(Of PictureBox) 'Grid Refrence Eg.( GR(4) = the 5th block in the grid )
Dim MainNode As Integer = 311 'The starting node number (GR(311))
Enum Directions As Integer 'Possible directions for the main node to go
North = 0
East = 1
South = 2
West = 3
End Enum
Dim CharDirection As Integer = 0 'Current direction that the move thread will use
Dim GameActive As Boolean = True 'Will be used for the move loop to stop the procedure.
Dim GridHeight As Integer = 24 'The number of rows down the grid will contain
Dim GridWidth As Integer = 24 'The number of nodes across the grid will contain
Dim GridColor As Color = Color.Wheat 'The color each grid node will be
Dim GridNodeColor As Color = Color.Red 'The color the active node will be
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Chr(119) Then 'w key is pressed
CharDirection = Directions.North
End If
If e.KeyChar = Chr(100) Then 'd key is pressed
CharDirection = Directions.East
End If
If e.KeyChar = Chr(115) Then 's key is pressed
CharDirection = Directions.South
End If
If e.KeyChar = Chr(97) Then 'a key is pressed
CharDirection = Directions.West
End If
Me.Text = CType(GR(MainNode), PictureBox).Tag
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateGrid() 'Creates all the controls and adds them to the form
CType(GR(MainNode), PictureBox).BackColor = Color.Red 'Sets the main node
Dim t As New Thread(AddressOf MoveChar) 'Creates the new thread for moving the node
t.IsBackground = True 'Makes sure the thread will exit upon request
t.Start() 'Starts the thread
End Sub
Sub MoveChar()
Do While GameActive 'Checks game state
Try
If CharDirection = Directions.North Then 'If moving north
CType(GR(MainNode), PictureBox).BackColor = GridColor 'Change the old main node to a normal grid node
MainNode = MainNode - GridWidth - 1 'Move the main node refrence to the new location
CType(GR(MainNode), PictureBox).BackColor = GridNodeColor 'Sets the new node color
End If
If CharDirection = Directions.East Then
CType(GR(MainNode), PictureBox).BackColor = GridColor
MainNode = MainNode + 1
CType(GR(MainNode), PictureBox).BackColor = GridNodeColor
End If
If CharDirection = Directions.South Then
CType(GR(MainNode), PictureBox).BackColor = GridColor
MainNode = MainNode + GridWidth + 1
CType(GR(MainNode), PictureBox).BackColor = GridNodeColor
End If
If CharDirection = Directions.West Then
CType(GR(MainNode), PictureBox).BackColor = GridColor
MainNode = MainNode - 1
CType(GR(MainNode), PictureBox).BackColor = GridNodeColor
End If
Catch ex As Exception
MsgBox("You died.") 'Bad and quick way to check if the main node exceeds the grid, Should check within the statments but im tired
End
End Try
Thread.Sleep(100)
Loop
End Sub
Sub CreateGrid()
Dim RP As Integer = 0 'Refrence point (used for naming the nodes)
For h As Integer = 0 To GridHeight
For w As Integer = 0 To GridWidth
Dim Gp As New PictureBox
Gp.BackColor = Color.Wheat 'Sets all the nodes to the base color
Gp.BorderStyle = BorderStyle.FixedSingle 'Creates a grid effect
Gp.Size = New Size(8, 8)
Gp.Location = New Point(w * 10, h * 10) 'Location of each node in its releative place
Gp.Name = RP
Gp.Tag = "X:" & w & " Y:" & h
GR.Add(Gp) 'Add the constructed node to the grid refrence
RP += 1 'New node refrence point
Next
Next
For Each CTRL As PictureBox In GR
Me.Controls.Add(CTRL) 'Adds all the nodes to the form
Next
End Sub
End Class
* - JavaProfit - Jan 18, 2015
Nice looking grid. I don't know much .net, so maybe this will persuade me to learn it.
* - SIM-1847363 - Jan 18, 2015
Nice work i did this once for one of my projects, but just like the people who seen that project. i must recommend that you try to make it in gdi.
* - Ninja.exe - Jan 18, 2015
That's one interesting game right their....
|