(note, this uses the green square)
First, you need to switch over to tile / grid movement. In 2D RPGs, the characters usually move in whole units of tiles. Here's how his works
Check to see if the player is centered on a tile If so Stop the player object only allow one direction of movement from that spot (up, down, left right) Each Direction Does this : Shove the player object off the tile (you'll see why) Set the Motion to the direction If Not 'the object must be moving so Move the Object!
This works by "modding" PlayerX and PlayerY by the tile size. So, if there is no remainder, the player object is centered on a tile. Mod returns the remainder of a division.
Because the playerobject begins a movement cycle on a tile, it needs to be shoved off of it, or the Else part of this operation won't work.
Here's the code :
Dim PlayerMoveX ' holds the player object horizontal movement Dim PlayerMoveY ' holds the player object vertical movement Dim GameOver ' set player object X and Y location playerX = 40 playerY = 40 graphics.Initialize 800, 600 key.Initialize greenSquare = graphics.LoadImage("greenSquare.png") gameOver = 0 Do ' Game Logic ' If the player hits ESCAPE or the X on the window, the game ends If key.Pressed(vk_escape) or key.Pressed(vk_windowx) Then gameOver = 1 ' check to see if player object is centered on a tile If PlayerX mod 40 = 0 and PlayerY mod 40 = 0 Then ' stop the player object PlayerMoveX = 0 PlayerMoveY = 0 ' get player input and set motion If key.Pressed(vk_left) Then 'shove player object off tile PlayerX = PlayerX - 1 'set motion left PlayerMoveX = -1 ElseIf key.Pressed(vk_right) Then PlayerX = PlayerX + 1 PlayerMoveX = 1 ElseIf key.Pressed(vk_up) Then PlayerY = PlayerY - 1 PlayerMoveY = -1 ElseIf key.Pressed(vk_down) Then PlayerY = PlayerY + 1 PlayerMoveY = 1 End If Else 'if player obj not centered on tile, then it's moving PlayerX = PlayerX + PlayerMoveX PlayerY = PlayerY + PlayerMoveY End If ' Set the image in a new location graphics.SetXY greenSquare, PlayerX, PlayerY graphics.Clear rgb(255,255,255) graphics.SetImage greenSquare graphics.SetFPS 60 graphics.Display Loop Until gameOver graphics.Terminate key.Terminate
Make sure you understand this, if not, ask questions. Next is basic "collision" —hartnell