View Single Post
 
Old 12-12-2004, 05:53 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Arrow Tile Map with animating character on it.

Well, the second installment here. This time I have a character on the map.
I use a collision detection to check if the character collides with the chess tile or the edge of the board.

New highlights here:
Code:
    Dim Charpics(3) As Bitmap        'Hold each frame in one of the array elements.
    Dim Cyc As Integer                  'This number tells which frame to draw.
The character animations are in separate frames.

Code:
        For LY = 0 To 15
            Ln = SR.ReadLine()  'One row of tiles from the file.

            For LX = 0 To 15    'Goes through each column.

                Chs = Ln.Substring(LX, 1)   'Get a string with one character in it.

                Map(LX, LY) = Integer.Parse(Chs)  'Convert this string into its matching number.
                '"0" -> 0, "1" -> 1 ...
                'The number is put into the map.

            Next

        Next

        SR.Close()
The map is read from the included text file.

Since the character is animating, I have created a form object
Dim FormGFX As Graphics 'Draws to the form.
That draws the map on demand.

Dim Backup As Bitmap 'Backup picture holds the map that we've just drawn.

GFX = Graphics.FromImage(Backup) 'GFX draws onto the backup picture.
FormGFX = Me.CreateGraphics() 'Draw to the form.

And now, we can draw the map to the form in the draw subroutine.
Code:
        'Map code.
        GFX.DrawImage(Charpics(Cyc), CharacterLocation.X * 16, CharacterLocation.Y * 16)
        'Draw the character to the display at the location.

        'At this point the whole map should reside on the backup bitmap.  The bitmap is not visible, so it must be made visible.
        FormGFX.DrawImage(Backup, Me.ClientRectangle())
The * 16 is the tilewidth, so this converts our player's tile location into a screen location, so that the player appears to be walking on the map.

We need to update the map display in order to show the character's animation.

Code:
    Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick
        Cyc = (Cyc + 1) Mod 4   'Cyc cycles through 0, 1, 2, 3, 0, 1, 2, 3, 0, 1 ...
        Draw()      'Draw.
    End Sub
And finally the collision detection.
Code:
   Private Sub TileTest_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        Dim Collided As Boolean

        'This is the basic movement.  Collision detection always comes after this movement caused by the user.
        If e.KeyCode = Keys.Left Then
            CharacterLocation.Offset(-1, 0)
        ElseIf e.KeyCode = Keys.Up Then
            CharacterLocation.Offset(0, -1)
        ElseIf e.KeyCode = Keys.Right Then
            CharacterLocation.Offset(1, 0)
        ElseIf e.KeyCode = Keys.Down Then
            CharacterLocation.Offset(0, 1)
        End If

        'Check for collisions after moving the character.
        If CharacterLocation.X < 0 OrElse CharacterLocation.X > Map.GetUpperBound(0) OrElse CharacterLocation.Y < 0 OrElse CharacterLocation.Y > Map.GetUpperBound(1) Then
            Collided = True
        ElseIf Map(CharacterLocation.X, CharacterLocation.Y) = 3 Then
            Collided = True

        End If

        'If we have collded, then the character needs to move back immediately.
        If Collided Then
            'Take all of the keycodes and reverse their operation.
            If e.KeyCode = Keys.Left Then
                CharacterLocation.Offset(1, 0)
            ElseIf e.KeyCode = Keys.Up Then
                CharacterLocation.Offset(0, 1)
            ElseIf e.KeyCode = Keys.Right Then
                CharacterLocation.Offset(-1, 0)
            ElseIf e.KeyCode = Keys.Down Then
                CharacterLocation.Offset(0, -1)
            End If

        End If

        'Note that the keydown event can fire more than once over the 100 ms that the timer is not drawing.
    End Sub
Of course, I've attached the commented project as well.
Attached Files
File Type: zip TileMap2.zip (21.0 KB, 228 views)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote