Question Coordinates for 2D game

NoIdeas

Active member
Joined
Aug 13, 2011
Messages
25
Location
Sweden
Programming Experience
1-3
Hi!

I'm making a 2D game where i need a coordinate system to move the player, and i have no idea how to do it.

The system needs this requirements:

1. clickable coordinates, so when i click a coordinate, i will have a picture move to that specific coordinate.

2. Different images for different coordinates, and ability to check what image they have.

And just for the record, I dont want to create 94 buttons or something to make this work... :ambivalence:


// NoIdeas
 
For mine i use a 2 dimensional array of points.

PrivateSub Form_Grid()
Dim X, Y AsInteger
X = Loc.X
Y = Loc.Y
For Lp AsInteger = 0 To Gloc.GetLength(0) - 1
Y = Loc.Y
For lP2 AsInteger = 0 To Gloc.GetLength(1) - 1
Gloc(Lp, lP2) = New Point(X, Y)
Y += Height
Next
X += Width
Next
EndSub

Loc is the start location of the grid, Gloc is my array holding the points, X would be the 0 dimesion, and Y the 1 dimension


now that we have this we need to know which tile has been clicked so lets look at that now


Public


Function Return_Tile(ByVal FrmPoint As Point) As Point
Dim FrmX, FrmY AsDouble
'if the point is within the grid
If Is_Within(FrmPoint, Loc, Bounding_Rectangle.Height, Bounding_Rectangle.Width) = TrueThen
'adjust the point so that on the amount that it intersect with the grid is used to calulate the tile
FrmX = (FrmPoint.X - Loc.X) / (Tile_Width)
FrmY = (FrmPoint.Y - Loc.Y) / (Tile_Height)
'Since this is alway a value of 5.5 would return 6 we must round down for all decimals places
FrmX = Decimal.Floor(FrmX)

FrmY = Decimal.Floor(FrmY)
Else
' If not in grid the select first tile, looking of a better way to handle this mabye just return it current location.
FrmX = 0
FrmY = 0
EndIf
ReturnNew Point(FrmX, FrmY)
EndFunction
You can use cursor.postion if you grid use screen coordiinates or the E from the control you using on the mouse click event E.location as FRMPoint

with these this you should be able to create a grid formed from points and find the out which tile was clicked.

also you will need this function to use the one above simple really just check to see if a point is within a rectangle

Public Function Is_Within(ByVal Loc1 As Point, ByVal Loc2 As Point, ByVal Height As Integer, ByVal Width As Integer) As Boolean
If Loc1.X >= Loc2.X And Loc1.X <= Loc2.X + Width And Loc1.Y >= Loc2.Y And Loc1.Y <= Loc2.Y + Height Then
Return True
End If

Return False
End Function

 
Last edited:
Thanks!

Thanks! :) I've been looking at the code, at it seems to make sense :)

Anyway, there is no declaration for bounding_rectangle and Tile_Width... What should I declare them as or are they some kind of miss-spelled function?

Thanks for the reply! :)
 
Hi again

I tried to dim Tile_width as integer and set it to 30, to make the tiles 30 pixels wide. Then i realized that bounding_rectangle.width was basically the same as tile_width, so i replaced it with that. I made a test by trying to move a label to the tiles i clicked, but it always ended up in the upper left corner :(

I'm sorry, but i just can't make it work at all...

But i've got the concept of how to do it, and if I find the solution, i will post it here later :)
 
Back
Top