I'm trying to make an ASCII graphics Visual Basic console game and my character won't move into the space immediately left on object?
-
Here is my entire program so far. The map is a long string with "\" meaning new line and 0 through F meaning a foreground color and G through V being a background color and every time I change the color somewhere on the map, I am unable to move my character to the space immediately left of it while playing. I've had this bug for days and it's really bugging me. I have no idea where the problem is. If you need more information, ask and I will provide it. Any help is appreciated. Module Module1 Private Structure Point Dim x As Integer Dim y As Integer End Structure Dim currentMap As String 'The current map as a string Dim inkey As String 'any key, like inkey$ in QBasic Dim currentLocation As Point 'current Point location on the map Dim nextLocation As Point 'used to test for collision before guy moves Dim backgroundColorOfGuy As Char Sub Main() currentLocation.x = 13 currentLocation.y = 9 nextLocation.x = 13 nextLocation.y = 9 setMap(-1, 0) 'Starting location (-1 = special location) 'Console.Write("Hello ") 'Console.BackgroundColor = ConsoleColor.Cyan 'Console.ForegroundColor = ConsoleColor.Green 'Console.Write("Hello") 'Dim anykey As String drawMap() setBackgroundColorOfGuy() Do drawGuy() inkey = Console.ReadKey(True).KeyChar 'input character does not display Select Case inkey Case "W" nextLocation.y = nextLocation.y - 1 preventCollision() Case "S" nextLocation.y = nextLocation.y + 1 preventCollision() Case "A" nextLocation.x = nextLocation.x - 1 preventCollision() Case "D" nextLocation.x = nextLocation.x + 1
-
Answer:
Case "D" nextLocation.x = nextLocation.x + 1 preventCollision() End Select Loop End Sub Private Sub preventCollision() Dim x As Integer = -1 Dim y As Integer For Each c As Char In currentMap If c = "\" Then y = y + 1 x = -1 ElseIf c "0" And c "1" And c "2" And c "3" And c "4" And c "5" And c "6" And c "7" _ And c "8" And c "9" And c "A" And c "B" And c "C" And c "D" And c "E" And c "F" _ And c "G" And c "H" And c "I" And c "J" And c "K" And c "L" And c "M" And c "N" _ And c "O" And c "P" And c "Q" And c "R" And c "S" And c "T" And c "U" And c "V" Then x = x + 1 End If If x = nextLocation.x And y = nextLocation.y And c " " Then nextLocation.x = currentLocation.x nextLocation.y = currentLocation.y End If Next End Sub Sub drawGuy() Console.SetCursorPosition(currentLocation.x, currentLocation.y) Console.Write(" ") Console.SetCursorPosition(nextLocation.x, nextLocation.y) Console.Write("Θ") currentLocation.x = nextLocation.x currentLocation.y = nextLocation.y End Sub Sub setBackgroundColorOfGuy() Select Case backgroundColorOfGuy 'maybe redundant to put this here, perhaps it's own class and called after drawMap()? Case "0" Console.BackgroundColor = ConsoleColor.Black Case "1" Console.BackgroundColor = ConsoleColor.Blue Case "2" Console.BackgroundColor = ConsoleColor.Cyan Case "3" Console.BackgroundColor = ConsoleColor.DarkBlue Case "4" Console.BackgroundColor = ConsoleColor.DarkCyan Case "5" Console.BackgroundColor = ConsoleColor.DarkGray Case "6" Console.BackgroundColor = ConsoleColor.DarkGreen Case "7" Console.BackgroundColor = ConsoleColor.DarkMagenta Case "8" Console.BackgroundColor = ConsoleColor.DarkRed Case "9" Console.BackgroundColor = ConsoleColor.DarkYellow Case "A" Console.BackgroundColor = ConsoleColor.Gray Case "B" Console.BackgroundColor = ConsoleColor.Green Case "C" Console.BackgroundColor = ConsoleColor.Magenta Case "D" Console.BackgroundColor = ConsoleColor.White Case "E" Console.BackgroundColor = ConsoleColor.Red Case "F" Console.BackgroundColor = ConsoleColor.Yellow End Select End Sub Sub drawMap() Console.Clear() Dim x As Integer = 0 Dim y As Integer = 0 'Dim foregroundColor As Boolean = False 'Dim backgroundColor As Boolean = False For Each c As Char In currentMap If c = "\" Then y = y + 1 x = 0 Console.Write(vbCrLf) ElseIf c = "0" Or c = "1" Or c = "2" Or c = "3" Or c = "4" Or c = "5" Or c = "6" Or c = "7" _ Or c = "8" Or c = "9" Or c = "A" Or c = "B" Or c = "C" Or c = "D" Or c = "E" Or c = "F" _ Or c = "G" Or c = "H" Or c = "I" Or c = "J" Or c = "K" Or c = "L" Or c = "M" Or c = "N" _ Or c = "O" Or c = "P" Or c = "Q" Or c = "R" Or c = "S" Or c = "T" Or c = "U" Or c = "V" Then Select Case c Case "0" Console.ForegroundColor = ConsoleColor.Black Case "1" Console.ForegroundColor = ConsoleColor.Blue Case "2" Console.ForegroundColor = ConsoleColor.Cyan Case "3" Console.ForegroundColor = ConsoleColor.DarkBlue Case "4" Console.ForegroundColor = ConsoleColor.DarkCyan Case "5" Console.ForegroundColor = ConsoleColor.DarkGray Case "6" Console.ForegroundColor = ConsoleColor.DarkGreen Case "7" Console.ForegroundColor = ConsoleColor.DarkMagenta Case "8" Console.ForegroundColor = ConsoleColor.DarkRed Case "9" Console.ForegroundColor = ConsoleColor.DarkYellow Case "A" Console.ForegroundColor = ConsoleColor.Gray Case "B" Console.ForegroundColor = ConsoleColor.Green Case "C" Console.ForegroundColor = ConsoleColor.Magenta Case "D" Console.ForegroundColor = ConsoleColor.White Case "E" Console.ForegroundColor = ConsoleColor.Red Case "F" Console.ForegroundColor = ConsoleColor.Yellow Case "G" Console.BackgroundColor = ConsoleColor.Black Case "H" Console.BackgroundColor = ConsoleColor.Blue Case "I" Console.BackgroundColor = ConsoleColor.Cyan Case "J" Console.BackgroundColor = ConsoleColor.DarkBlue Case "K" Console.BackgroundColor = ConsoleColor.DarkCyan Case "L" Console.BackgroundColor = ConsoleColor.DarkGray Case "M" Console.BackgroundColor = ConsoleColor.DarkGreen Case "N" Console.BackgroundColor = ConsoleColor.DarkMagenta Case "O" Console.BackgroundColor = ConsoleColor.DarkRed Case "P" Console.BackgroundColor = ConsoleColor.DarkYellow Case "Q" Console.BackgroundColor = ConsoleColor.Gray Case "R" Console.BackgroundColor = ConsoleColor.Green Case "S" Console.BackgroundColor = ConsoleColor.Magenta Case "T" Console.BackgroundColor = ConsoleColor.White Case "U" Console.BackgroundColor = ConsoleColor.Red Case "V" Console.BackgroundColor = ConsoleColor.Yellow End Select Else x = x + 1 Console.Write(c) End If Next 'currentMap End Sub Sub setMap(ByVal x As Integer, ByVal y As Integer) currentMap = "" Select Case x Case -1 'Negative one is for special maps, like in houses (not outside) Select Case y Case 0 currentMap = "\\\\" currentMap = currentMap & " 8Q████████████████████████████████████████████████G██\" currentMap = currentMap & " Q██ ██ DΘ8 ██ E▒▒▒▒▒▒▒8G██\" currentMap = currentMap & " Q██ ██ ██ E▒▒▒▒▒▒▒8G██\" currentMap = currentMap & " Q██ ██ ██ G██\" currentMap = currentMap & " Q██ ██ ██ G██\" currentMap = currentMap & " Q██ ██ ██ G██\" currentMap = currentMap & " Q████████ ██████████ █████ G██\" currentMap = currentMap & " Q██ V┌──┐Q G██\" currentMap = currentMap & " Q██████████████ ████ V│ │QΘ8 G██\" currentMap = currentMap & " Q██ ██ V│ │Q G██\" currentMap = currentMap & " Q██ ██ V└──┘Q G██\" currentMap = currentMap & " Q██ ██ G██\" currentMap = currentMap & " Q██ ██ G██\" currentMap = currentMap & " Q██ DΘ8 ██ G██\" currentMap = currentMap & " Q██████████████████████████ ████████████████████G██\\\" backgroundColorOfGuy = "A" End Select Case 0 Select Case y Case 0 End Select End Select End Sub End Module
Ailurophile at Answerbag.com Visit the source
Related Q & A:
- What are the object-oriented features of Visual Basic.NET?Best solution by msdn.microsoft.com
- I'm trying to write a song?Best solution by Yahoo! Answers
- I'm trying to get a meeting with a diplomat to counsel me on a career in international affairs?Best solution by Yahoo! Answers
- Messenger won't display song i'm listening to?Best solution by Yahoo! Answers
- How do I output from one form to another in Visual Basic?Best solution by support.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.