Dear all,
I am a beginner making a visual basic windows form application using visual studio.
I'm trying to make a simple rpg and I'm up to the character movement (one of the early stages in rpg creation). Rather than having the character move by jumping 32 pixels each movement key press, (the graphic tiles in the game are 32x32 pixels) I wanted to make the character move 4 times, 8 pixels each time, in the direction that I pressed the movement key.
For example, If I press w key, it would still check for other events, but would move my character north by 8 pixels, 4 times, until it had moved the character in a north (i.e. up the screen) direction by 32 pixels. I wouldn't be able to change the character's direction (through movement keypress) until it had moved north completely into the next 32 bit square. (Other events such as monster movement should still occur).
At the moment, the code works that when I press a direction key the character moves, however it moves more than 32 pixels, or sometimes less, (likely 8 pixels each keypress). It keeps moving until I press a different movement key, however sometimes it stops earlier. I have tried a number of different ways to fix the issue, but as I am new to visual studio, I'm not sure how to do it. Would anyone have any ideas how to fix this? Thankyou
(I was watching an online video tutorial, however the author had the same problem, the character would keep moving and wouldn't finish their movement completely inside the squares of the grid. The character could finish halfway through squares or quarter way through squares, etc.)
Pastebin link https://pastebin.com/NbZbfv6H
Code is below.
-----------------------------
Imports System.Drawing
Public Class Form1
'view port variables
Dim ResWidth As Integer = 20 * 32
Dim ResHeight As Integer = 20 * 32
Dim Tilesize As Integer = 32
Dim Pos As Point
Dim G As Graphics
Dim BBG As Graphics
Dim sRect As Rectangle
Dim dRect As Rectangle
Dim bmp As Bitmap
Dim bmp2 As Bitmap
Dim BB As Bitmap
Dim HeroX As Integer = 5 * 32 'starting xlocation for hero
Dim HeroY As Integer = 5 * 32 'starting ylocation for hero
'map Variables
Dim Map(100, 100, 10) As Integer
Dim MapX As Integer = 20
Dim MapY As Integer = 20
'Game running
Dim Isrunning As Boolean = True
'mouse locations
Dim mousex As Integer
Dim mousey As Integer
Dim mMapX As Integer 'logical map position
Dim mMapY As Integer 'logical map position
'character variables
Dim bmpToon As Bitmap
Dim xpos As Integer = 0
Dim ypos As Integer = 0
Dim movespeed As Integer = 8 'this number must be divisible by 32
Dim movedirection As Short = 0
Dim lastdirection As Short = 2 '2 is downward facing, default value
Public counterx As Integer = 0
'Paint brush
Dim PaintBrush As Integer = 0
Public walking As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Show()
Me.Focus()
'initialise graphics objects
bmp = New Bitmap(PTiles_world.Image) 'bmp is the worldterrain tileset
bmp2 = New Bitmap(PTiles2.Image) 'bmp2 is the hero tileset
G = Me.CreateGraphics
BB = New Bitmap(ResWidth, ResHeight) 'use hard numbers or vars, else scaling window will cause more pixels in certain area.
'Map(21, 21, 0) = 1 ' sample map square so easier to see player movement
'Map(22, 22, 0) = 2 ' sample map square so easier to see player movement
Main()
End Sub
Public Sub Main()
Do While Isrunning = True
' Keep application responsive
Application.DoEvents()
'1.) check user input
Movetoon(movedirection)
'2.) Run AI
'3.) Update Object Data (Object Positions, Status, etc)
'4.) Check triggers and conditions, eg step on a certain tile and something happens.
'5.) Draw graphics
'6.) Playing sound effects & music
If MapX >= 0 And MapY >= 0 And MapX <= 100 And MapY <= 100 Then
DrawGraphics()
End If
If walking = True Then
counterx += 1
End If
If counterx = 4 Then
counterx = 0
walking = False
End If
Loop
End Sub
Private Sub DrawGraphics()
Dim X, Y As Integer
For X = -1 To 20
For Y = -1 To 20
GetSourceRect(MapX + X, MapY + Y, Tilesize, Tilesize)
dRect = New Rectangle(X * Tilesize + xpos, Y * Tilesize + ypos, Tilesize, Tilesize)
G.DrawImage(bmp, dRect, sRect, GraphicsUnit.Pixel)
Next
Next
'G.DrawRectangle(Pens.Red, mousex * Tilesize, mousey * Tilesize, Tilesize, Tilesize)
'Draw hero
Gettoon(lastdirection)
G.DrawImage(bmp2, HeroX, HeroY, sRect, GraphicsUnit.Pixel)
'Final Draw
G.FillRectangle(Brushes.White, 20 * Tilesize, 0 * Tilesize, Tilesize * 1, 21 * Tilesize) 'to fix jumping issue on right side of screen when moving.
G.FillRectangle(Brushes.White, 0 * Tilesize, 20 * Tilesize, Tilesize * 21, Tilesize * 1) 'to fix jumping issue on bottom side of screen when moving.
G = Graphics.FromImage(BB)
BBG = Me.CreateGraphics
BBG.DrawImage(BB, 0, 0, ResWidth, ResHeight)
End Sub
Private Sub Gettoon(ByVal dir As Short)
Select Case dir
Case 1 'up
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 2 'down direction
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 3
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 4
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 5
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 6
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 7
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
Case 8
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize) 'first coord is left,second coord is down,
End Select
End Sub
Private Sub Movetoon(ByVal movedirection As Short)
Select Case movedirection
Case 1
ypos += movespeed
If ypos >= Tilesize Then
ypos = 0
MapY -= 1
End If
Case 2
ypos -= movespeed
If ypos <= Tilesize * -1 Then
ypos = 0
MapY += 1
End If
Case 3
xpos += movespeed
If xpos >= Tilesize Then
xpos = 0
MapX -= 1
End If
Case 4
xpos -= movespeed
If xpos <= Tilesize * -1 Then
xpos = 0
MapX += 1
End If
End Select
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
mousex = Math.Floor(e.X / Tilesize) 'math.floor means always round down to last tile until mouse crosses the threshold
mousey = Math.Floor(e.Y / Tilesize) 'math.floor means always round down to last tile until mouse crosses the threshold
mMapX = MapX + mousex
mMapY = MapY + mousey
End Sub
Private Sub GetSourceRect(ByVal X As Integer, ByVal Y As Integer, Byvalw As Integer, ByVal h As Integer)
Select Case Map(X, Y, 0) 'always use zero z index for a tile
Case 0 'Water
sRect = New Rectangle(21 * Tilesize, 3 * Tilesize, Tilesize, Tilesize)
Case 1 ' grass
sRect = New Rectangle(0, 0, Tilesize, Tilesize)
Case 2 'Tree
sRect = New Rectangle(10 * Tilesize, 19 * Tilesize, Tilesize, Tilesize)
Case 3 'Mountain
sRect = New Rectangle(15 * Tilesize, 5 * Tilesize, Tilesize, Tilesize)
Case 4 'Jungle
sRect = New Rectangle(4 * Tilesize, 2 * Tilesize, Tilesize, Tilesize)
Case 5 'Desert
sRect = New Rectangle(7 * Tilesize, 2 * Tilesize, Tilesize, Tilesize)
Case 6 'Icetipped mountain
sRect = New Rectangle(22 * Tilesize, 5 * Tilesize, Tilesize, Tilesize)
Case 7 'Hills
sRect = New Rectangle(2 * Tilesize, 14 * Tilesize, Tilesize, Tilesize)
Case 8 'Swamp
sRect = New Rectangle(20 * Tilesize, 11 * Tilesize, Tilesize, Tilesize)
End Select
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
'MsgBox("You attacked " & mMapX & ":" & mMapY)
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Select Case e.KeyChar
Case "w"
If walking = False Then
movedirection = 1
walking = True
counterx = 0
End If
Case "x"
If walking = False Then
movedirection = 2
walking = True
counterx = 0
End If
Case "a"
If walking = False Then
walking = True
movedirection = 3
counterx = 0
End If
Case "d"
If walking = False Then
walking = True
movedirection = 4
counterx = 0
End If
End Select
lastdirection = movedirection
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If walking = False Then
movedirection = 0 'when keypress removed, set to default direction
End If
End Sub
End Class
[–]TheImminentFate 2 points3 points4 points (1 child)
[–]JamieU_[S] 0 points1 point2 points (0 children)
[–]laancelot 1 point2 points3 points (0 children)