data
BlitzPlus Forums/BlitzPlus Programming/data
| ||
hi, i keep seeing people use the data command to create a tile-like look on their game. Like on platform games where they have different numbers translate to a different image. example: data 1,1,1,1,1 data 1,0,0,0,1 data 1,0,0,0,1 data 1,0,0,0,1 data 1,1,1,1,1 this would create like an outline sort of thing. anyway if you understand a thing i tried to explain please help me out and show me how to do it. thanks in advance. |
| ||
I suppose this would be the most basic way to do that: (the drawing is really small) |
| ||
Like b32 said only bigger so you don't have to squint :)sw = 800 sh = 600 Graphics sw,sh,32,2 SetBuffer BackBuffer() ;use 'box' data Restore box ;change these for different size boxes boxX = 48 boxY = 48 ;pixel spacing between boxes ;change to 0 for no spacing spacing = 2 For y = 0 To 4 For x = 0 To 4 ;read a color (i.e. a number 1-4) Read colorData ;Define your color scheme here Select colorData Case 0: Color 255,255,255 Case 1: Color 255, 0, 0 Case 2: Color 0,255, 0 Case 3: Color 0, 0,255 Case 4: Color 255,255, 0 Default: Color 255,255,255 End Select ;Rect x*(boxX + spacing ), y*(boxY + spacing ), boxX, boxY, True ;just add an offset to move the tiles to where ever you want on screen (centered here) offSetX = (sw - (5 * (boxX + spacing))) /2 offSetY = (sh - (5 * (boxY + spacing))) /2 Rect x*(boxX + spacing ) + offSetX, y*(boxY + spacing ) + offSetY, boxX, boxY, True Next Next Text 5,560,"Left click to exit" Flip WaitMouse() ;wait for a mouse click to exit End .box Data 4,1,1,1,1 Data 4,0,0,0,2 Data 4,0,0,0,2 Data 4,0,0,0,2 Data 3,3,3,3,2 |
| ||
This may interest you: http://www.blitzbasic.com/codearcs/codearcs.php?code=1987 you need to add the setcursorpos command in the user32 decls. |
| ||
I'd avoid data statements and use one of the following: - a binary file created by a map editor that goes into a 2d array. - a set of functions that *create* high-level things like platforms, boxes etc. Then a map is a matter of performing some of those functions. Again going into a 2d array. Data can be sufficient for 4 values orso, but when your map has like 256 different tiles, you aren't going to love data statements. In addition: your signature is truly over-sized! |
| ||
CS_TBL is absolutely correct, in that large, complex maps are best stored in binary files on disk. Data statements are good for learning how to implement those binary files. Using data statements to change a small detail just change one of the values; instant gratification. Using binary files you'll need to use a small routine or better yet a map editor to update the map file. Binary files are best used when you already know what you're doing, data statements can be useful while learning but you'll soon outgrow them. |
| ||
Here's a small example of my 2nd point however, using functions to create a map. Note that I've been using a bank, but it's easy to see how to deal with it in the Showmap and Maprect functions.Graphics 640,480 map=CreateMap(32,24) Maprect map,0,0,32,24,255,100 Maprect map,1,1,30,22,32,100 For t=1 To 9 Maprect map,1,1,30,22,32+(t*12),40 Next Maprect map,0,4,8,2,255,100 Maprect map,8,12,8,4,255,100 Maprect map,24,16,6,4,255,100 Showmap Map Flip Repeat Until KeyDown(1) ; Esc End Function Showmap(map) mapw=PeekShort(map,0) maph=PeekShort(map,2) For y=0 To maph-1 For x=0 To mapw-1 v=PeekByte(map,4+x+mapw*y) ; 'v' here contains the number of the tile (0..255 in this case) Color v,v,v Rect x*8,y*8,8,8,True Next Next End Function Function CreateMap(w,h) bank=CreateBank(w*h+4) PokeShort bank,0,w PokeShort bank,2,h Return bank End Function Function Maprect(map,px,py,w,h,v,chance) mapw=PeekShort(map,0) maph=PeekShort(map,2) If w<1 w=1 If h<1 h=1 For y=0 To h-1 For x=0 To w-1 If InBox(x+px,y+py,0,0,mapw,maph) If Rnd(100)<=chance PokeByte map,4+(px+x+mapw*(py+y)),v EndIf EndIf Next Next End Function Function InBox(px,py,x,y,w,h) If px>=x And px<(x+w) And py>=y And py<(y+h) Return 1 Else Return 0 End Function |
| ||
Your InBox function... You might want to look at rectsoverlap. |
| ||
Yeah, well.. :P In a number of cases, making a small function takes less time than finding something appropriate in the help. ^_^ Btw, this one works for 1 pixel, not for a rect. |
| ||
I ran into a problem. When i put cls in the loop the tiles aren't drawn. |
| ||
Here's a loop in which to draw things: ------------------- CLS DrawAllYourStuff FLIP ------------------- |
| ||
i tried that but it says it ran out of data because i put the whole for...next loop in the main loop. I know im doing something wrong but i don't know how to fix it. |
| ||
The standard forum answer: show us the code! Oh wait, don't tell me, I know.. :P You have your 'read' function inside the mainloop! Wanna bet? :P |
| ||
here is my code. if i don't put the Read command in the main loop where do i put it |
| ||
never mind i got it!! finally!! thanks for all the help! |
| ||
hold on i thought i had it but it's not working again. |
| ||
You could also put 'Restore' right before the first For..Next loop that reads the data. |
| ||
Could someone post an example of how to use binary files with the code that I posted, or give an example that shows its use instead of data. |
| ||
Sure .. at some point, you read the map from the Data. Right after that, you can put this map in a file. I made a 'short version' that shows this: |
| ||
the images aren't drawing can you guys show me how to do it |
| ||
Type bullet Field x#,y# Field xv#,yv# Field frame End Type Global bullet.bullet Here's an advice: stick to the common rule to call your types T<name> and the variable you're going to use bullet, so in this case: TBullet and bullet. (or Bullet, whatever you want) This makes the difference between a type definition and an instance more clear. Select filled Case 1 DrawImage cl1,x*32,y*32 Case 2 DrawImage cl2,x*32,y*32 Case 3 DrawImage pl1,x*32,y*32 Case 4 DrawImage pl7,x*32,y*32 Case 5 DrawImage pl6,x*32,y*32 Case 6 DrawImage wl2,x*32,y*32 Case 6 DrawImage vn1,x*32,y*32 ; was this second case 6 supposed to be right?? End Select You may want to consider arrays. Like Dim picture(10) can hold 10 images. Then in your draw routine you don't need to Select-Case on the value, you just draw the right picture from the array according to the mapvalue. So if you mapvalue is 4, then: DrawImage picture(mapvalue),x*32,y*32 draws image 4. This will especially be practical when you have 100+ tiles.. :P In any case, when you're done experimenting with 'data', really consider working with binaries. Did you try/test my map-function example? |
| ||
As for your code, the images are drawing, so I think they are not loaded. Try putting them in the same folder as your code first, as a test. |
| ||
everything is loaded fine. my problem is that i can't figure out what part of the code to in the main loop so they draw. i know they're loaded because when i remove the Cls command the show but when i put it back in they don't because the drawimage commands aren't in the main loop. |
| ||
Are we still talking about the code you posted above ? I've included the Cls command, and placed the character in the middle of the screen: I replaced LoadImage/LoadAnimImage with a custom function, for testing. If you run it, you can see the blue thingie walk around the red thingies world. If you comment the functions out, it should load the original image files again. |
| ||
i finally got it every one. My mistake was not putting the restore command in the loop. thanks for all the help! |