Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
request for printing patterns with for loops tutorial
#11
(11-20-2024, 11:59 PM)fistfullofnails Wrote: Thanks everyone, but I still need to start with something simple, like a square, because I really don't understand what's happening.

Code: (Select All)
FOR i = 1 to 3
   PRINT "***"
NEXT

The above would print 3 lines for you of "***", making your square.

***
***
***
Reply
#12
@fistfullofnails You can "Print patterns" on Screen 0, the default screen (when Screen is not called in code), simply by changing colors and printing a space = " ". A space ONLY prints the background color AND can be used to clear text on a line instead of CLS so you can erase parts of screen without erasing everything.

There are 8 background colors in Screen 0 and here is a horizontal striped using color changes and printing spaces:
Code: (Select All)
For row = 1 To 25 ' the default = screen 0 has 25 rows  to put characters you type
    For col = 1 To 80 ' the default = screen 0 has 80 columns  to put characters you type
        ' you can color screen 0 simple by specifying a backcolor and printing a space
        Color , row Mod 8 ' a mod b divides a by b and return remainder a number between 0 & b-1 inclusive
        Locate row, col: Print " "; ' print a space at each location
    Next
Next

simply change
Color ,row, Mod 8
to
Color , col, Mod 8

and the horizontal stripes become vertical!
Code: (Select All)
For row = 1 To 25 ' the default = screen 0 has 25 rows  to put characters you type
    For col = 1 To 80 ' the default = screen 0 has 80 columns  to put characters you type
        ' you can color screen 0 simple by specifying a backcolor and printing a space
        Color , col Mod 8 ' a mod b divides a by b and return remainder a number between 0 & b-1 inclusive
        Locate row, col: Print " "; ' print a space at each location
    Next
Next
b = b + ...
Reply
#13
OK @fistfullofnails to print a true square on default screen 0

Here is red square printed with background color 4 into middle of screen:
Code: (Select All)
'  a character cell is 8 pixels wide X 16 pixels high
rowStart = 11: rowEnd = 15 ' 5 vertical rows  5 * 16 = 80 pixels
colStart = 36: colEnd = 45 ' 10 columns 10 * 8 = 80 pixels

' 80 pixels X 80- pixels = true square in screen 0

' pick a color 0 to 7 not 0 = default black background say 4
backcolor = 4 ' try different colors here 4 is red

'  color forecolor,  backcolor   '<<< command to control color

Color , backcolor '  set color to 4, now anywhere we print backcolor is red = 4

' color our square on default black background
For row = rowStart To rowEnd
    For col = colStart To colEnd
        Locate row, col: Print " "; ' print a space at each location of the square
    Next
Next
Sleep
b = b + ...
Reply
#14
bplus riffing on the last again:
Code: (Select All)
rowStart = 11: rowEnd = 15 ' 5 vertical rows  5 * 16 = 80 pixels
colStart = 36: colEnd = 45 ' 10 columns 10 * 8 = 80 pixels

' 80 pixels X 8- pixels = true square in screen 0

' pick a color 0 to 7 not 0 say
sqH = 5 ' 1 to 25 rows
sqW = 2 * sqH ' for a square width = 2*height = twice as many columns
sqLeft = 36 ' left most column
sqTop = 11 ' top most row
backcolor = 4 ' try different colors here 4 is red

GoSub printSquare
Locate 22, 23: Print "zzz... press any for random squares"
Sleep

' riffing again
' pick a random square height max is 25
Do
    sqH = Int(Rnd * 25) + 1 ' 1 to 25 rows
    sqW = 2 * sqH ' for a square width = 2*height = twice as many columns
    sqLeft = Int(Rnd * (80 - sqW)) + 1 ' left most column
    sqTop = Int(Rnd * (25 - sqH)) + 1 ' top most row
    backcolor = (backcolor + 1) Mod 8 ' 0 to 7 colors (including black as "eraser"
    GoSub printSquare
    _Limit 2
Loop Until _KeyDown(27) ' until escape key is pressewd
End
printSquare:
Color , backcolor
For row = sqTop To sqTop + sqH - 1
    For col = sqLeft To sqLeft + sqW - 1
        Locate row, col: Print " "; ' print a space at each location of the square
    Next
Next
Return
b = b + ...
Reply




Users browsing this thread: 3 Guest(s)