Hello,
I'm writing a game in VB 2005 (VB.NET) and I don't know what I'm doing!
Is that blunt enough?
I've written the code below which Opens the New Game called "Knights of Honor", displays a SplashScreen, then starts a New Game with the Game Board, and as you move the Mouse over the territories they are highlighted and an Info Box appears to give you information about that territory. It works very fast, but has a flicker. I can't supply all the files as they amount to more than 10 Megs (lots of image files). Maybe someone can checkout the code and after telling me what an idiot I am, maybe give me a suggestion or 2. Thank you for listening.
[code]Public Class KnightsOfHonor
#Region "Load Images"
Dim VirtualBoard As New Bitmap("GameBoard.jpg") 'Our Game Board
Dim saveBoard As New Bitmap("GameBoard.jpg") 'Copy of Game Board for restoring
Dim TerrBoard As New Bitmap("Terrs.bmp") 'Colored Territories for finding with Mouse!
Dim TerrInfoBox As New Bitmap("TInfo.gif") 'An Info Box used during Game play
Dim DrawGraphics As Graphics = Graphics.FromImage(VirtualBoard) 'Our new Graphics Object
' An image file for every territory on the Board.
Dim Terrs() = {Image.FromFile("Terr1.gif"), Image.FromFile("Terr2.gif"), _
Image.FromFile("Terr3.gif"), Image.FromFile("Terr4.gif"), Image.FromFile("Terr5.gif"), _
Image.FromFile("Terr6.gif"), Image.FromFile("Terr7.gif"), Image.FromFile("Terr8.gif"), _
Image.FromFile("Terr9.gif"), Image.FromFile("Terr10.gif"), Image.FromFile("Terr11.gif"), _
Image.FromFile("Terr12.gif"), Image.FromFile("Terr13.gif"), Image.FromFile("Terr14.gif"), _
Image.FromFile("Terr15.gif"), Image.FromFile("Terr16.gif"), Image.FromFile("Terr17.gif"), _
Image.FromFile("Terr18.gif"), Image.FromFile("Terr19.gif"), Image.FromFile("Terr20.gif"), _
Image.FromFile("Terr21.gif"), Image.FromFile("Terr22.gif"), Image.FromFile("Terr23.gif"), _
Image.FromFile("Terr24.gif"), Image.FromFile("Terr25.gif"), Image.FromFile("Terr26.gif"), _
Image.FromFile("Terr27.gif"), Image.FromFile("Terr28.gif"), Image.FromFile("Terr29.gif"), _
Image.FromFile("Terr30.gif")}
' An image file for every territory highlighted.
Dim TerrsHL() = {Image.FromFile("Terr1hl.gif"), Image.FromFile("Terr2hl.gif"), _
Image.FromFile("Terr3hl.gif"), Image.FromFile("Terr4hl.gif"), Image.FromFile("Terr5hl.gif"), _
Image.FromFile("Terr6hl.gif"), Image.FromFile("Terr7hl.gif"), Image.FromFile("Terr8hl.gif"), _
Image.FromFile("Terr9hl.gif"), Image.FromFile("Terr10hl.gif"), Image.FromFile("Terr11hl.gif"), _
Image.FromFile("Terr12hl.gif"), Image.FromFile("Terr13hl.gif"), Image.FromFile("Terr14hl.gif"), _
Image.FromFile("Terr15hl.gif"), Image.FromFile("Terr16hl.gif"), Image.FromFile("Terr17hl.gif"), _
Image.FromFile("Terr18hl.gif"), Image.FromFile("Terr19hl.gif"), Image.FromFile("Terr20hl.gif"), _
Image.FromFile("Terr21hl.gif"), Image.FromFile("Terr22hl.gif"), Image.FromFile("Terr23hl.gif"), _
Image.FromFile("Terr24hl.gif"), Image.FromFile("Terr25hl.gif"), Image.FromFile("Terr26hl.gif"), _
Image.FromFile("Terr27hl.gif"), Image.FromFile("Terr28hl.gif"), Image.FromFile("Terr29hl.gif"), _
Image.FromFile("Terr30hl.gif")}
#End Region
#Region "Territory Data"
' X and Y co-ords for placing each territory
Dim Terrxy() = _
{11, 11, 247, 11, 543, 11, 718, 11, 843, 30, 11, 87, 164, 46, 351, 85, 491, 111, 617, 136, _
660, 66, 745, 171, 11, 203, 127, 154, 110, 262, 416, 236, 613, 270, 857, 259, 11, 380, 171, 367, _
323, 337, 525, 420, 733, 382, 11, 525, 148, 512, 106, 616, 282, 492, 371, 605, 524, 575, 779, 560}
' The pixel size of each territory image.
Dim Terrsize() = _
{268, 129, 311, 104, 179, 123, 271, 115, 162, 157, 207, 126, 256, 145, 182, 211, 177, 151, 157, 165, _
199, 158, 260, 131, 115, 204, 318, 157, 318, 132, 301, 159, 291, 209, 148, 272, 202, 188, 278, 155, _
306, 167, 224, 209, 272, 217, 149, 181, 191, 132, 303, 90, 280, 152, 260, 101, 254, 131, 226, 146}
' TerrArmy - Soldiers, Archers, Knights, Catapults - for all 30 Terrs.
Dim TerrArmy(120) As Integer
' TerrPop - Locals, MaxPop - for all 30 Terrs.
Dim TerrPop() = _
{645, 2581, 129, 518, 153, 612, 213, 854, 247, 988, 802, 3211, 1002, 4008, 939, 3759, 703, 2815, 633, 2535, _
358, 1434, 779, 3118, 852, 3409, 995, 3981, 800, 3201, 664, 2658, 508, 2033, 462, 1851, 514, 2057, 577, 2308, _
1263, 5052, 926, 3705, 602, 2409, 507, 2029, 627, 2508, 579, 2317, 1030, 4123, 979, 3917, 877, 3508, 1051, 4207}
' TerrData - Owner, Worth, DefV, Castle, PassingTh - for all 30 Terrs.
Dim TerrData(150)
#End Region
#Region "Player Data"
Dim PlName() = {"", "Unknown Knight", "Roger the Red", "Black Baer", "Lord Hawke", "Tall Tom", _
"Lord Reever", "Eric the Bold", "Sir William", "Mark de Castille", "Rogue Richard", _
"Piter NewGlen", "Allain the Brave"}
' Players - Strength, Wisdom, Courage, Honor, Value, Gold, Joust (84)
Dim PlData(84)
#End Region
#Region "Program Variables"
Dim aBrush As New Drawing2D.LinearGradientBrush(ClientRectangle, _
Color.Red, Color.Yellow, Drawing2D.LinearGradientMode.Horizontal)
Dim UnderColor As Color = New Color
Dim Terrnum As Integer = 0
Dim saveTerrnum As Integer = 0
Dim InfoBoxX, InfoBoxY As Integer
Dim Borders As Boolean = False
Dim TerrHL As Boolean = False
Dim TerrInfo As String = ""
Dim TerrOwner As String = ""
' GameStatus - 0 = No Game in progress, 1 = Game in Progress, 2 = New Game Query
Dim GameStatus As Byte = 255
Dim saveGStatus As Byte = 255
#End Region
Private Sub KnightsOfHonor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyForms.KnightsOfHonor = Me
VirtualBoard = Image.FromFile("SplashScreen.jpg")
PBox1_GameBoard.Image = VirtualBoard
End Sub
Private Sub PBox1_GameBoard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PBox1_GameBoard.MouseMove
If GameStatus <> 1 Then Exit Sub ' No Game in progress then Exit
' Find the color under the Mouse Pointer in relation to the Terrs.bmp
'The Red portion of this color is the Territory number 1 to 30
' e.X, e.Y = Mouse coords X and Y
UnderColor = TerrBoard.GetPixel(e.X, e.Y)
Terrnum = GetColor(UnderColor)
If Terrnum = saveTerrnum Then Exit Sub ' Still hovering over same Territory
If ((Terrnum = 0) Or (Terrnum = 255)) Then ' A Border or Water region
TerrInfo = ""
Label1.Text = "No Territory " & Terrnum
If TerrHL Then
PBox1_GameBoard.Image = saveBoard ' Restore the Game Board
saveTerrnum = Terrnum
TerrHL = False
End If
Exit Sub
Else
If TerrData(Terrnum * 5 - 5) = 0 Then
TerrOwner = "Open Territory"
Else : TerrOwner = PlName(TerrData(Terrnum * 5 - 5))
End If
TerrInfo = " Territory no. " & Terrnum & vbNewLine & TerrOwner & _
vbNewLine & TerrArmy(Terrnum * 4 - 4) & " - Sodiers " & _
vbNewLine & TerrArmy(Terrnum * 4 - 3) & " - Archers " & _
vbNewLine & TerrArmy(Terrnum * 4 - 2) & " - Knights " & _
vbNewLine & TerrArmy(Terrnum * 4 - 1) & " - Catapults" & _
vbNewLine & " " & TerrPop(Terrnum * 2 - 1) & " Locals"
Label1.Text = "Territory no. " & Terrnum
End If
If TerrHL Then
PBox1_GameBoard.Image = saveBoard ' Restore the Game Board
saveBoard = DirectCast(PBox1_GameBoard.Image.Clone, Bitmap) 'Save the Game Board
TerrHL = False
End If
If ((Terrnum > 0) And (Terrnum < 31)) Then
saveBoard = DirectCast(PBox1_GameBoard.Image.Clone, Bitmap) 'Save the Game Board
' I don't know why I need this line except that the DrawGraphics lines below
' will not work without it.
Dim DrawGraphics As Graphics = PBox1_GameBoard.CreateGraphics
'Highlight a territory by drawing over it.
DrawGraphics.DrawImage(TerrsHL(Terrnum - 1), Terrxy(Terrnum * 2 - 2), _
Terrxy(Terrnum * 2 - 1), Terrsize(Terrnum * 2 - 2), Terrsize(Terrnum * 2 - 1))
saveTerrnum = Terrnum ' save the number of the terr. that was just highlighted.
TerrHL = True
'Draw the Info Box for the user.
DrawGraphics.DrawImage(TerrInfoBox, InfoBoxX, InfoBoxY, TerrInfoBox.Width, TerrInfoBox.Height)
Dim aFont As New System.Drawing.Font("Arial", 9, FontStyle.Regular)
If GameStatus = 1 Then
DrawGraphics.DrawString(TerrInfo, aFont, Brushes.Black, InfoBoxX + 12, InfoBoxY + 9)
End If
End If
End Sub
Private Sub PBox1_GameBoard_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PBox1_GameBoard.MouseLeave
If GameStatus <> 1 Then Exit Sub ' Not a Game then Exit
Label1.Text = "Off Game Board"
If TerrHL Then ' If we've left a territory highlighted then restore the Game Board.
PBox1_GameBoard.Image = saveBoard
VirtualBoard = saveBoard
End If
End Sub
Private Sub Initialize_Game_Variables()
Dim a As Integer
' TerrArmy - Soldiers, Archers, Knights, Catapults - for all 30 Terrs.
For a = 0 To 119
TerrArmy(a) = 0
Next
' TerrData - Worth, DefV, Castle, Owner, PassingTh
For a = 0 To 149
TerrData(a) = 0
Next
' Players - Strength, Wisdom, Courage, Honor, Value, Gold, Joust (84)
For a = 0 To 83
PlData(a) = 0
Next
InfoBoxX = (PBox1_GameBoard.Width / 2) - (TerrInfoBox.Width / 2)
InfoBoxY = (PBox1_GameBoard.Height / 2) - (TerrInfoBox.Height / 2) + 4
Terrnum = 0
GameStatus = 0
End Sub
Public Function GetColor(ByVal colorObj As System.Drawing.Color) As String
Return Int(colorObj.R) ' We only need the Red portion
End Function
Private Sub EXITToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EXITToolStripMenuItem2.Click
Application.Exit()
End Sub
Private Sub NewGameToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem2.Click
PBox1_GameBoard.Image = New Bitmap("GameBoard.jpg")
saveBoard = DirectCast(PBox1_GameBoard.Image.Clone, Bitmap)
Initialize_Game_Variables()
GameStatus = 1
End Sub
End Class
Public Class MyForms
Public Shared KnightsOfHonor As Form
Public Shared SplashScreen As Form
Public Shared Game_Options As Form
End Class[/code]