Older Version
Newer Version
benjamin805
Jul 22, 2011
This is my second attempt at creating 2D level walls or borders. This time I am using sprites. The movement is very primitive, but I hope to add animation in my next attempt. You need the images below copied into your Liberty BASIC folder.
[[file:cave1.bmp]][[file:tree.bmp]][[file:food.bmp]]
[[code format="lb"]]
'basic 2d game level wall/border example 2
'using sprites
'programmed by Ben Jimenez
'8=up 6=right 2=down 4=left 7=up/left
'9=up/right 1=down/left 3=down/right
'July 22,2011
nomainwin
WindowWidth = 470
WindowHeight = 500
open "Cave dude eat food" for graphics_nsb_nf as #main
print #main, "font ms_sans_serif 0 16"
loadbmp "sprite1","cave1.bmp"
loadbmp "tree","tree.bmp"
loadbmp "food","food.bmp"
print #main, "addsprite sprite1 sprite1";
dim pxy$(22,22)
'start postion of first wall piece
x=10
y=20
for r=1 to 22 'first row
for c=1 to 22 'current row column loop
read d$
pxy$(r,c)=d$
if d$="t" then 'if border piece then draw it
tcnt=tcnt+1
#main,"addsprite tree";tcnt;" tree"
#main,"spritexy tree";tcnt;" ";x;" ";y
end if
if d$="f" then 'if border piece then draw it
fcnt=fcnt+1
#main,"addsprite food";fcnt;" food"
#main,"spritexy food";fcnt;" ";x+5;" ";y+10
food=food+1
end if
if d$="s" then
#main,"spritexy sprite1 ";x;" ";y
#main,"spritescale sprite1 40"
px=x 'set current pen x position to player start x
py=y ' set current pen y position to player start y
ar=r 'remember current row location
ac=c 'remember current column locaton
end if
x=x+20 'increase column x location by 20
next c
x=10
y=y+20 'increase row y location by 20
next r
#main,"when characterInput [checkkey]"
[main.inputLoop] 'wait here for input event
#main,"setfocus"
#main,"drawsprites"
if pxy$(ar,ac)="e" and food=0 then
notice "You collected all the food and escaped!"
end if
if pxy$(ar,ac)="e" and food>0 then
notice "You need to collect more food!"
end if
wait
[checkkey]
'player move loop
'player can move as long as his path
'is not blocked by tree
key$ = Inkey$
if len(key$) < 2 then
if key$="2" then 'down
if pxy$(ar+1,ac)<>"t" then 'if next space is not a tree
py=py+20
#main,"spritexy sprite1 ";px;" ";py
ar=ar+1
end if
end if
if key$="6" then 'right
if pxy$(ar,ac+1)<>"t" then 'if next space is not a tree
px=px+20
#main,"spritexy sprite1 ";px;" ";py
ac=ac+1
end if
end if
if key$="4" then 'left
if pxy$(ar,ac-1)<>"t" then 'if next space is not a tree
px=px-20
#main,"spritexy sprite1 ";px;" ";py
ac=ac-1
end if
end if
if key$="8" then 'up
if pxy$(ar-1,ac)<>"t" then 'if next space is not a tree
py=py-20
#main,"spritexy sprite1 ";px;" ";py
ar=ar-1
end if
end if
if key$="3" then 'down,right
if pxy$(ar+1,ac+1)<>"t" then 'if next space is not a tree
px=px+20
py=py+20
#main,"spritexy sprite1 ";px;" ";py
ac=ac+1
ar=ar+1
end if
end if
if key$="1" then 'down,left
if pxy$(ar+1,ac-1)<>"t" then 'if next space is not a tree
px=px-20
py=py+20
#main,"spritexy sprite1 ";px;" ";py
ac=ac-1
ar=ar+1
end if
end if
if key$="9" then 'up,right
if pxy$(ar-1,ac+1)<>"t" then 'if next space is not a tree
px=px+20
py=py-20
#main,"spritexy sprite1 ";px;" ";py
ac=ac+1
ar=ar-1
end if
end if
if key$="7" then 'up,left
if pxy$(ar-1,ac-1)<>"t" then 'if next space is not a tree
px=px-20
py=py-20
#main,"spritexy sprite1 ";px;" ";py
ac=ac-1
ar=ar-1
end if
end if
end if
if pxy$(ar,ac)="f" then
#main,"spritecollides sprite1";
input #main,list$
#main,"spritevisible ";word$(list$,1);" off";
pxy$(ar,ac)="x"
food=food-1
end if
goto [main.inputLoop]
'map data
data t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t
data t,x,x,x,x,x,x,x,t,f,x,t,x,t,x,t,x,x,x,x,x,t
data t,x,t,t,x,x,t,x,t,x,t,t,t,x,x,x,t,t,x,f,x,t
data t,t,t,t,x,x,x,x,x,t,x,x,t,t,x,t,x,t,t,x,x,t
data t,x,f,x,x,x,x,x,x,x,t,x,t,t,t,x,x,x,t,x,x,t
data t,x,x,t,x,x,x,t,x,t,t,t,x,t,t,x,x,t,t,t,x,t
data t,x,t,t,x,x,t,x,t,x,x,t,x,x,x,t,t,t,t,t,x,t
data t,x,x,x,x,t,x,f,t,t,x,x,x,t,x,t,t,t,t,t,x,t
data t,x,x,t,x,x,t,t,t,x,x,t,x,t,t,x,x,x,x,t,f,t
data t,x,t,t,x,t,x,x,x,t,t,x,x,x,x,t,x,x,x,x,t,t
data t,x,x,t,x,x,x,x,t,x,s,t,x,x,x,x,x,x,t,t,x,t
data t,x,x,x,t,t,x,t,x,t,t,t,x,t,x,t,t,x,x,x,x,t
data t,x,x,t,x,x,x,t,x,x,t,x,x,x,t,x,x,x,x,x,x,t
data t,x,t,x,x,t,x,t,t,x,x,x,x,x,x,x,x,t,t,t,x,t
data t,t,x,x,x,x,x,x,t,x,x,t,x,x,t,t,x,t,t,t,x,t
data t,x,t,t,x,x,t,x,x,x,t,x,x,t,t,t,x,t,t,t,t,t
data t,x,x,t,x,t,t,x,t,x,t,x,x,t,x,x,x,x,x,t,t,t
data t,t,x,t,x,x,t,x,f,t,x,x,t,x,x,t,t,x,t,t,x,e
data t,t,f,t,x,x,x,x,x,t,x,x,x,t,x,x,t,x,t,t,x,t
data t,x,t,x,x,t,x,t,x,t,t,x,t,t,x,x,t,x,x,x,x,t
data t,x,t,x,x,x,t,x,x,x,x,x,t,f,t,x,x,t,x,x,x,t
data t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t
[[code]]