Mynotoar
Active member
Hey again! I know I keep asking questions but you guys are so wonderful and I need some help ^^'.
I'm trying to make a program where you can move around a cursor on a 15x20 grid (the size can change dynamically,) and place coloured blocks (█) in any way you like to make a basic ASCII picture. I have a grid stored as a 2D array that runs on a loop: every time it first writes a blank grid full of empty spaces (-), then it defines a cursor which you use to place blocks, and then it waits for a keypress. If you press A or D it changes the colour of the cursor, if you press the arrowkeys it moves the cursor around, and I want it to place blocks when I press space - but I'm having trouble with it.
I have an array initialised, and every time it writes the grid it reads from that array and if there are any coordinates defined in it then it will place a block there. The declaration looks like this:
Dim PixelData(,) As Integer = {{3, 4, 6}, {7, 8, 1}}
It's a 2D array and it reads every block in the braces, using the first two numbers to work out the coordinates of where to place a block, and the third number is the colour, so {3, 4, 6} will place a block three down, four across, and the colour purple (ConsoleColor = 6.)
This works fine when initialising, but I don't want to initalise, I want to add to it, so that every time I press space a new set of braces is created using the two Cursor position values and the Colour value. I tried to figure out how to use ReDim, but I couldn't work out the syntax, and if I can I want to use something that doesn't eat up so much of my CPU.
Can anyone help me figure out the best way to add more values to the array? Someone mentioned using ArrayLists, which would be interesting, only I'd have to change everything to accommodate and I don't know how to use ArrayLists.
I'll post the code below, with some hazy comments.
Thank you!
I'm trying to make a program where you can move around a cursor on a 15x20 grid (the size can change dynamically,) and place coloured blocks (█) in any way you like to make a basic ASCII picture. I have a grid stored as a 2D array that runs on a loop: every time it first writes a blank grid full of empty spaces (-), then it defines a cursor which you use to place blocks, and then it waits for a keypress. If you press A or D it changes the colour of the cursor, if you press the arrowkeys it moves the cursor around, and I want it to place blocks when I press space - but I'm having trouble with it.
I have an array initialised, and every time it writes the grid it reads from that array and if there are any coordinates defined in it then it will place a block there. The declaration looks like this:
Dim PixelData(,) As Integer = {{3, 4, 6}, {7, 8, 1}}
It's a 2D array and it reads every block in the braces, using the first two numbers to work out the coordinates of where to place a block, and the third number is the colour, so {3, 4, 6} will place a block three down, four across, and the colour purple (ConsoleColor = 6.)
This works fine when initialising, but I don't want to initalise, I want to add to it, so that every time I press space a new set of braces is created using the two Cursor position values and the Colour value. I tried to figure out how to use ReDim, but I couldn't work out the syntax, and if I can I want to use something that doesn't eat up so much of my CPU.
Can anyone help me figure out the best way to add more values to the array? Someone mentioned using ArrayLists, which would be interesting, only I'd have to change everything to accommodate and I don't know how to use ArrayLists.
I'll post the code below, with some hazy comments.
VB.NET:
Sub ConsolePaint()
Dim BorderColour As ConsoleColor = 1
Dim BGColour As ConsoleColor = 0
Dim BlankColour As ConsoleColor = 15
Dim CursorColour As ConsoleColor = 1
'Defines the colour of the border, background, blank spaces, and the cursor.
Dim GridHeight As Integer = Console.WindowHeight - 5
Dim GridWidth As Integer = Console.WindowWidth - 2
'Normally defines the Grid at around 78x20...
GridHeight = 15 : GridWidth = 20
'But here I wanted a 15x20 grid.
Dim GridData() As Integer = {GridHeight, GridWidth}
'Defines the ColourGrid array according to the height and width values.
Dim PixelData(,) As Integer = {{3, 4, 5}}
'Fills the grid with pre-defined pixels. The first number is the down coordinate,
'The second number is the across coordinate, and the third number is the colour.
Dim ColourGrid(GridData(0), GridData(1)) As String
'Creates the Grid itself.
Dim BlankPixel As String = "-"
Dim Block As String = "█"
Dim CursorPixel As String = "█"
'Defines the Blank Spaces as a hyphen, and for Pixels and the Cursor to be a Block.
Dim CursorPos() As Integer = {0, 0}
'The starting position of the Cursor.
Dim Keypress As ConsoleKey
Dim ValidKey As Boolean
Do
Console.Clear()
For i = 1 To 5
For j = 0 To 15
Console.ForegroundColor = j
Console.Write(Block)
'This creates a pretty multi-coloured border at the top.
Next
Next
Console.ResetColor()
Console.BackgroundColor = ConsoleColor.White
Console.ForegroundColor = ConsoleColor.Black
Console.Write("ConsolePaint")
Console.Write(" - ←↑↓→: move cursor - A/D: change colour - M: Menu - Space: Place ")
Console.ResetColor()
Console.Write("Colour: ")
Console.ForegroundColor = CursorColour
Console.Write(Block)
'This highlights the current colour selected.
Console.ResetColor()
Console.Write(" [")
Console.WriteLine(CursorColour & "]")
Console.WriteLine()
Console.ResetColor()
For i = 0 To GridHeight
For j = 0 To GridWidth
ColourGrid(i, j) = BlankPixel
'This fills the Grid with Blank Spaces.
Next
Next
ColourGrid(CursorPos(0), CursorPos(1)) = CursorPixel
'This creates the Cursor at the starting position.
For i = 0 To GridWidth + 2
Console.Write(Block)
'This creates a top border two wider than the Grid.
Next
For i = 0 To GridHeight
For j = 0 To GridWidth
If j = 0 Then
Console.WriteLine()
Console.Write(Block)
'This puts a block at the left of every row.
End If
Console.BackgroundColor = BGColour
If PixelData.GetLength(1) > 0 Then
If i = PixelData(0, 0) And j = PixelData(0, 1) Then
Console.ForegroundColor = PixelData(0, 2)
ColourGrid(PixelData(0, 0), PixelData(0, 1)) = Block
'This writes pre-defined blocks.
End If
End If
If i = CursorPos(0) And j = CursorPos(1) Then
Console.ForegroundColor = CursorColour
'This sets the colour of the cursor.
End If
Console.Write(ColourGrid(i, j))
'This is the actual thing that writes out everything.
Console.ResetColor()
If j = GridWidth Then
Console.Write(Block)
'This writes a block at the end of every line.
End If
Next
Next
For i = 0 To GridWidth + 2
If i = 0 Then
Console.WriteLine()
End If
Console.Write(Block)
'This puts a block border at the bottom.
Next
Do
'This do loop waits for user input, and based on the keypress,
'moves the cursor around the grid. If the cursor cannot move,
'it does nothing. If an invalid key is pressed, it does nothing.
Keypress = Console.ReadKey(True).Key
If Keypress = ConsoleKey.UpArrow Then
If CursorPos(0) - 1 < 0 Then
ValidKey = False
Else
CursorPos(0) -= 1
ValidKey = True
End If
ElseIf Keypress = ConsoleKey.DownArrow Then
If CursorPos(0) + 1 > GridHeight Then
ValidKey = False
Else
CursorPos(0) += 1
ValidKey = True
End If
ElseIf Keypress = ConsoleKey.LeftArrow Then
If CursorPos(1) - 1 < 0 Then
ValidKey = False
Else
CursorPos(1) -= 1
ValidKey = True
End If
ElseIf Keypress = ConsoleKey.RightArrow Then
If CursorPos(1) + 1 > GridWidth Then
ValidKey = False
Else
CursorPos(1) += 1
ValidKey = True
End If
ElseIf Keypress = ConsoleKey.A Then
If CursorColour - 1 < 0 Then
CursorColour = 15
Else
CursorColour -= 1
End If
ValidKey = True
ElseIf Keypress = ConsoleKey.D Then
If CursorColour + 1 > 15 Then
CursorColour = 0
Else
CursorColour += 1
End If
ValidKey = True
ElseIf Keypress = ConsoleKey.Spacebar Then
'THIS IS THE PROBLEM AREA WHERE I HAVEN'T WORKED OUT WHAT TO DO.
Else
ValidKey = False
End If
Loop Until ValidKey = True
Loop
End Sub
Thank you!