|
Post by Rod on Jun 12, 2024 15:24:27 GMT
Try writing that last comment without GOTO. You don’t GOTO a sub you always call and return just like a function.
I need to write you some sample code.
|
|
|
Post by Rod on Jun 13, 2024 13:16:40 GMT
Have a look at this, it is draw.bas that ships with Just BASIC rewritten as all subs. There is virtually no main program everything is executed by a sub. The program flow is directed by the when leftButton events written in each sub. There are only four global values.
If I get time I will rewrite your battleship game with subs.
'First we set up our window nomainwin bmpbutton #1, "bmp\redbttn.bmp", colorRed, UL, 5, 5 bmpbutton #1, "bmp\yllwbttn.bmp", colorYellow, UL, 40, 5 bmpbutton #1, "bmp\grnbttn.bmp", colorGreen, UL, 75, 5 bmpbutton #1, "bmp\bluebttn.bmp", colorBlue, UL, 110, 5 bmpbutton #1, "bmp\panbttn.bmp", setForPan, UR, 110, 5 bmpbutton #1, "bmp\circbttn.bmp", setForEllipse, UR, 40, 5 bmpbutton #1, "bmp\sqrbttn.bmp", setForBox, UR, 5, 5 bmpbutton #1, "bmp\linebttn.bmp", setForLine, UR, 75, 5 graphicbox #1.graph, 5, 37, 300, 200 WindowWidth = 312 WindowHeight = 282 open "Liberty Draw" for graphics_nsb_nf as #1 #1 "trapclose quit" #1 "fill lightgray ; flush"
'we set one rightbutton up event to clear the screen 'left button events are managed inside the subs #1.graph "when rightButtonUp clear" #1.graph "down" #1.graph "color black"
'this is our program, a few globals global startX,startY,oMouseX,oMouseY 'set up for drawing a line call setForLine h$ 'now just wait for user input 'how the program reacts is managed 'with the when leftButton commands in the subs 'the subs are called from here when a button is 'clicked or the mouse moves. 'always, always, whatever sub has been called the program flow returns here to wait for further user input. wait 'Thats it the program consists of three lines
sub startDraw h$,x,y startX=x startY=y #1.graph "place "; startX; " "; startY end sub
sub firstLine h$,x,y #1.graph "when leftButtonMove trackLine" #1.graph "line "; startX; " "; startY; " ";x; " ";y oMouseY = y oMouseX = x end sub
sub firstFrame h$,x,y #1.graph "when leftButtonMove trackBox" #1.graph "box ";x; " ";y oMouseY = y oMouseX = x end sub
sub trackBox h$,x,y #1.graph "color white" #1.graph "box "; oMouseX; " "; oMouseY #1.graph "color black" #1.graph "place "; startX; " "; startY #1.graph "box ";x; " ";y oMouseY = y oMouseX = x end sub
sub trackLine h$,x,y #1.graph "color white" #1.graph "line "; startX; " "; startY; " "; oMouseX; " "; oMouseY #1.graph "color black" #1.graph "place "; startX; " "; startY #1.graph "line "; startX; " "; startY; " ";x; " ";y oMouseY = y oMouseX = x end sub
sub finishLine h$,x,y #1.graph, "when leftButtonMove firstLine" #1.graph, "discard" #1.graph, "line "; startX; " "; startY; " "; MouseX; " "; MouseY #1.graph, "flush ; redraw" end sub
sub finishBox h$,x,y #1.graph "when leftButtonMove firstFrame" #1.graph "discard" #1.graph "boxfilled "; MouseX; " "; MouseY #1.graph "flush ; redraw" end sub
sub finishEllipse h$,x,y #1.graph "when leftButtonMove firstFrame" #1.graph "discard" #1.graph "place "; int((startX+x)/2); " "; int((startY+y)/2) #1.graph "ellipsefilled "; abs(x-startX); " "; abs(y-startY) #1.graph "flush ; redraw" end sub
sub clear h$,x,y print #1.graph, "cls" end sub
sub setForLine h$ #1.graph "when leftButtonDown startDraw" #1.graph "when leftButtonMove firstLine" #1.graph "when leftButtonUp finishLine" end sub
sub setForBox h$ #1.graph "when leftButtonDown startDraw" #1.graph "when leftButtonMove firstFrame" #1.graph "when leftButtonUp finishBox" end sub
sub setForEllipse h$ #1.graph "when leftButtonDown startDraw" #1.graph "when leftButtonMove firstFrame" #1.graph "when leftButtonUp finishEllipse" end sub
sub setForPan h$ filedialog "Get *.bmp file", "bmp\*.bmp", panFile$ if panFile$ = "" then exit sub
loadbmp "panWithMe", panFile$ #1.graph "when leftButtonDown drawBmp" #1.graph "when leftButtonMove drawBmp" #1.graph "when leftButtonUp flushBmp" end sub
sub drawBmp h$,x,y #1.graph "drawbmp panWithMe ";x; " ";y end sub
sub flushBmp h$,x,y #1.graph "flush" end sub
sub colorRed h$ #1.graph "backcolor red" end sub
sub colorYellow h$ #1.graph "backcolor yellow" end sub
sub colorGreen h$ #1.graph "backcolor green" end sub
sub colorBlue h$ #1.graph "backcolor blue" end sub
sub lineThickness h$ prompt "Line Thickness"; lineThickness$ #1.graph "size "; lineThickness$ end sub
sub printIt h$ #1.graph, "print" end sub
sub quit h$ confirm "Are you sure?"; answer$ if answer$ = "no" then exit sub close #1 end end sub
|
|
|
Post by honky on Jun 14, 2024 8:40:02 GMT
A fine example, thank you. I expected you to do recursion, which could not be possible without sub.
|
|
|
Post by Rod on Jun 14, 2024 18:41:51 GMT
Sorry, I keep having to respond. Recursion is perfectly possible with if then GOTO. If done goto end else goto start.
The point I try to make is that more formal structures have come into fashion.
So call myrecursive sub is exactly the same as the if then goto version but it dresses up the code and forces the programmer to follow clean coding loop structures.
Remember with recursion we are just calling a block of code till a condition is met.
Dont get too hung up on it but don’t dismiss the moder alternatives out of hand, try a few.
|
|