News:

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

Main Menu

Simple platformer with slopes

Started by programaths, January 23, 2021, 09:50:07 AM

Previous topic - Next topic

programaths

This code does the following:


  • Create a World
  • Create a Map
  • Create a Level
  • Create a tiled background with paralax
  • Convert a map into a World line, with support of slopes
  • Use Restore and Read to load a kevel
  • Query the world

It answers the following:

  • How do I know if my sprite is touching the floor ?
  • How do I detect that my sprite is touching a wall ?
  • How do I create slopes ?
  • How do I implement basic physics with friction/damping ?

The code is copy-paste friendly. That means you can run it without having to make changes.
All sprites are procedural.

PlayBASIC Code: [Select]
; PROJECT : SimpleLevel
; AUTHOR : Programaths
; CREATED : 1/22/2021
; EDITED : 1/23/2021
; ---------------------------------------------------------------------
MyWorld=NewWorld()
MyCamera=NewCamera()
CameraCls MyCamera,on
CameraClsColour MyCamera,RGB(0,0,0)

level1:
data 20,15
data 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,1
data 1,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,1,0,2,3,0,0,2,0,0,1
data 1,0,0,0,0,0,0,0,0,0,1,0,5,4,0,2,4,0,0,1
data 1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,1
data 1,0,1,0,1,1,1,1,1,1,1,0,6,7,0,0,0,0,0,1
data 1,0,1,0,0,1,0,0,0,0,0,0,9,8,0,0,0,0,1,1
data 1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,9,1
data 1,0,1,0,0,1,0,0,0,6,7,0,0,0,2,3,0,0,0,1
data 1,0,1,0,1,1,0,0,0,9,8,0,0,0,5,4,0,0,0,1
data 1,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


MyMap = NewMap(1)
MapDebug MyMap,off
TileWidth=64
TileHeight=64
NumberOfTiles = 10
CreateMapGFX MyMap,TileWidth,TileHeight,NumberOfTiles,RGB(0,0,0)
currentTileImage=NewImage(TileWidth,TileHeight)

; The blocks definition
RenderToImage currentTileImage

; A square block
cls
BoxC 0,0,TileWidth,TileHeight,1,$ffffaa
LightImage currentTileImage,16,16,200,1.0
GetMapBlk MyMap,1,0,0

; A 45° slope down left /|
cls
TriC 0,TileHeight,TileWidth,TileHeight,TileWidth,0,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,2,0,0

; A 45° slope down right |\
cls
TriC 0,0,TileWidth,TileHeight,0,TileHeight,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,3,0,0

; A 45° slope up right |/
cls
TriC 0,0,TileWidth,0,0,TileHeight,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,4,0,0

; A 45° slope up left \|
cls
TriC 0,0,TileWidth,0,TileWidth,TileHeight,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,5,0,0

; A 45° half slope down left /|
cls
TriC 0,TileHeight,TileWidth,TileHeight,TileWidth,TileHeight/2,$ffffaa
LightImage currentTileImage,32,16,200,1.0
GetMapBlk MyMap,6,0,0

; A 45° half slope down right |\
cls
TriC 0,TileHeight,TileWidth,TileHeight,0,TileHeight/2,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,7,0,0

; A 45° half slope up right |/
cls
TriC 0,0,TileWidth,0,0,TileHeight/2,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,8,0,0

; A 45° half slope up left \|
cls
TriC 0,0,TileWidth,0,TileWidth,TileHeight/2,$ffffaa
LightImage currentTileImage,32,32,200,1.0
GetMapBlk MyMap,9,0,0

deleteImage currentTileImage

hs#=0.0
vs#=0.0
grounded=false

restore level1

local w=ReadData()
local h=ReadData()

currentTileImage=NewImage(64,64)
RenderToImage currentTileImage
boxC 0,0,64,64,1,$ffffaa
LightImage currentTileImage,10,10,255,0.5
background=NewImage((w+2)*64,(h+2)*64)
RenderToImage background
GridImage currentTileImage,0,0,h+2,w+2,1

deleteImage currentTileImage

MyLevel=NewLevel(MyMap,w,h)
LevelTransparent MyMap,MyLevel,0

RenderToScreen

CaptureToWorld MyWorld

ballImg=CreateBallImage(64)
spr = NewSprite(150,150,ballImg)
AutoCenterSpriteHandle spr,true
SpriteCollisionMode spr, 4
SpriteCollisionWorld spr, MyWorld
SpriteCollisionRadius spr,24
SpriteCollision 1,on
SpriteCollisionClass 1,1

for i=0 to h-1
for j=0 to w-1
PokeLevelTile MyMap,MyLevel,j,i,readData()
next j
next i

createWordLines(MyMap,MyLevel)
PartitionWorld MyWorld, 32

drawgfximmediate

do

CaptureToScene
ClsScene

CaptureDepth 4
drawImage background, GetSpriteX(spr)*0.9-GetScreenWidth()/2,GetSpriteY(spr)*0.9-GetScreenHeight()/2,0
Login required to view complete source code


kevin


Excellent...  nice use of various virtually unknown commands too







stevmjon

nice demo. a good one for beginners to learn from.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.