I my first try at platform game logic, inspired after a visit to http://games.freebasic.net/basicgaming.php
Code: Select all
#include "fbgfx.bi"
const as integer SCRN_W = 800, SCRN_H = 600
'const as integer BLOCK_SZ = 20
'const as integer BLOCKS_X = SCRN_W \ 20, BLOCKS_Y = SCRN_H \ 20
const as single GRAVITY = 3000 '[pixels/s^2]
type xy_sgl
dim as single x, y
end type
type player_type
dim as xy_sgl p '[pixels]
dim as xy_sgl v '[pixels/s]
dim as integer state
end type
sub clearScreen()
line(0, 0)-(SCRN_W-1, SCRN_H-1), &h00000000, bf
end sub
sub printAt(x as integer, y as integer, text as string)
locate y, x: print text;
end sub
enum
ST_STANDING
ST_WALKING
ST_JUMPING
'ST_DIYING
'ST_DEAD
ST_NONE
end enum
dim as string states(ST_NONE) = {"STANDING", "WALKING", "JUMPING", "NONE"}
'dim as integer blocks(BLOCKS_X-1, BLOCKS_Y-1)
dim as double tNow, tPrev, dt
dim as boolean quit = false
dim as player_type player
player.p = type(100.0, 100.0)
player.state = ST_JUMPING
screenres(SCRN_W, SCRN_H, 32)
tNow = timer
dt = 0
while not quit
if multikey(FB.SC_ESCAPE) then quit = true
with player
if (.state <> ST_JUMPING) then
.v.x = 0
.state = ST_STANDING
if multikey(FB.SC_LEFT) then
.v.x = -200.0
.state = ST_WALKING 'maybe
end if
if multikey(FB.SC_RIGHT) then
.v.x = +200.0
.state = ST_WALKING 'maybe
end if
end if
.p.x += .v.x * dt
'jump if not in air
if multikey(FB.SC_UP) then
'if (.p.y > 499) then
if (.state <> ST_JUMPING) then
.v.y = -800.0
.state = ST_JUMPING
end if
end if
.v.y += GRAVITY * dt '[m/s] = [m/s^2] * [s]
.p.y += .v.y * dt '[m] = [m/s] * [s]
'jumped (or dropped) on to somthing
if (.p.y > 500.0) then
.p.y = 500.0
.v.y = 0
if (.state = ST_JUMPING) then .state = ST_STANDING
end if
end with
screenlock
clearScreen()
printAt(2, 2, "Keys: <left>, <right>, <up>")
printAt(2, 4, "Player state: " + str(player.state) + " = " + states(player.state))
printAt(2, 6, "x: " + str(player.p.x) + ", " + str(player.p.y))
circle(int(player.p.x + 0.5), int(player.p.y + 0.5)), 20, &h00ff0088
screenunlock
sleep 1, 1
inkey 'clear key buffer
tPrev = tNow
tNow = timer
dt = tNow - tPrev
wend