News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

getting the tile that was hit

Started by RaverDave, December 11, 2005, 03:23:53 AM

Previous topic - Next topic

RaverDave

Hi @ll..

Ok I am fiddling around finally with playbasic!
Now then I changed the code of the basic collision example to read in data to build the level, what i want now is to find out which tile within the map the player has collided with,and to delete that tile from the map level( think arknoid! )..here is the code i have so far

PlayBASIC Code: [Select]
  OpenScreen 640,480,32,2 
SetFPS 0
Mouse Off
ScreenVsync On

PlayerX=50
PlayerY=430
PlayerWidth =10
PlayerHeight =10
Speed=4
MyMap = GetFreeMap()
CreateMap MyMap, 1

TileWidth=32
TileHeight=16
Number_of_tiles = 5

; Loop through and draw a series of randomly coloured Boxes to the screen
For lp=1 To Number_of_tiles
xpos=lp*tileWidth
BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()
Next

; Grab the box graphics that we've just drawn as image.
TempImage=GetFreeImage()
GetImage TempImage,0,0,xpos+TIleWidth,Tileheight



RemStart
So far, we've created our Map and Manually Created an image containing some
coloured blocks. Now it's time to import this image into our Map for use.

When we have map block graphics stored in an image, we import them using
the MakeMapGFX command. This command requires the Map you wish to store these
blocks in, the source Image to import, the Tile Width, the Tile Height, number
of tiles to import and finally the transparent colour (if any) to use for
these tiles.
RemEnd


; Import this image into our previously defined Map.
MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)



RemStart
Now we have a Map that has a set of tiles ready for use in it, so the next
thing we need to is create a level. This is done with the CREATELEVEL command.
Which requires two things, it needs the know within what MAP this level should
be created, and what Level Index it should for this level.


RemEnd


; Create level 1 in MyMAP, 100 tile across and 200 tiles down.
CreateLevel MyMap,1, 100,200



RemStart
We're just about ready now to start drawing, but first we need to fill the level
with what tiles it should draw. In this example, the following code will randomly fill
the level. Normally you would use a level editing program like PLAY MAPPER for
such things. But your welcome to do it manually if you like.
RemEnd


For Ylp=0 To 12
For Xlp=0 To 19
t=ReadData()
If t>0
PokeLevelTile MyMap,1,Xlp,ylp,t
EndIf
Next xlp
Next ylp

Data 1,1,1,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0,0,0
Data 1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0
Data 1,1,1,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0
Data 1,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0
Data 1,0,0,0,1,1,1,0,1,0,1,0,0,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0
Data 1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0
; Draw the Level to the screen at Xpos 0 and Ypos 50


; Show the user te screen and wait for a key to be pressed
Repeat



DrawMap MyMap,1,0,0
; Store the players possible new position
PLayerNewX=PlayerX
PLayerNewY=PlayerY

; Handle the players Movement
If UpKey() Then PlayerNewY=PLayerY-Speed
If DownKey() Then PlayerNewY=PLayerY+Speed
If LeftKey() Then PlayerNewX=PLayerX-Speed
If RightKey() Then PlayerNewX=PLayerX+Speed



; ==========================
; Handle Collision
; ===========================

If CheckMapImpact(MyMap,1,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)
; If there was impact with the map, then read our
; new position back.
PlayerX=GetMapImpactX()
PlayerY=GetMapImpactY()

; Check what side the impact occured upon
; and display a message

If GetMapImpactLeft()
Print "Impact occured on the LEFT side"
EndIf

If GetMapImpactRight()
Print "Impact occured on the RIGHT side"
EndIf

If GetMapImpactTop()
Print "Impact occured on the TOP side"
EndIf

If GetMapImpactBot()
Print "Impact occured on the Bottom side"
EndIf


Else
; No Collision, Then set the current players
; position to it's new position
PlayerX=PlayerNewX
PlayerY=PlayerNewY
EndIf


X2=playerX+PlayerWidth
Login required to view complete source code



stef

Hi!

Interesting and good idea!

(In your code you should use "Cls 0" after "repeat"or "text 0,0" instead of "print")

Greetings
stef

RaverDave

yeh,well thats no big deal,in fact that print code is coming out,its of no use really,i just want this collision working,then i will get the ball(square ball!!) bouncing around by itself destroying the tiles

kevin

#3
When a collision occurs you just peek out the block(s as there might be more than 1 impact)  

Which is pretty much what the bellow changes do.  Although there's a little error with it.  I'll give you hint, it's the logic when the tile is to be deleted..  currently were deleting the tile on the same level as the block.  but that's not always going to be the case.


PlayBASIC Code: [Select]
; OpenScreen 640,480,32,2
;SetFPS 0
;Mouse Off
;ScreenVsync On

PlayerX=50
PlayerY=430
PlayerWidth =10
PlayerHeight =10
Speed=4
MyMap = GetFreeMap()
CreateMap MyMap, 1

TileWidth=32
TileHeight=16
Number_of_tiles = 5

; Loop through and draw a series of randomly coloured Boxes to the screen
For lp=1 To Number_of_tiles
xpos=lp*tileWidth
BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()
Next

; Grab the box graphics that we've just drawn as image.
TempImage=GetFreeImage()
GetImage TempImage,0,0,xpos+TIleWidth,Tileheight



RemStart
So far, we've created our Map and Manually Created an image containing some
coloured blocks. Now it's time to import this image into our Map for use.

When we have map block graphics stored in an image, we import them using
the MakeMapGFX command. This command requires the Map you wish to store these
blocks in, the source Image to import, the Tile Width, the Tile Height, number
of tiles to import and finally the transparent colour (if any) to use for
these tiles.
RemEnd


; Import this image into our previously defined Map.
MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)



RemStart
Now we have a Map that has a set of tiles ready for use in it, so the next
thing we need to is create a level. This is done with the CREATELEVEL command.
Which requires two things, it needs the know within what MAP this level should
be created, and what Level Index it should for this level.


RemEnd


; Create level 1 in MyMAP, 100 tile across and 200 tiles down.
CreateLevel MyMap,1, 100,200



For Ylp=0 To 12
For Xlp=0 To 19
t=ReadData()
If t>0
PokeLevelTile MyMap,1,Xlp,ylp,t
EndIf
Next xlp
Next ylp

Data 1,1,1,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0,0,0
Data 1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0
Data 1,1,1,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0
Data 1,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0
Data 1,0,0,0,1,1,1,0,1,0,1,0,0,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0
Data 1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0
; Draw the Level to the screen at Xpos 0 and Ypos 50


; Show the user te screen and wait for a key to be pressed
Repeat



DrawMap MyMap,1,0,0



PLayerX,PLayerY=Handle_player(PLayerX,PLayerY,PLayerWIdth,PlayerHeight,Speed,MyMap,1)



X2=playerX+PlayerWidth
Y2=playerY+PlayerHeight
Box playerX,playerY,x2,y2,1

Rem Circle playerX,playerY,10,1
v=ScanCode()
If v=16
quit=1
endif

Sync
Until quit=1






Function Handle_player(PLayerX,PLayerY,PLayerWIdth,PlayerHeight,Speed,ThisMap,ThisLevel)

; Store the players possible new position
PLayerNewX=PlayerX
PLayerNewY=PlayerY

; Handle the players Movement
If UpKey() Then PlayerNewY=PLayerY-Speed
If DownKey() Then PlayerNewY=PLayerY+Speed
If LeftKey() Then PlayerNewX=PLayerX-Speed
If RightKey() Then PlayerNewX=PLayerX+Speed

; ==========================
; Handle Collision
; ===========================
If CheckMapImpact(thisMap,ThisLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)

; If there was impact with the map, then read our
; new position back.
PlayerX=GetMapImpactX()
PlayerY=GetMapImpactY()

TileWidth=GetMapBlockWidth(ThisMap)
TileHeight=GetMapBlockHeight(ThisMap)

TileX=PlayerX/TileWidth
TileY=PlayerY/Tileheight

; Check what side the impact occured upon
; and display a message

If GetMapImpactLeft()
Login required to view complete source code



RaverDave

If CheckMapImpact& #40;thisMap,ThisLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHe
ight)  

this line errors,is it coz you are using 'thismap' instead of 'mymap'??

RaverDave

#5
errr.in fact it errors all over that line :)
could you do me a favour Kev and get the collisions going for me with some nice comments to learn from?

stef

hi!

 If CheckMapImpact(thisMap,ThisLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)

this works better (but crashes on left window border=

stef

RaverDave

#7
hmmm,all i get is a compile error...

Error number 23

Irregular brackets : missing closing


Very strange as checking the syntax it seems correct

stef

#8
hi!

If CheckMapImpact(thisMap,ThisLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWid
th,PlayerHeight)

do all in one line!!



that's a strange copy and paste or whatever error!!


If CheckMapImpact(thisMap,ThisLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)


it replaced " (" with "&  #40;" !!!!!


stef

RaverDave

#9
This is getting weird,I still get the same error with the brackets still in place..


I have:

If Checkmapimpact(thismap,thislevel,playerX,playerY,playernewX,playernewY,Playerwidth,playerheight)



Surely this should be ok??

RaverDave

Ah!! sussed it out,It didnt like the command splitting up onto another line!
Ok thanks Alot, there is a slight problem in that it doesnt delete a tile at a certain point on the tile,but i think thats coz the block is much smaller than the tile,least I presume!

stef

hi

your line works on my computer with PB v1.89m

RaverDave

#12
Actually this is odd,and some point it stops the ball but doesnt delete the tile nearest to the ball,but one above it..how do i get around this?
If you mess around long enough you will see..

In fact an easy way to see the problem is approach a tile from the bottom upwards,but offset the block(ball) from the tile to the left so only half the block approaches it..errrr...like this..




          |----------------|
          |                    |
          |----------------|

      |-----|
      |-----|    <============== Move Upwards!


RaverDave

My Oh My! My artistic example ruined by weird phantom tabs!! Anyhow you get the idea!

kevin

#14
I mentioned this above with the original post,  I was hoping once i'd shown the method you could correct the logic to handle mis-aligned cases.

Rather than just dump the working solution, i'll post a little something about why it occurs and how to fix it later.