VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a

by Winter Grave (1 Submission)
Category: Games
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 19th December 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a character through a map (Not a Tile_Based

API Declarations


Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28


Rate I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a



'by Winter
'You will need the following objects:
'A PictureBox to draw (PScreen)
'A PictureBox with the Map (MapPicture)
'A PictureBox with the Character (CharPic)
'A PictureBox with the Character's Mask (CharMask)
'A Timer to do Character animation (CharAnim)
'A Timer to call the Character's movements (Motion)

'The variables
Dim CharXFrame As Integer
Dim CharYFrame As Integer
Dim CharX As Integer
Dim CharY As Integer
Dim MapX As Integer
Dim MapY As Integer
Dim MapWidth As Integer
Dim MapHeight As Integer

Private Sub CharAnim_Timer()
'You will use the BitBlt function to draw the character on the screen
'and taking the source from the picture that has the character's frames
'the variables CharXFrame and CharYFrame set the xSrc and ySrc for the
'BitBlt function
'Let's say the character is 40 x 40 pixels and on the bitmap the animation
'you want to play is on the seccond linte. We use this instead of the
'sprite control.
CharYFrame = 40
CharXFrame = CharXFrame + 40
If CharXFrame > 40 Then CharXFrame = 0
End Sub

Private Sub Movements()
'Make the Character Move with the keys
If GetAsyncKeyState(VK_UP) < 0 Then CharY = CharY - 5 'You can choose a value other than 5
If GetAsyncKeyState(VK_DOWN) < 0 Then CharY = CharY + 5
If GetAsyncKeyState(VK_LEFT) < 0 Then CharX = CharX - 5
If GetAsyncKeyState(VK_RIGHT) < 0 Then CharX = CharX + 5

'Take care of moving the map if the character goes up
'near the edge of the screen
If CharY < PScreen.Height * 0.1 Then
MapY = MapY + 5 'Move the map down
If MapY < 0 Then CharY = PScreen.Height * 0.1 'While the edge of the map is still above the edge of the screen, keep the character at the 10% from the edge of the screen
If MapY > 0 Then MapY = 0 'For keeping map's edge at the edge of screen
If CharY < 0 Then CharY = 0 'For keeping the character inside the map
End If

'Take care of moving the map if the character goes down
'near the edge of the screen
If CharY > (PScreen.Height * 0.9) - 40 Then 'Remember Character is 40 x 40 px
MapY = MapY - 5 'Move the map up
If MapY > PScreen.Height - MapPicture.Height Then PacY = (PScreen.Height * 0.9) - 40
If MapY < PScreen.Height - MapPicture.Height Then MapY = PScreen.Height - MapPicture.Height
If CharY > PScreen.Height - 100 Then CharY = PScreen.Height - 100
End If

'Take care of moving the map if the character goes left
'near the edge of the screen
If CharX < PScreen.Width * 0.1 Then
MapX = MapX + 5 'Move the map right
If MapX < 0 Then CharX = PScreen.Width * 0.1 'While the edge of the map is beyond the edge of the screen, keep the character at 10% from the edge of the screen
If MapX > 0 Then MapX = 0 'Keep the map's edge at the screen's edge
If CharX < 0 Then CharX = 0 'Keep the character inside the map
End If

'Take care of moving the map if the character goes right
'near the edge of the screen
If CharX > (PScreen.Width * 0.9) - 40 Then 'Remember the character is 40x40 px
MapX = MapX - 5
If MapX > PScreen.Width - MapPicture.Width Then CharX = (PScreen.Width * 0.9) - 40
If MapX < PScreen.Width - MapPicture.Width Then MapX = PScreen.Width - MapPicture.Width
If CharX > PScreen.Width - 40 Then CharX = PScreen.Width - 40
End If

'For last, to draw all on the screen with the BitBlt
'Draw background
BitBlt PScreen.hDC, MapX, MapY, MapPicture.Width, MapWidth, MapHeight, MapPicture.hDC, 0, 0, vbSrcCopy

'Draw the character's mask
BitBlt PScreen.hDC, CharX, CharY, 40, 40, CharMask.hDC, CharXFrame, CharYFrame, vbSrcAnd
'Draw the character
BitBlt PScreen.hDC, CharX, CharY, 40, 40, CharPic.hDC, CharXFrame, CharYFrame, vbSrcPaint
PScreen.Refresh 'Refresh the screen
End Sub

Private Sub Motion_Timer()
'Call the Movements Sub
Movements
End Sub


Download this snippet    Add to My Saved Code

I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a Comments

No comments have been posted about I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a . Why not be the first to post a comment about I have been trying to create a game RPG style like Zelda 3. The following code shows how to move a .

Post your comment

Subject:
Message:
0/1000 characters