VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,161 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,538 people online right now. Registration is fast and FREE... Join Now!




Bitmaps, Arrays and .txt

 

Bitmaps, Arrays and .txt, Making a simple tile based map

Shukumei

11 Sep, 2009 - 08:34 AM
Post #1

D.I.C Head
**

Joined: 22 Sep, 2008
Posts: 54


My Contributions
Hi everyone, I am having a few issues making a simple RPG in VB.net 2008 - Before anyone says you should be using a different lang. for that, I am well aware, however I am learning VB at Uni - so for now my experimentation will be done in VB to futher my learning.

also I dont want suggestions on wether bmp or png or what have you are better or worse smile.gif

What I have: a png file 256 x 64 which has 8 32x32 tiles on 2 rows. And I have a txt file with the test map data on it:

Test Map Data txt:
keeping in mind 1 = wall and 2 = grass

11111
12221
12221
12221
11211

What I have worked out: with e.graphics I have worked out how to choose and part of the png and draw it to the form (no pic box, etc - trying to avoid them if I can), I know how to read a txt file to an array.

What I Want: I would like to have a way to attach segment of the png to the values from the txt file in a 2D Array and then somehow draw the 'map' on to the form that way - then all i have to do to change the map is change the txt file. I hope being clear - if not feel free to ask questions.

For now I have no code to show, I have gone from having code, pulling out hair, to no code and back again - right now I am back on a blank canvas so to speak.

If anyone has any ideas it would be great to know.

One more thing (maybe a few) - yes I have googled(about 3 days worth), I have asked my peers at Uni - I hope it can be done smile.gif

Thank you in advance.

User is offlineProfile CardPM
+Quote Post


Shukumei

RE: Bitmaps, Arrays And .txt

11 Sep, 2009 - 08:57 AM
Post #2

D.I.C Head
**

Joined: 22 Sep, 2008
Posts: 54


My Contributions
Was just read the post about Splitting Strings - Just a quick thought - should I use dilimiters in my txt file:

11111
12221
12221
12221
11211

or

1,1,1,1,1
1,2,2,2,1
1,2,2,2,1
1,2,2,2,1
1,1,2,1,1

This post has been edited by Shukumei: 11 Sep, 2009 - 08:58 AM
User is offlineProfile CardPM
+Quote Post

mark.bottomley

RE: Bitmaps, Arrays And .txt

11 Sep, 2009 - 09:01 AM
Post #3

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 868



Thanked: 148 times
My Contributions
I wouldn't use delimiters as you can walk the string as characters - the limitation is that each feature can only be one character (unless you do slightly trickier parsing of the string.
User is offlineProfile CardPM
+Quote Post

Shukumei

RE: Bitmaps, Arrays And .txt

11 Sep, 2009 - 09:10 AM
Post #4

D.I.C Head
**

Joined: 22 Sep, 2008
Posts: 54


My Contributions
QUOTE(mark.bottomley @ 11 Sep, 2009 - 09:01 AM) *

I wouldn't use delimiters as you can walk the string as characters - the limitation is that each feature can only be one character (unless you do slightly trickier parsing of the string.


Thank you for the advice - still back at square one though - lol biggrin.gif
I will work this out even if means I go bold - mmm, but I love my hair... oh the hard choices in life tongue.gif
User is offlineProfile CardPM
+Quote Post

mark.bottomley

RE: Bitmaps, Arrays And .txt

11 Sep, 2009 - 12:50 PM
Post #5

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 868



Thanked: 148 times
My Contributions
CODE

TerrainChar = InputLine(I)
Select Case TerrainChar
  Case 1
    ' draw the wall png at x, y
  Case 2
    ' draw the grass png at x, y
  Case other terrain types - e.g. Water, mountain, etc...
  ...
End Case
' Update the x, y - if last character in line, increment x, reset y, read new line else increment y


You need to decide how the 5x5 blocks are placed reletive to each other. e.g. in a column, row, grid, etc.. you may need to add grid coordinates for the sub blocks if you want to be able to enter them in random order, or an incomplete grid.
User is offlineProfile CardPM
+Quote Post

Shukumei

RE: Bitmaps, Arrays And .txt

13 Sep, 2009 - 07:55 AM
Post #6

D.I.C Head
**

Joined: 22 Sep, 2008
Posts: 54


My Contributions
Thank your help mark.bottomley - this is the code I end up with and it works biggrin.gif

CODE
Dim mapFileSR As New StreamReader(mapFile)
        Dim index As Integer = 0

        Dim tileXLoc As Point = New Point(0, 0)
        Dim tileYLoc As Point = New Point(32, 32)

        While Not mapFileSR.EndOfStream
            lineoftext = mapFileSR.ReadLine
            If lineoftext <> "" Then
                ReDim Preserve myLines(index)
                myLines(index) = lineoftext

                For y As Integer = 0 To myLines.Length - 1

                    For x As Integer = 0 To myLines(y).Length - 1

                        tileXLoc.Y = y * 32
                        tileXLoc.X = x * 32

                        Select Case myLines(y).Substring(x, 1)

                            Case CChar("1") 'Grass
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                           New Rectangle(0, 0, 32, 32), GraphicsUnit.Pixel)

                            Case CChar("2") 'Stone Wall
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                       New Rectangle(32, 0, 32, 32), GraphicsUnit.Pixel)

                            Case CChar("3") 'Floor Boards
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                       New Rectangle(64, 0, 32, 32), GraphicsUnit.Pixel)

                            Case CChar("4") 'Stone in Grass
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                       New Rectangle(0, 32, 32, 32), GraphicsUnit.Pixel)

                            Case CChar("5") 'Dirt in Grass
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                       New Rectangle(32, 32, 32, 32), GraphicsUnit.Pixel)

                            Case CChar("6") 'Dirt
                                e.Graphics.DrawImage(tiles, New Rectangle(tileXLoc, CType(tileYLoc, Drawing.Size)), _
                                       New Rectangle(64, 32, 32, 32), GraphicsUnit.Pixel)

                        End Select
                    Next x
                Next y

                index += 1

            End If
        End While

        'Draw the Hero to the screen - only here for testing purposes
        e.Graphics.DrawImage(heroSprite, New Rectangle(64, 64, 32, 32), _
                                    New Rectangle(160, 0, 32, 32), GraphicsUnit.Pixel)


Thank you for your help again Mark biggrin.gif





User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:04PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month