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.
This example computes map of 'occupied' rects from the 2 colour image. The Image is expected to be either black (rgb=0 ) or pure white (rgb=$ffffff). The user can draw circles with the mouse to fill the frame in. The occupied Map is drawn behind it in GREEN and RED blocks in real time.
This is a more updated version of the previous example. The only real difference is that, this one uses built maps where is the original one demonstrates the hands on method. Apart from that they're pretty much the same, with just some tweaks for newer editions of PlayBasic V1.64M and above.
This is another short PlayBasic source code example, this one's demonstrating a selective redrawing method using PlayBasic Maps. The program attempts to reduce the cost per refresh by not redrawing the entire backdrop picture every frame. It does this by tagging the overdrawn zones that need to be restored using a Map. So basically the backdrop picture is first cut up into an Map representation. In this example the block size is 32x by 32y pixels, giving us a level size of (800/32 by 600/32). The demo uses two versions of the level tile map data. One is the original and the second is a version we're going to draw from. We need two since the redraw version gets cleared and altered each frame.
When drawing character objects (in this demo those are the circular blobs), we move the object then convert it's 2d space coordinates back into map coordinates. Then copy from this tile/block area from Original Level to the Refresh Level. We repeat this process for all on screen objects. So after processing our characters, the refresh map contains the parts of the backdrop that need to refreshed (reset) back to it's original state before drawing the next frame. So rather than redraw the entire backdrop picture every time, we can selectively refresh the backdrop. This type of method works really well in 2D games, as it allows the program to lower the cost per-pixel, ironing out the performance for older systems. By older, I mean systems beyond 5 years, closer to 10 years old really
This example lets the user navigate an object through a map. The object is only allowed to move in 4 directions and the object is assumed to be the same size as a map tile.
The path ways through the map must also be a single block in size, so the object fits tightly. Normally this would mean moving through the tunnel would require pixel perfect navigation. To counter this, the movement code takes two approaches. Firstly, there's check to see if the object is able to move in the requested direction. So when the player presses up, we check the tile above, if that's empty they're allowed to move up. If that fails. We fall into a second phase, where we look at the tiles above and the left/right of our current position. If one of those is empty, then we then translate the user up request, into either a left or right movement. Which will slide them into position.
Code built with PB1.64N beta 4 (may need changes for older versions)
This is a quick example of a dirty rectangle style refresh using a tile map. The example allows you to render a 'static' backdrop page with a bunch of the sprites moving over it. The primary objective of such approaches is to reduce the cost per pixel.
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.
PlayBasic Code:
MyMap =NewMap(1);
TileWidth=48
TileHeight=48
Number_of_tiles =5; Loop through and draw a series of randomly coloured Boxes to the screenFor lp=1To 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)Leveltransparent1,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 modedrawgfximmediate
PLayerSize =32; Create an image of a circle (for something to look at)Cls0
Size=PLayerSize/2Circlec Size,Size,Size,1,$00ff00GetImage10,0,0,PLayerSize,PLayerSize
preparefximage10; create a sprite CreateSprite1autocenterspritehandle1,trueSpriteImage1,10spriteCollision1,true; Set collision mode to WorldSpriteCollisionMode1,4SpriteCollisionWorld1,MyCollisionWorld
; Set the Sprites Collision Circle to just smaller than then players; image. In this example the players image is also a circle.SpriteCollisionRadius1,(PlayerSize/2)-2; SpriteCollisionDebug 1,truePositionSprite1,150,150; Create a CameraCreateCamera1DoCaptureToSceneClsSceneCapturedepth100DrawMap MyMap,1,0,0; MOve the Sprite around (8 way movement)
Speed=5
Xspeed=0
Yspeed=0ifLeftKey()then Xspeed=-Speed
ifRightKey()then Xspeed=Speed
ifUpKey()then Yspeed=-Speed
ifDownKey()then Yspeed=Speed
if Xspeed<>0or Yspeed<>0; MOve the sprite, this will auto slide the sprite; off any wall it hits in the Collision World MoveSprite1,xspeed,yspeed
endifDrawAllSprites;POsition the camera over the sprite
Xpos=GetSpriteX(1)-(GetScreenWidth()/2)
Ypos=GetSpriteY(1)-(GetScreenHeight()/2)POsitionCamera1,Xpos,Ypos
; Tell the camera to draw whats in viweDrawcamera1Syncloopspacekey()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..; =============================================================================AcresetConstant 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; =====================================================================Dot0,0
Y=Ypos
For Ylp=0to LevelHeight
Y2=Y+BlockHeight
X=Xpos
TopEdgeState=EdgeSTate_Searching
TopStartXpos=Xpos
BotEdgeState=EdgeSTate_Searching
BotStartXpos=Xpos
For Xlp=0to 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 arrayiF Ylp>0; Grab the tile above our current tile
AboveCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp-1); Check if this tile is transparentif AboveCurrentTile=0; Since there's no tile above then this must be a top; edge of the blockif TopEdgeState=EdgeSTate_Searching
inc TopEdgeState
TopStartXpos=X
endifelseif TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
;' Line TopStartXpos,y,X,yLine x,y,TopStartXpos,y
endifendifEndif; Check the Y position to make sure we don't step off the Level arrayif Ylp<LevelHeight
; Grab the tile above our current tile
BellowCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp+1); Check if this tile is transparentif BellowCurrentTile=0; Since there's no tile above then this must be a top; edge of the blockif BotEdgeState=EdgeSTate_Searching
inc BotEdgeState
BotStartXpos=X
endifelseif BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endifendifendifelse; close edgesif TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
Line x,y,TopStartXpos,y
endifif BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endifendif
X=X+BlockWidth
next
Y=Y+BlockHeight
next; ====================================================================; #2 now we repeat this process, but scanning from Top to Bottom; =====================================================================
X=Xpos
For Xlp=0to LevelWidth
X2=X+BlockWidth
Y=Ypos
LeftEdgeState=EdgeSTate_Searching
LeftStartYpos=Y
RightEdgeState=EdgeSTate_Searching
RightStartYpos=Y
For ylp=0to 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 arrayiF Xlp>0; Grab the tile to the LEFT of our current tile
PreviousTile =PeekLevelTile(Thismap,Thislevel,Xlp-1,Ylp); Check if this tile is transparentif PreviousTile=0; Since there's no tile then this must be the LEFT; edge of the blockif LeftEdgeState=EdgeSTate_Searching
inc LeftEdgeState
LeftStartYpos=y
endifelseif LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endifendifEndif; Check the X position to make sure we don't step off the Level arrayif Xlp<LevelWidth
; Grab the tile above our current tile
NextTile =PeekLevelTile(Thismap,Thislevel,Xlp+1,Ylp); Check if this tile is transparentif NextTile=0; Since there's no tile on the RIGHT then this must be a right; edge of the blockif RightEdgeState=EdgeSTate_Searching
inc RightEdgeState
RightStartYpos=Y
endifelseif RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
; Line x2,RightStartYpos,x2,yLine x2,y,x2,RightStartYpos
endifendifendifelse; close edges (if open)if LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endifif RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
;' Line x2,RightStartYpos,x2,yLine x2,y,x2,RightStartYpos
endifendif
Y=Y+BlockHeight
next
X=X+BlockWidth
nextEndFunction` *=----------------------------------------------------------------=*` -------- Read Map ---------` *=----------------------------------------------------------------=*Function Read_Level(ThisMap,ThisLevel)
Map_Width=readdata()
Map_Height=readdata(); Init the Level array within this Mapcreatelevel ThisMap,ThisLevel,Map_Width+1,Map_Height+1for zlp=0to map_Height-1for xlp=0to map_width-1pokeleveltile ThisMap,Thislevel,xlp,zlp,ReadData()next xlp
next zlp
EndFunction Map_Width,Map_Depth
data32data42data1,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,1data1,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,1data1,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,1data1,0,0,0,0,2,2,2,2,0,2,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,2,0,0,0,2,2,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1data1,0,0,2,2,2,2,0,2,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,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,1data1,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,1data1,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
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.