|
This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey.
Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.
Found #1 item in Worlds category by Kevin Picone
Convert Map To Collision Worlds
|
By: Kevin Picone Added: August 31st, 2005
|
Category: All,Maps,Collision,Worlds
|
This code creates a game level with a series of randomly colored boxes on the screen, which are drawn as an image. The image is then imported into a map called "MyMap." The code then reads in a level from data statements, makes the level transparent, and draws it to the screen. The code also creates a collision world from the map, and sets up a sprite and camera.
The sprite can be moved around the screen using arrow keys, and will automatically slide off walls it hits in the collision world. The camera is positioned over the sprite, and will draw what is in view. The game will continue to loop until the space key is pressed.
MyMap =NewMap(1)
;
TileWidth=48
TileHeight=48
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
; Import this image into our previously defined Map.
MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)
; =======================================================
; Read the Level in from the data statements bellow
; =======================================================
Read_Level(1,1)
Leveltransparent 1,1,0
; Draw the Level to the screen at Xpos 0 and Ypos 0
Xpos=0
Ypos=0
; Create the Collision world
MyCollisionWorld=NewWorld()
CaptureToWorld MyCollisionWorld
MakeCollisionWorldFromMap(MyMap,1,xpos,ypos)
PartitionWorld MyCollisionWorld,64
; restore drawing back to immediate mode
drawgfximmediate
PLayerSize = 32
; Create an image of a circle (for something to look at)
Cls 0
Size=PLayerSize/2
Circlec Size,Size,Size,1,$00ff00
GetImage 10,0,0,PLayerSize,PLayerSize
preparefximage 10
; create a sprite
CreateSprite 1
autocenterspritehandle 1,true
SpriteImage 1,10
spriteCollision 1,true
; Set collision mode to World
SpriteCollisionMode 1,4
SpriteCollisionWorld 1,MyCollisionWorld
; Set the Sprites Collision Circle to just smaller than then players
; image. In this example the players image is also a circle.
SpriteCollisionRadius 1,(PlayerSize/2)-2
; SpriteCollisionDebug 1,true
PositionSprite 1,150,150
; Create a Camera
CreateCamera 1
Do
CaptureToScene
ClsScene
Capturedepth 100
DrawMap MyMap,1,0,0
; MOve the Sprite around (8 way movement)
Speed=5
Xspeed=0
Yspeed=0
if LeftKey() then Xspeed=-Speed
if RightKey() then Xspeed=Speed
if UpKey() then Yspeed=-Speed
if DownKey() then Yspeed=Speed
if Xspeed<>0 or Yspeed<>0
; MOve the sprite, this will auto slide the sprite
; off any wall it hits in the Collision World
MoveSprite 1,xspeed,yspeed
endif
DrawAllSprites
;POsition the camera over the sprite
Xpos=GetSpriteX(1)-(GetScreenWidth()/2)
Ypos=GetSpriteY(1)-(GetScreenHeight()/2)
POsitionCamera 1,Xpos,Ypos
; Tell the camera to draw whats in viwe
Drawcamera 1
Sync
loop spacekey()
end
; =============================================================================
; This Function converts a map Level into collision world, which is nothing
; more than drawing lines around the bounding area around of the tiles. It
; considers tile zero to be transparent (which is soft). Everything else is
; think is hard
;
; once the map is a collision world you can run ray interest on it, sliding
; collision among other things..
; =============================================================================
Acreset
Constant EdgeState_Searching =ac(1)
Constant EdgeState_FoundStart =ac(1)
Function MakeCollisionWorldFromMap(thisMap,Thislevel,xpos,ypos)
LevelWidth =GetLevelWidth(Thismap,thislevel)
LevelHeight =GetLevelHeight(Thismap,thislevel)
BlockWidth=GetMapBLockWidth(ThisMap)
BlockHeight=GetMapBLockHeight(ThisMap)
; ====================================================================
; #1 first Pass over the level looking for blocks running Left to Right
; =====================================================================
Dot 0,0
Y=Ypos
For Ylp=0 to LevelHeight
Y2=Y+BlockHeight
X=Xpos
TopEdgeState=EdgeSTate_Searching
TopStartXpos=Xpos
BotEdgeState=EdgeSTate_Searching
BotStartXpos=Xpos
For Xlp=0 to LevelWidth
CurrentTile =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)
; Check if this tile is not our transparent tile zero
If CurrentTile<>0
; Check the Y position to make sure we don't step off the Level array
iF Ylp>0
; Grab the tile above our current tile
AboveCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp-1)
; Check if this tile is transparent
if AboveCurrentTile=0
; Since there's no tile above then this must be a top
; edge of the block
if TopEdgeState=EdgeSTate_Searching
inc TopEdgeState
TopStartXpos=X
endif
else
if TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
;' Line TopStartXpos,y,X,y
Line x,y,TopStartXpos,y
endif
endif
Endif
; Check the Y position to make sure we don't step off the Level array
if Ylp<LevelHeight
; Grab the tile above our current tile
BellowCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp+1)
; Check if this tile is transparent
if BellowCurrentTile=0
; Since there's no tile above then this must be a top
; edge of the block
if BotEdgeState=EdgeSTate_Searching
inc BotEdgeState
BotStartXpos=X
endif
else
if BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endif
endif
endif
else
; close edges
if TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
Line x,y,TopStartXpos,y
endif
if BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endif
endif
X=X+BlockWidth
next
Y=Y+BlockHeight
next
; ====================================================================
; #2 now we repeat this process, but scanning from Top to Bottom
; =====================================================================
X=Xpos
For Xlp=0 to LevelWidth
X2=X+BlockWidth
Y=Ypos
LeftEdgeState=EdgeSTate_Searching
LeftStartYpos=Y
RightEdgeState=EdgeSTate_Searching
RightStartYpos=Y
For ylp=0 to LevelHeight
CurrentTile =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)
; Check if this tile is not our transparent tile zero
If CurrentTile<>0
; Check the Y position to make sure we don't step off the Level array
iF Xlp>0
; Grab the tile to the LEFT of our current tile
PreviousTile =PeekLevelTile(Thismap,Thislevel,Xlp-1,Ylp)
; Check if this tile is transparent
if PreviousTile=0
; Since there's no tile then this must be the LEFT
; edge of the block
if LeftEdgeState=EdgeSTate_Searching
inc LeftEdgeState
LeftStartYpos=y
endif
else
if LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endif
endif
Endif
; Check the X position to make sure we don't step off the Level array
if Xlp<LevelWidth
; Grab the tile above our current tile
NextTile =PeekLevelTile(Thismap,Thislevel,Xlp+1,Ylp)
; Check if this tile is transparent
if NextTile=0
; Since there's no tile on the RIGHT then this must be a right
; edge of the block
if RightEdgeState=EdgeSTate_Searching
inc RightEdgeState
RightStartYpos=Y
endif
else
if RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
; Line x2,RightStartYpos,x2,y
Line x2,y,x2,RightStartYpos
endif
endif
endif
else
; close edges (if open)
if LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endif
if RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
;' Line x2,RightStartYpos,x2,y
Line x2,y,x2,RightStartYpos
endif
endif
Y=Y+BlockHeight
next
X=X+BlockWidth
next
EndFunction
` *=----------------------------------------------------------------=*
` -------- Read Map ---------
` *=----------------------------------------------------------------=*
Function Read_Level(ThisMap,ThisLevel)
Map_Width=readdata()
Map_Height=readdata()
; Init the Level array within this Map
createlevel ThisMap,ThisLevel,Map_Width+1,Map_Height+1
for zlp=0 to map_Height-1
for xlp=0 to map_width-1
pokeleveltile ThisMap,Thislevel,xlp,zlp,ReadData()
next xlp
next zlp
EndFunction Map_Width,Map_Depth
data 32
data 42
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,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0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1
data 1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,1
data 1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1
data 1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
data 1,0,1,0,0,1,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1
data 1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
|
|
Viewing Page [1] of [0]
Want More Source Codes?:
Release Type:
The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.
|
|