Basically the game we're going to make is about a miner who we'll represent as an "A" character. He's a simple little dude, his job is to mine the whole mining zone to find as much Gold, Silver, and Copper as he can. Sounds simple right? Well it's not, because there are hidden dangers ahead.
We'll begin with my game engine. It's called Engine(simple enough), and it encapsulates the player in a class.
Public Class Engine
Public GameOver = False
#Region "Player Info/Class"
Public Class Player
'Player Location Variables
Private Player_X As Integer = 2
Private Player_Y As Integer = 2
'Player Exhaustion Boolean
Public Nottired As Boolean = True
'New Initialization of the Player
Public Sub New(ByVal PlayerName As String)
PlayerN = PlayerName 'Sets the Player Name
End Sub
Public Property PlayerX As Integer 'Returns and sets the player X location
Get
Return Player_X
End Get
Set(ByVal value As Integer)
Player_X = value
End Set
End Property
Public Property PlayerY As Integer 'Returns and sets the player Y location
Get
Return Player_Y
End Get
Set(ByVal value As Integer)
Player_Y = value
End Set
End Property
Private _PlayerName As String 'Player name Variable
Public Property PlayerN As String 'Returns and sets the player name
Get
Return _PlayerName
End Get
Set(ByVal value As String)
_PlayerName = value
End Set
End Property
Private Live As Integer = 5 'Player Lives variable
Public Property Lives As Integer 'Returns and sets the player lives
Get
Return Live
End Get
Set(ByVal value As Integer)
Live = value
End Set
End Property
Private Scores As Integer = 0 'Player Score
Public Property Score As Integer 'Returns and sets the player score
Get
Return Scores
End Get
Set(ByVal value As Integer)
Scores = value
End Set
End Property
End Class
#End Region
Here we dive into the player class, organizing properties, and initialization to be turned into an object that we will use to keep track of our player.
#Region "Map Info/Creation"
Public MaxX As Integer = 74 'Map Max Width/X
Public MaxY As Integer = 19 'Map Max Height/Y
Private Map As New List(Of String) 'Map Canvas
Public Player1 As Player 'Player
Public Sub New(ByVal PlayerName As String)
Player1 = New Player(PlayerName) 'Initiatlizing the Game Engine, and Player
End Sub
Public Sub DrawGame() 'Drawing Graphics
If Map.Count >= 1 Then 'Check to see if a map exists
Map.Clear() 'Clear the current map for redrawing
End If
Dim MaxxCount As Integer = 0
'Designing Map
For g = 0 To MaxY
Dim NewLayer As String = String.Empty 'New Layer for the map
MaxxCount = 0
For i = 0 To MaxX
If Player1.PlayerY = g And Player1.PlayerX = i Then
NewLayer &= "A" 'drawing the player
Else
If i = 0 Or g = 0 Or i = MaxX Or g = MaxY Then
NewLayer &= "-" 'drawing the border
Else
If MaxxCount <= MaxX Then
NewLayer &= "x" 'drawing the dig sites
End If
End If
End If
MaxxCount += 1
Next
Map.Add(NewLayer)
Next
'Draw Player Information and Stats'
Console.WriteLine("Player: " & Player1.PlayerN & " Move: W A S D")
Console.WriteLine("Lives: " & Player1.Lives & " Dig: G")
Console.WriteLine("Score: " & Player1.Score)
'Console.WriteLine("Coords: " & Player1.PlayerX.ToString & ", " & Player1.PlayerY.ToString)
Console.WriteLine("Alerts: " & GameMessage)
'Draw Map'
For Each line As String In Map
Console.WriteLine(line)
Next
End Sub
#End Region
Here we go into the creation of the map, using the same concept as seen in the snippet, but creating our own map out of it.
#Region "Game Events"
Public GameMessage As String = String.Empty
Public Const Gold As Integer = 3
Public Const Silver As Integer = 15
Public Const Copper As Integer = 19
Public Const ExplosiveGasPocket As Integer = 50
Public Const FoundMedKit As Integer = 27
Public Sub DidIHitGold()
Dim Random As Random = New Random()
Dim RandomInt As Integer = Random.Next(0, 51)
Select Case RandomInt
Case Gold
Player1.Score += 100
GameMessage = "Found Gold!"
Case Silver
Player1.Score += 25
GameMessage = "Found Silver!"
Case Copper
Player1.Score += 5
GameMessage = "Found Copper!"
Case ExplosiveGasPocket
Player1.Lives -= 1
GameMessage = "Ouch!"
Case FoundMedKit
Player1.Lives += 1
GameMessage = "Found a Medkit!"
End Select
If Player1.Lives = 0 Then
Console.WriteLine("Game Over! Try Again by Pressing Escape. ")
GameOver = True
End If
If Player1.Score >= 1000 Then
Console.WriteLine("You Win! Thanks for playing, press escape to restart.")
GameOver = True
End If
End Sub
#End Region
End Class
Very simple here. When the DidIhitgold sub routine is run, it randomly determines the players event and then checks for conditions to be met. I know it's not a good life for this little dude, he really has no control, and neither does the player. It's really a game of chance.
Let's get to using this engine now.
Imports System.Threading.Thread
We're going to need the threading to manage the key press events in a separate thread so as not to slow the game engine down at all.
Module ConsoleGame
Private Game As Engine 'Declare the variable to contain the object, but do not initialize allowing us to access it easily when the time comes.
Sub Main()
'Here we establish information to be displayed via title, and written at the menu.
Console.WriteLine("Welcome to my simple game.")
Console.WriteLine("")
Console.WriteLine("Please Enter Your Name to Begin!")
Dim ControlsThread As New Threading.Thread(AddressOf LookForKeyPress) 'Using a separate thread to intercept key presses.
ControlsThread.Start()
Dim Player As String = Console.ReadLine() 'Getting the player name
Game = New Engine(Player) 'Initializing a new instance of the Engine class
Console.Clear() 'Clearing the current text of the console
Game.DrawGame() 'Calling the creategameboard sub routine under the engine
Console.Title = "Game v1.6 - Player: " & Game.Player1.PlayerN
End Sub
Let's set up the game, first we initialize the game, call it's draw routine. It should appear to be a neat grid. Drawing is important, because like all games the graphics are drawn over and over and over to simulate animations, and such. We're doing no different here, we call the draw routine to make its rounds determine where the player should be, and the rest of the make shift console gui we've created.
Private Sub LookForKeyPress() 'Here we handle the key presses, very simple. The character moves, we adjust his position, clear the console and redraw the game to correct his physical position
While True 'We begin our loop to continuously check for key presses on another thread
Dim Ck As ConsoleKeyInfo = Console.ReadKey(True) 'We read the key, but we avoid letting the key pass to the console input.
Select Case Ck.Key
Case ConsoleKey.W
If Game.GameOver = False Then
If Game.Player1.PlayerY + 1 > 1 And Game.Player1.PlayerY + 1 < Game.MaxY And Game.GameOver = False Then
Game.Player1.PlayerY -= 1
ElseIf Game.Player1.PlayerY + 1 < 0 And Game.GameOver = False Then
Game.Player1.PlayerY -= 1
ElseIf Game.Player1.PlayerY + 1 > Game.MaxY And Game.GameOver = False Then
Game.Player1.PlayerY -= 1
ElseIf Game.GameOver = False Then
Game.Player1.PlayerY += 1
End If
Console.Clear()
Game.DrawGame()
Game.Player1.Nottired = True
End If
Case ConsoleKey.S
If Game.GameOver = False Then
If Game.Player1.PlayerY + 1 > 1 And Game.Player1.PlayerY + 1 < Game.MaxY And Game.GameOver = False Then
Game.Player1.PlayerY += 1
ElseIf Game.Player1.PlayerY + 1 < 0 And Game.GameOver = False Then
Game.Player1.PlayerY += 1
ElseIf Game.Player1.PlayerY + 1 > Game.MaxY And Game.GameOver = False Then
Game.Player1.PlayerY += 1
Else
Game.Player1.PlayerY -= 1
End If
Console.Clear()
Game.DrawGame()
Game.Player1.Nottired = True
End If
Case ConsoleKey.A
If Game.GameOver = False Then
If Game.Player1.PlayerX + 1 > 1 And Game.Player1.PlayerX + 1 < Game.MaxX And Game.GameOver = False Then
Game.Player1.PlayerX -= 1
ElseIf Game.Player1.PlayerX + 1 < 0 And Game.GameOver = False Then
Game.Player1.PlayerX -= 1
ElseIf Game.Player1.PlayerX + 1 > Game.MaxX And Game.GameOver = False Then
Game.Player1.PlayerX -= 1
Else
Game.Player1.PlayerX += 1
End If
Console.Clear()
Game.DrawGame()
Game.Player1.Nottired = True
End If
Case ConsoleKey.D
If Game.GameOver = False Then
If Game.Player1.PlayerX + 1 > 1 And Game.Player1.PlayerX + 1 < Game.MaxX And Game.GameOver = False Then
Game.Player1.PlayerX += 1
ElseIf Game.Player1.PlayerX + 1 < 0 And Game.GameOver = False Then
Game.Player1.PlayerX += 1
ElseIf Game.Player1.PlayerX + 1 > Game.MaxX And Game.GameOver = False Then
Game.Player1.PlayerX += 1
Else
Game.Player1.PlayerX -= 1
End If
Console.Clear()
Game.DrawGame()
Game.Player1.Nottired = True
End If
Case ConsoleKey.G
If Game.GameOver = False Then
If Game.Player1.Nottired = True Then
Game.DidIHitGold()
Game.Player1.Nottired = False
Else
Game.GameMessage = "Too Tired!"
Console.Clear()
Game.DrawGame()
End If
End If
Case ConsoleKey.Escape
If Game.GameOver = True Then
Console.Clear()
Main()
End If
End Select
End While
End Sub
End Module
Here we take our grids dimensions, and the players location to determine that he is not outside the border area at all for the animation keys. Before that though we make sure that the game is not over, that the player is not tired, and that the game is over when it should be according to the key that is pressed. If the player moves then the game is redrawn simulating the animation as we discussed, and everything flows according to the conditions. There you have it a simple expansion on a simple yet mind boggling concept encapsulated in a game and additionally introducing briefly the concept of drawing graphics.





MultiQuote


|