[I'm not subscribed to the PC-ASK or DOS-DISCUSS lists, only SurvPC; if this
message gets through to those lists please cc: me any comments :)]

>       I found this task immpossible recently using QBasic

QBasic should actually make it easier than bog-standard BASICs; subroutines
with local variables make life a lot simpler if you wanted to do it
recursively, but you can always iterate within a FOR loop using a GOSUB
subroutine if you really want traditional BASIC.

>        little did I realize that everytime PRINT is invoked it prints
> to/on a new/next line...

By default, PRINT <expression> will output the value of <expression> and a
newline.  PRINT <expression>; will suppress the newline.  LOCATE <row>,
<column> will move the cursor.

One solution to your problem, in QBasic, would be:

DECLARE SUB DrawRect(size%)

CONST SCRWIDTH = 80
CONST SCRHEIGHT = 25
CONST ASPECT = 3

CLS

DIM size%

FOR size% = 20 TO 4 STEP -4
    DrawRect size%
NEXT

SUB DrawRect(size%)
    DIM y%, xmin%, xmax%, ymin%, ymax%

    IF size% < 2 THEN EXIT SUB

    xmin% = (SCRWIDTH - ASPECT * size%) / 2
    xmax% = xmin% + ASPECT * size% - 1
    ymin% = (SCRHEIGHT - size%) / 2
    ymax% = ymin% + size% - 1

    LOCATE ymin%, xmin%
    PRINT STRING$(ASPECT * size%, "*");

    FOR y% = (ymin% + 1) TO (ymax% - 1)
        LOCATE y%, xmin%
        PRINT "*";
        LOCATE y%, xmax%
        PRINT "*";
    NEXT

    LOCATE ymax%, xmin%
    PRINT STRING$(ASPECT * size%, "*");
END SUB

That's using QBasic's subroutines to make the code more readable than GOSUBs
and labels and so forth; since I'm mainly a C/Java programmer these days I
prefer not to use such constructs.  A recursive version can be formed by
changing the three-line FOR loop to simply "DrawRect 40" and adding the line
"DrawRect size% - 4" to the end of the DrawRect routine.  Add a SLEEP 1 or
similar delay before the call to make it 'animated'.

Oh, BTW, the code above has rounding errors in it; I'm used to having
fractions truncated and QBasic seems to round them off.  Add INT() calls as
needed if memory serves, it's been a long time since I wrote any QuickBASIC
code in anger.

Regards,
Ben A L Jemmett.
(http://web.ukonline.co.uk/ben.jemmett/, http://www.deltasoft.com/)

Reply via email to