Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
request for printing patterns with for loops tutorial
#1
I'm trying to figure out how to build a christmas tree and such by using for loops.  I can't seem to find any using QB64 or Basic and I do not understand by looking at python examples.  I probably need to see something simple like maybe just a square before moving on to a christmas tree.  As in something like this

****
****
****
Reply
#2
(11-20-2024, 02:47 AM)fistfullofnails Wrote: I'm trying to figure out how to build a christmas tree and such by using for loops.  I can't seem to find any using QB64 or Basic and I do not understand by looking at python examples.  I probably need to see something simple like maybe just a square before moving on to a christmas tree.  As in something like this

****
****
****

     I googled a bit and found this.

     Probably a bit more elaborate than you wanted.

    
Code: (Select All)

'erl  12.9.98      Christmas Tree
CLS
SCREEN 12      '640 x 480

COLOR 15                                ' **** puts snow in background
RANDOMIZE TIMER
FOR Snow = 1 TO 300
    DO
        LET Column = INT(RND * 100) + 1
        LET Row = INT(RND * 100) + 1
    LOOP UNTIL Row <= 28 AND Column <= 80
    LOCATE Row, Column
    PRINT "*"
NEXT Snow

LOCATE 1, 71
COLOR 12
PRINT "Eric Laslo"
COLOR 15

FOR TreeBody = 1 TO 18          ' **** loop that draws body as a series of
    IF TreeBody = 1 THEN        ' **** overlapping triangles that
        LET StartRow = 50        ' **** increase in size and move down
        LET LeftColumn = 300
        LET RightColumn = 340
        LET BottomLine = 60
    ELSE
        LET StartRow = StartRow + 10
        LET LeftColumn = LeftColumn - 8
        LET RightColumn = RightColumn + 8
        LET BottomLine = BottomLine + 20
    END IF
    LINE (320, StartRow)-(LeftColumn, BottomLine), 2
    LINE (LeftColumn, BottomLine)-(RightColumn, BottomLine), 2
    LINE (RightColumn, BottomLine)-(320, StartRow), 2
    PAINT (320, BottomLine - 1), 2
NEXT TreeBody

LINE (300, 400)-(340, 430), 6, BF    'trunk

LINE (0, 430)-(640, 480), 7, BF            'ground
LINE (0, 430)-(640, 430), , , &H1112        'snow on ground
LINE (0, 431)-(640, 431), , , &H1113
LINE (0, 432)-(640, 432), , , &H1114
LINE (0, 433)-(640, 433), , , &H1115
LINE (0, 434)-(640, 434), , , &H1116
LINE (0, 435)-(640, 435), , , &H1117
LINE (0, 436)-(640, 436), , , &H1118
LINE (0, 437)-(640, 437), , , &H1119
LINE (0, 438)-(640, 438), , , &H1110
LINE (0, 439)-(640, 439), , , &H111
LINE (0, 440)-(640, 440), , , &H112

'star
LINE (290, 40)-(350, 40), 14
LINE (290, 40)-(320, 60), 14
LINE (350, 40)-(320, 60), 14
LINE (320, 20)-(340, 70), 14
LINE (320, 20)-(300, 70), 14
LINE (300, 70)-(320, 60), 14
LINE (340, 70)-(320, 60), 14
PAINT (320, 50), 14
PAINT (320, 30), 14
PAINT (340, 41), 14
PAINT (300, 41), 14
PAINT (315, 60), 14
PAINT (325, 60), 14

'LET C = C                                    'sets color
CIRCLE (320, 75), 21, 4, 3.2, 6.2, 1 / 5      'decorations
CIRCLE (320, 90), 26, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 105), 30, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 120), 37, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 135), 47, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 150), 50, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 165), 55, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 180), 60, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 195), 70, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 210), 77, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 225), 82, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 240), 88, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 255), 96, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 270), 102, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 285), 106, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 300), 110, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 315), 120, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 330), 127, 14, 3.2, 6.2, 1 / 5
CIRCLE (320, 345), 130, 4, 3.2, 6.2, 1 / 5
CIRCLE (320, 360), 135, 9, 3.2, 6.2, 1 / 5
CIRCLE (320, 375), 144, 14, 3.2, 6.2, 1 / 6

DO UNTIL X = 9999 AND Y = 9999        'lights
    READ X, Y
    IF X = 9999 THEN EXIT DO
    DATA 300,80,290,200,240,400,300,130,310,300,340,210,360,250,370,140,360,315
    DATA 450,370,270,270,300,180,290,315,270,150,250,190,260,320,200,390
    DATA 400,270,320,390,290,380,345,380,330,90,350,130,360,180,370,350,340,370
    DATA 300,110,310,360,190,390
    DATA 9999,9999,9999
    DO                                'determines color
        LET C = INT(RND * 100)
    LOOP UNTIL C = 1 OR C = 4 OR C = 5 OR C = 10 OR C = 12 OR C = 14
    CIRCLE (X, Y), 3, C
    PAINT (X, Y), C
LOOP

DO WHILE song = 0
        dth1$ = "O3L4G.L8FL4EDCDEC"
        dth2$ = "O3L8DEFDL4E.L8DL4CO2BO3L2CL4G.L8FL4ED"
        dth3$ = "O3L4CDECL8DEFDL4E.L8DL4CO2BO3L2C"
        dth4$ = "O3L4D.L8EL4FDE.L8FL4GD"
        dth5$ = "O3L8EFL4GL8ABL4O4CO3BAL2GL4G.L8FL4ED"
        dth6$ = "O3L4CDECL8AAAAL4G.L8FL4EDL2C"
    PLAY dth1$ + dth2$ + dth3$ + dth4$ + dth5$ + dth6$
        oct1$ = "O3L4CL8F.L16FL4FGL8A.L16AL4A.L8AL8GAL4B-E"
        oct2$ = "O3L4GFCL8F.L16FL4FGL8A.L16AL4A.L8A"
        oct3$ = "O3L8GAL4B-EGFO4L8P8CL8CO3AO4L4D.L8C"
        oct4$ = "O4L8CO3B-L4B-.L8B-B-GO4L4C.O3L8B-B-AL4A"
        oct5$ = "O3L4CL8F.L16FL4FGL8A.L16AL4A.L8AL8GAL4B-EGF"
    PLAY oct1$ + oct2$ + oct3$ + oct4$ + oct5$
LOOP

COLOR 15
END
   
Reply
#3
Simpler thing:
Code: (Select All)
Screen _NewImage(400, 600, 32)

xc = 200
For i = 1 To 30
    _PrintString (xc - .5 * i * 8, i * 16 + 20), String$(i, "*")
Next
For i = 31 To 35
    _PrintString (xc - .5 * 2 * 8, i * 16 + 20), String$(2, "*")
Next
Sleep

xc = 200 ' center pixel
For i = 1 To 30 ' rows and strings of *
_PrintString (xc - .5 * i * 8, i * 16 + 20), String$(i, "*")
Next

_printstring (x, y), text$ locates at exact pixel (x, y) to start print string assigned to it = text$
* is an Ascii character width 8 pixels, height 16 pixels
each * takes an 8x16 pixel space to print, That's the 8 and 16 multiplied by i

for x start at xc center pixel of screen and back up half the length of string = .5 * i * 8
for y start i * 16 = row added 20 to offset from top of screen for space before first star.

String$(n, x$) creates string of n * x$ (first char in x$)
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#4
Color the tree:
Code: (Select All)
Screen _NewImage(400, 600, 32)
xc = 200
For i = 1 To 31
    Locate i + 1, 25 - Int(i / 2)
    For j = 1 To i
        Color _RGB32(Rnd * 200 + 55, -(Rnd < .5) * Rnd * 255, -(Rnd < .5) * Rnd * 255)
        Print "*";
    Next
Next
Color _RGB32(100, 50, 25)
For i = 32 To 35
    _PrintString (xc - .5 * 2 * 8, i * 16), String$(2, "*")
Next
Sleep

Color the tree AND make lights twinkle:
Code: (Select All)
Screen _NewImage(400, 600, 32)
_PrintMode _KeepBackground
xc = 200
For i = 1 To 31
    Locate i + 1, 25 - Int(i / 2)
    For j = 1 To i
        Color _RGB32(Rnd * 200 + 55, -(Rnd < .5) * Rnd * 255, -(Rnd < .5) * Rnd * 255)
        Print "*";
    Next
Next
Color _RGB32(100, 50, 25)
For i = 32 To 35
    _PrintString (xc - .5 * 2 * 8, i * 16), String$(2, "*")
Next
' twinkle
Do
    i = Int(Rnd * 31) + 1
    Locate i + 1, 25 - Int(i / 2)
    k = Int(Rnd * i) + 1
    For j = 1 To i
        If j = k Then
            Color _RGB32(Rnd * 200 + 55, -(Rnd < .5) * Rnd * 255, -(Rnd < .5) * Rnd * 255)
            Print "*";
        Else
            Print " ";
        End If
    Next
    _Limit 10
Loop
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#5
Aren't you guys making this all too complicated for such a simple task and question?

Code: (Select All)
For i = 0 To 20
    Print Space$(40 - i); String$(i * 2 + 1, "*")
Next

Three simple lines of code.  That's all you need.  Nothing fancy.  Nothing complex.  Just a FOR statement, a PRINT statement, and a NEXT.
Reply
#6
Thanks everyone, but I still need to start with something simple, like a square, because I really don't understand what's happening.
Reply
#7
I suggest giving Terry's tutorial a go. It's an excellent learning resource.

QB64 Tutorial - The Tutorial
Reply
#8
Hey @fistfullofnails. You might want to take look at Terry Ritchie's QB64PE Tutorial to get started with programming in BASIC.

Terry's Link     Terry's Site    

Otherwise, the way to make a square, for example, starting at location 100 x 100 (x,y) would be LINE (100, 100)-(200,200), "color", B (for BOX)
Use the WIKI pages to look up commands and such. 

QB64PE Wiki

And feel free to ask lotsa questions around here. This is a newbie-friendly safe zone.   Smile
Reply
#9
(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.

Here is that square and some comments
Code: (Select All)
' col is short for column
' using default non graphics screen = Screen 0
' uses rows and cols for locating a character that is printed
' this screen is 80 characters wide and 25 characters high.
' Characters not pixels, anything Ascii that is printed typing

' Locate row, col
' row 1 is top row in screen any less is error
' col 1 is the first on the left


' print 3 rows of 4 *'s across
colStart = 1: colEnd = 4 ' for 4 * across
rowStart = 1: rowEnd = 3 ' for 3 rows
For row = rowStart To rowEnd
    For col = colStart To colEnd
        Locate row, col: Print "*"; ' print each * one at a time  the ; let's next print occur on same line
        _Delay .5
    Next
Next
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#10
riffing on last
Code: (Select All)
' col is short for column
' using default non graphics screen = Screen 0
' uses rows and cols for locating a character that is printed
' this screen is 80 characters wide and 25 characters high.
' Characters not pixels, anything Ascii that is printed typing

' Locate row, col
' row 1 is top row in screen any less is error
' col 1 is the first on the left


' print 3 rows of 4 *'s across
x$ = "*"
colStart = 1: colEnd = 4 ' for 4 * across
rowStart = 1: rowEnd = 3 ' for 3 rows
GoSub printJob
Locate 10, 1: Print "press any to see 40 across, 10 down of X's "
Sleep

' 40 x 10 X's
x$ = "X"
colStart = 1: colEnd = 40 ' for 4 * across
rowStart = 1: rowEnd = 10 ' for 3 rows
GoSub printJob
End

printJob:
For row = rowStart To rowEnd
    For col = colStart To colEnd
        Locate row, col: Print x$; ' print each * one at a time  the ; let's next print occur on same line
        _Delay .1
    Next
Next
Return
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  _Clearcolor and _setalpha day request TempodiBasic 0 442 04-29-2025, 09:27 AM
Last Post: TempodiBasic
  a little tutorial demo for Windows in SCREEN 0 TempodiBasic 0 583 12-10-2024, 01:00 AM
Last Post: TempodiBasic
  Portable Version of Terry Ritchie's Tutorial Updated mpgcan 6 5,802 09-22-2024, 09:13 PM
Last Post: TerryRitchie
  Steve's basic SIN/COS Tutorial SMcNeill 5 1,791 07-12-2024, 06:53 PM
Last Post: vince
  BIG QB64 Tutorial update TerryRitchie 4 1,502 05-22-2024, 06:20 PM
Last Post: TerryRitchie

Forum Jump:


Users browsing this thread: 1 Guest(s)