hope this helps, knocked this up in about half an hour (still new to AGK), it uses my own hitbox functions, in practice you'd apply sprites to the hit box coords etc.
Paste this into a new project and it should run
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "plat" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 320, 256 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//─────────────────────────────────────────────────────────────────────────────────────────────────
global col_red as integer : col_red = MakeColor(255,0,0)
global col_grn as integer : col_grn = MakeColor(0,255,0)
#constant ROWS = 10
#constant COLS = 8
#constant BLOCKSIZE = 32
//─────────────────────────────────────────────────────────────────────────────────────────────────
// Create a level, 1 = blocks, 2 = player
level_data as integer[]
level_data = [ 1,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,0,0,0,
1,1,1,0,0,0,0,0,1,1,
1,0,0,0,0,0,0,0,0,0,
1,0,2,0,0,1,1,0,0,0,
1,1,1,1,1,1,1,1,1,1]
// Loop threw level data and make blocks and position the player
For y = 0 To COLS-1
For x = 0 To ROWS-1
If level_data[x+(y*ROWS)] = 1 Then AddBlock(x, y)
If level_data[x+(y*ROWS)] = 2 Then Player = AddPlayer(x, y)
Next x
Next y
//─────────────────────────────────────────────────────────────────────────────────────────────────
// Block functions and code
Global Blocks as TBlock[]
Type TBlock
x as float
y as float
hitbox as THitBox
EndType
// Create a block
Function AddBlock(x as integer, y as integer)
local a as TBlock
a.x = x * BLOCKSIZE
a.y = y * BLOCKSIZE
a.hitbox = rkHitBoxMake(BLOCKSIZE, BLOCKSIZE)
rkHitBoxSetPosition(a.hitbox, a.x, a.y)
Blocks.insert(a)
EndFunction
// Draw
Function DrawBlocks()
For a = 0 To Blocks.Length
rkHitBoxDraw(Blocks[a].hitbox, col_red)
Next
EndFunction
//─────────────────────────────────────────────────────────────────────────────────────────────────
// Player functions and code
Global Player as TPlayer
Type TPlayer
x as float
y as float
width as float
height as float
hitbox as THitBox
gravity as float
yVelocity as float
xVelocity as Float
EndType
// Add a player and configure
Function AddPlayer(x as integer, y as integer)
local a as TPlayer
a.x = x * BLOCKSIZE
a.y = y * BLOCKSIZE
a.gravity = 0.4
a.xVelocity = 3
a.width = BLOCKSIZE / 2
a.height = BLOCKSIZE / 2
a.hitbox = rkHitBoxMake(a.width, a.height)
rkHitBoxSetPosition(a.hitbox, a.x, a.y)
EndFunction a
// Draw
Function DrawPlayer()
rkHitBoxDraw(Player.hitbox, col_grn)
EndFunction
// Update & control player
Function UpdatePlayer()
local onfloor as integer
// Apply contant downward force
Player.y = Player.y + Player.yVelocity
Player.yVelocity = Player.yVelocity + Player.gravity
if Player.yVelocity > 6.5 then Player.yVelocity = 6.5
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
// Check for ceiling collisions
If Player.yVelocity<0.0
For a = 0 To Blocks.length
If rkHitBoxIntersects(Player.hitbox, Blocks[a].hitbox)
Player.y = Player.y + rkHitBoxGetEncroachTop(Player.hitbox, Blocks[a].hitbox)
Player.yVelocity = 1
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
exit
Endif
Next a
else
// Check for floor collisions
onfloor = 0
For a = 0 To Blocks.length
If rkHitBoxIntersects(Player.hitbox, Blocks[a].hitbox)
Player.y = Player.y - rkHitBoxGetEncroachBottom(Player.hitbox, Blocks[a].hitbox)
Player.yVelocity = 1
onfloor = 1
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
exit
Endif
Next a
Endif
// Jump with space bar
If GetRawKeyPressed(32) and onfloor
Player.yVelocity = -8.5
Endif
// Move left and right
if GetRawKeyState(39) `Right
Player.x = Player.x + Player.xVelocity
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
For a = 0 To Blocks.length
If rkHitBoxIntersects(Player.hitbox, Blocks[a].hitbox)
Player.x = Player.x - rkHitBoxGetEncroachRight(Player.hitbox, Blocks[a].hitbox)
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
exit
Endif
Next a
elseif GetRawKeyState(37) `Left
Player.x = Player.x - Player.xVelocity
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
For a = 0 To Blocks.length
If rkHitBoxIntersects(Player.hitbox, Blocks[a].hitbox)
Player.x = Player.x + rkHitBoxGetEncroachLeft(Player.hitbox, Blocks[a].hitbox)
rkHitBoxSetPosition(Player.hitbox,Player.x,Player.y)
exit
Endif
Next a
Endif
EndFunction
//╭─────────────────────────────────────────────────────────────────────────────────────────────────╮
//| MAIN LOOP |
//╰─────────────────────────────────────────────────────────────────────────────────────────────────╯
do
DrawPlayer()
DrawBlocks()
UpdatePlayer()
Sync()
loop
//╭─────────────────────────────────────────────────────────────────────────────────────────────────╮
//| Hitbox functions - hitboxes |
//╰─────────────────────────────────────────────────────────────────────────────────────────────────╯
Type THitBox
x as float
y as float
width as float
height as float
x_offset as float
y_offset as float
EndType
// Draw hitbox
Function rkHitBoxDraw(a ref as THitBox, col as integer)
drawbox(rkHitBoxGetX(a),rkHitBoxGetY(a),rkHitBoxGetX(a)+rkHitBoxGetWidth(a),rkHitBoxGetY(a)+rkHitBoxGetHeight(a),col,col,col,col,0)
drawline(rkHitBoxGetX(a),rkHitBoxGetY(a),rkHitBoxGetX(a)+rkHitBoxGetWidth(a),rkHitBoxGetY(a)+rkHitBoxGetHeight(a),col,col)
drawline(rkHitBoxGetX(a)+rkHitBoxGetWidth(a),rkHitBoxGetY(a),rkHitBoxGetX(a),rkHitBoxGetY(a)+rkHitBoxGetHeight(a),col,col)
Endfunction
// Apply an offset value that added when SetPosition is used
Function rkHitBoxSetOffsetX(a ref as THitBox,value as float)
a.x_offset = value
Endfunction
// Apply an offset value that added when SetPosition is used
Function rkHitBoxSetOffsetY(a ref as THitBox,value as float)
a.y_offset = value
Endfunction
// Return a hitbox object
Function rkHitBoxMake(iWidth as Float, iHeight as Float)
local a as THitBox
a.width = iWidth
a.height = iHeight
Endfunction a
// Set HitBox Position
Function rkHitBoxSetPosition(a ref as THitBox,ix as float, iy as float)
a.x = ix
a.y = iy
Endfunction
// How much does the source's BOTTOM side encroach into the target
Function rkHitBoxGetEncroachBottom(source as THitBox, target as THitBox)
Local sy as float : sy = rkHitBoxGetY(source)
Local sh as float : sh = rkHitBoxGetHeight(source)
Local ty as float : ty = rkHitBoxGetY(target)
Local th as float : th = rkHitBoxGetHeight(target)
local return_value as float
return_value = abs((sy+sh) - ty)
Endfunction return_value
// How much does the source's TOP side encroach into the target
Function rkHitBoxGetEncroachTop(source as THitBox, target as THitBox)
Local sy as float : sy = rkHitBoxGetY(source)
Local sh as float : sh = rkHitBoxGetHeight(source)
Local ty as float : ty = rkHitBoxGetY(target)
Local th as float : th = rkHitBoxGetHeight(target)
local return_value as float
return_value = abs(sy - (ty+th))
Endfunction return_value
// How much does the source's LEFT side encroach into the target
Function rkHitBoxGetEncroachLeft(source as THitBox, target as THitBox)
Local sx as float : sx = rkHitBoxGetX(source)
Local sw as float : sw = rkHitBoxGetWidth(source)
Local tx as float : tx = rkHitBoxGetX(target)
Local tw as float : tw = rkHitBoxGetWidth(target)
local return_value as float
return_value = abs(sx - (tx+tw))
Endfunction return_value
// How much does the source's RIGHT side encroach into the target
Function rkHitBoxGetEncroachRight(source as THitBox, target as THitBox)
Local sx as float : sx = rkHitBoxGetX(source)
Local sw as float : sw = rkHitBoxGetWidth(source)
Local tx as float : tx = rkHitBoxGetX(target)
Local tw as float : tw = rkHitBoxGetWidth(target)
local return_value as float
return_value = abs((sx+sw) - tx)
Endfunction return_value
// Return if source and target are overlaping
Function rkHitBoxIntersects(source as THitBox, target as THitBox)
Local sx as float : sx = rkHitBoxGetX(source)
Local sy as float : sy = rkHitBoxGetY(source)
Local sw as float : sw = rkHitBoxGetWidth(source)
Local sh as float : sh = rkHitBoxGetHeight(source)
Local tx as float : tx = rkHitBoxGetX(target)
Local ty as float : ty = rkHitBoxGetY(target)
Local tw as float : tw = rkHitBoxGetWidth(target)
Local th as float : th = rkHitBoxGetHeight(target)
local return_value as integer
If ((sx + sw) > tx And sx < (tx + tw) And (sy + sh) > ty And sy < (ty + th))
return_value = 1
Endif
Endfunction return_value
Function rkHitBoxGetX(a as THitBox)
Endfunction a.x+a.x_offset
Function rkHitBoxGetY(a as THitBox)
Endfunction a.y+a.y_offset
Function rkHitBoxGetWidth(a as THitBox)
Endfunction a.width
Function rkHitBoxGetHeight(a as THitBox)
Endfunction a.height
.