How do i code a level editor?

Blitz3D Forums/Blitz3D Beginners Area/How do i code a level editor?

767pilot(Posted 2004) [#1]
How do you go about designing and coding a level editor for a 2d game?

I want to make a level that has walls and if the character hits the wall then he doesnt go through it. The wall will be a bitmap image, say 40,10 pixels. I dont want to use the 'imagecollide' function as when i add more gfx to the level it would make the code sloppy.

I saw examples of 'data' and 'read' commands but cannot find a clear tutorial regarding this and how to implement it into a game

thanks for reading
graham


WolRon(Posted 2004) [#2]
How about using this.


DATA statements are fine for smaller quantities of data, but if the amount of data becomes too great, you should consider using seperate files.

You have several options available to you to manage your level data. You can simply use DATA statements as you suggested or you can use Arrays (work well for 2D boards) or even Arrays of Types. You may even try using Banks to contain your data if it doesn't fit well into Arrays or Types.

You can use the ImagesCollide command if you want to check for collisions, but with a 2D game set up like a grid, you actually don't even need to use the ImagesCollide command to check for collisions between your characters and the inanimate objects of your level. You can just check if the X,Y value of the character is about to overlap a position taken up by a 'nonwalkable' tile.


767pilot(Posted 2004) [#3]
How would you set up a 2d grid and check for a collision? Say if my character was 80x80 pixels, the wall was 10,60 and an enemy 20,20; how would you detect what was at a certain position on the screen? How would you know if it was an enemy or a wall? Whats the best way to do this?


eg Create an array ie Dim world(30,30). What would the 30 represent in terms of size? It wouldnt be pixels would it?

I have some code to move a man (with 12 frames of anim) around the screen and a bat flying up and down. I want to make a big level but want to create a level editor as it would be a lot easier to design the level. Ive seen some example code but its not step by step and i cant figure out the reasons behind it.

Many thanks for any help i may receive !!


WolRon(Posted 2004) [#4]
Well, you could do it a pratically infinite number of different ways, it just depends on what suits your situation best.

Taking your example of:
a character that's 80x80 pixels
wall that's 10x60 pixels
enemy that's 20x20 pixels

Looks like the number 10 evenly divides into all of those images, so a grid that has elements that each represent a 10x10 pixel square could be a good choice.

Assuming your game was on a 640x480 screen, that would require an Array with 64 x 48 elements (3072).
Dim Board(64, 48)
Of course, you aren't limited to 10x10 squares. You could use 5x5 squares to gain more 'resolution' to your game if you desired, but the memory requirements would be 4 times as much {12,288 elements}...
Dim Board(128, 96)

To check if your character collided with an obstacle, you would perform a check between the characters X,Y coordinate and whatever 'block' would be behind the character.

For example, assume that you have a 64x48 grid (made up of 10x10 squares).
Assume that the character is at the location 118, 30.
Assume that the grid coordinate 19,3 is empty.
Assume that the grid coordinate 20,3 is a wall.
If you want to move the character 1 pixel to the right (location 119) then check if grid 19,3
Floor((119+80)/10)
If Board(Floor((PlayerX+80)/10), Floor((PlayerY)/10)) <> wall Then ...
is OK to move to. Since it is, allow the move.

If you want to move the character 1 more pixel to the right (location 120) then check if grid 20,3
Floor((120+80)/10)
is OK to move to. Since it isn't, don't allow the move to occur.

How it works:
Floor removes any decimals left from the division
119 is the location you want to check
80 is the width of your character (since you are moving right, you need to check the right side)
10 is the width of each grid space


767pilot(Posted 2004) [#5]
ah, becoming a bit clearer now, thanks

how would you tell if its a wall or an enemy at point (x,x)?

(tried the editor you suggested earlier in post but prefer to code my own as i am new to games design etc, also because i dont understand it or how to implement in blitz :) )


WolRon(Posted 2004) [#6]
I wasn't finished updating my post, reread it.


767pilot(Posted 2004) [#7]
thanks WolRon, will try to get this clearer in my head tomorrow and start to draft something on paper!

appreciate your help with this


WolRon(Posted 2004) [#8]
Updated it a bit more...
By the way, you should consider using that Map Editor I suggested. It will help you a great deal later on. If you have problems with it, ask me for assistance, or MrCredo.


767pilot(Posted 2004) [#9]
ok, will give it a go, only concern really is how to import it and use with blitz

going to implement the code for the collision checking you gave earlier, will worry about the level editor later in week

thanks again


RiK(Posted 2004) [#10]
Try taking a look at the current community project. You could learn a lot from their editor code.


767pilot(Posted 2004) [#11]
whats the link please?


RiK(Posted 2004) [#12]
Oops, sorry!

Here you go: http://www.blitzbasic.com/Community/posts.php?topic=38223