In this episode, I am going to attempt to make my character move by the arrow keys. Just left and right. First, I need to set up the bitmap that will hold the character
Dim PlayerBmp As Bitmap 'This will hold the main character graphics. 'Actually, it is a bunch of frames of the character. |
Const CHARWIDTH As Integer = 16 Const CHARHEIGHT As Integer = 32 'These define the width of the character in the file. |
PlayerBmp = New Bitmap(Application.StartupPath & "\..\sumult.bmp") 'Loads the image into this bitmap. 'Given this arrangement, the bitmap must be in the same directory as the solution. PlayerBmp.MakeTransparent(Color.Magenta) 'Makes all of the magenta in the bitmap transparent. |
GFX.DrawImage(PlayerBmp, 100, LANDHEIGHT - CHARHEIGHT, New Rectangle(0, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display. Right now, the 100 is a fixed number, but When the character moves |
Dim PlayerLoc As Rectangle 'This will hold the current location of the player. X and Y included. 'Why didn't I use point? Because I am probably going to use the height and width for collisions later. Dim GoLeft, GoRight As Boolean 'Indicates if the left / right buttons are pressed. Const MOVESPEED As Integer = 5 'How much I move every tick |
PlayerLoc = New Rectangle(100, LANDHEIGHT - CHARHEIGHT, CHARWIDTH, CHARHEIGHT) 'Instantiate the player location. |
Private Sub PFMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.Left Then GoLeft = True ElseIf e.KeyCode = Keys.Right Then GoRight = True End If End Sub |
Private Sub PFMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Left Then GoLeft = False ElseIf e.KeyCode = Keys.Right Then |
If GoLeft Then PlayerLoc.Offset(-MOVESPEED, 0) 'Move player left. UpdateArtwork = True ElseIf GoRight Then PlayerLoc.Offset(MOVESPEED, 0) 'Move player right. UpdateArtwork = True End If |
GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(0, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character To the display, based on the current character location. |
Dim CharDirec As Integer 'Which direction is the character facing? |
If GoLeft Then PlayerLoc.Offset(-MOVESPEED, 0) 'Move player left. UpdateArtwork = True CharDirec = 0 'Look left. ElseIf GoRight Then PlayerLoc.Offset(MOVESPEED, 0) 'Move player right. UpdateArtwork = True CharDirec = 1 'Look right. End If |
GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * (CharDirec << 2), 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location. |
GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * 4, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location. |