Here is a simple little game I wrote along time ago, and converted to LB, and now a JB version. In this version we make use of WAV files supplied with Windows, but it could probably be improved if you adapt it to use more elaborate WAV files you can find. In the LB version, use is made of some of the 128 voices in the WINMM.DLL similar to the way Alyce uses in her PIANO6.BAS supplied with LB. Another change from LB had to be made was in target detection. The ideal method to detect if an object has hit a specific target in any program is to use pixel detection, achieved with LB by a suitable API call, as shown to me some time ago by others more knowledgeable than me. In this version we simply check if x relates to subm being the row position of the sub or x relates to spos the row position of the destroyer.
At the start you will see my fussy way of ensuring the window is usually displayed in the centre of the screen, with ux and uy. The buttons are not inserted in the opening window, but activated when safe to do so using the dialog as #b at [fbutn], thus preventing anyone activating a button at the wrong time. When closing the program in [quit] a check has to be made to see if this dialog window is open
A little warning here regarding the popular method of the way [halt] is used to create program delays. If you activate your task manager by pressing Ctrl+Atl+Del, and ensure Processes is activated, you will notice as either shot is fired making use of [halt], the CPU Usage jumps alarmingly for a fraction of a second. This is why there is a far more economical method of causing a delay using the CPU with LB, again by using an API call which are not available with JB.
You may have noticed most of my articles in these JB News Letters relate to the way I manage to amend some of my LB programs on my site to work almost a well with JB, but I often point out the advantages of LB. There is an old expression used in the North of England ‘ you can’t get owt for nowt’, meaning while we must be grateful for Carl releasing this Free Just Basic, we cannot of course expect it to cover all the facilities of Liberty Basic!
nomainwin
ux = 1 : uy = 1
if DisplayWidth > 1000 then ux = 120 : uy = 90
UpperLeftX = ux : UpperLeftY = uy
WindowWidth = 800 : WindowHeight = 600
open "SUBHUNT" for graphics_nsb as #s
#s "trapclose [quit]; fill 0 50 50; backcolor 0 50 50"
#s "color cyan; font arial 12; down"
for wave = 1 to 400
xwave=int(rnd(1)*700)+50
ywave=int(rnd(1)*500)+30
#s "place ";xwave;" ";ywave
#s "\~"
next wave
#s "getbmp drawing 1 1 700 500"
#s "font arial 24 bold; place 250 100"
#s "\ SUBMARINE HUNT. "
#s "font arial 14 bold; place 210 200"
#s "\The Sub will only be seen briefly only once."
#s "\It will fire a torpedoe, and then move a little."
#s "\Move the Destroyer then fire a Depth Charge."
#s "\";space$(10);"Game ends when either are hit."
#s "\\";space$(15);"CLICK FIRE TO BEGIN."
'destroyer
#s "color lightgray; backcolor lightgray"
#s "place 10 40; boxfilled 60 50; place 36 40; boxfilled 44 32"
#s "size 5; set 20 38; set 34 38; set 50 38"
#s "size 2; line 42 40 42 30"
#s "size 1; line 20 38 13 33; line 50 38 57 33"
#s "size 3; color 0 50 50; backcolor 0 50 50"
#s "line 12 49 10 44; line 56 49 60 44"
#s "getbmp ship 1 1 60 50"
#s "color 0 50 50; place 10 30; boxfilled 60 50"
playwave "ringin" : turn = 0
tl$ = " START" : gosub [fbutn] : wait
[go]
shot = 1 : playwave "start"
#s "background drawing; drawsprites"
spos = int(rnd(1)*500)+50 ' destroyer
#s "drawbmp ship ";spos-25;" 460"
' submarine
#s "color lightgray; size 6; line 350 30 380 30"
#s "set 365 25; size 2; set 357 27; line 367 25 367 19"
#s "size 1; line 353 25 357 25"
delay = 2000 : gosub [halt] : delay = 100
#s "color 0 50 50" ' hide submarine
#s "place 342 16; boxfilled 388 38"
battle = 1 : #s "size 2"
subm = int(rnd(1)*500)+50 : x = subm : hit = 0
't$="spos";str$(spos);" ";"x";str$(x) : notice t$
while battle = 1
turn = 1 : playwave "low"
for y = 40 to 580 step 10 ' Submarine fire
if x > spos-16 and x < spos +34 and y > 490 then
hit = 1
exit for
end if
#s "color yellow; line ";x;" ";y;" ";x;" ";y+4
gosub [halt] : scan
if y mod 150 = 0 then playwave "recycle"
#s "color 0 50 50; line ";x;" ";y;" ";x;" ";y+4
next y
if hit = 1 then gosub [hit]
[destroyer]
#s "drawbmp ship ";spos-25;" 460"
tl$=" MOVE" : gosub [fbutn] : wait
[shoot] ' Destroyer fire
x = spos : note = 100 : turn = 2 : hit = 0
playwave "chimes"
for y = 400 to 0 step -10
if x > subm-15 and x < subm+15 and y < 40 then
hit = 1
exit for
end if
#s "color white; set ";x;" ";y
gosub [halt] : scan
if y mod 100 = 0 then playwave "start"
#s "color 0 50 50; set ";x;" ";y
next y
if hit = 1 then gosub [hit]
shot = shot + 1 : gosub [submove]
wend
[done] ' FINISH
#s "place 200 450"
if turn = 1 then lost$ = "Destroyer" else lost$ = "Submarine"
#s "\";lost$;" sunk. You tried ";shot;" depth charges."
confirm "Another Try?"; q$
if q$ = "yes" then [go] else [quit]
[fire]
close #b : butn = 0
if turn = 0 then [go] else [shoot]
[left]
close #b : butn = 0
#s "place ";spos-15;" 480; boxfilled ";spos+35;" 510"
if spos > 40 then spos = spos - 10
goto [destroyer]
[right]
close #b : butn = 0
#s "place ";spos-15;" 480; boxfilled ";spos+35;" 510"
if spos < 740 then spos = spos + 10
goto [destroyer]
[submove] ' move submarine
move = int(rnd(1)*100)+20-(int(rnd(1)*100)+20)
subm = subm + move : x = subm
if x < 20 or x > 700 then [submove]
return
[hit]
#s "color red; size 20; set ";x;" ";y
playwave "tada": battle = 0 : y = 0
goto [done]
[halt]
tim=time$("ms")
while time$("ms") < tim+delay
wend : return
[fbutn]
UpperLeftX = 470 : UpperLeftY = 500
WindowWidth = 130 : WindowHeight = 55
if turn > 0 then button #b, "<--", [left], UL,1,1
button #b, "FIRE", [fire], UL,40,1
if turn > 0 then button #b, "-->", [right], UL,90,1
open tl$ for dialog as #b
#b "trapclose [quit]" : butn = 1
return
[quit]
if butn = 1 then close #b
close #s : end