Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
linebricks or brickfill
#1
Here's a couple routines to create randomly varying brickfill patterns. There are briefer and mathematically slicker ways to do simple brick patterns but if you want to add a little variation in color and style the code can get a little longer. If you can make use of it feel free.

Code: (Select All)
'linebricks
'playing with brick patterns drawn with the line command
'fills whole screen and 2 other area with a colored brick pattern that can randomly vary from brick to brick
'press any key for brickish wonder    or esc to quit
Screen _NewImage(800, 500, 32)
_Title "Linebricks a brickfill demo"
Randomize Timer
Do
    'this randomly sey bw (brick width) bh (brick height) and mw (mortar width) for the purposes of demonstration
    bw = 4 + 4 * Int(Rnd * 8)
    bh = (bw * 3) / 5
    mw = bh / (4 + Rnd * 8)
    If mw < .5 Then mw = .5
    brickfill 0, 0, _Width, _Height, bw, bh, mw, _RGB32(240, 40, 40), _RGB32(100, 100, 100), 49, 12
    brickfill 0, 0, 200, 150, bw, bh, mw, _RGB32(40, 40, 40), _RGB32(100, 100, 100), 50, 6
    brickfill 200, 160, 400, 350, bw * 2, bh * 2, mw, _RGB32(40, 240, 40), _RGB32(100, 100, 100), 20, 10
Loop Until waitanykey$ = Chr$(27)
Function waitanykey$
    _KeyClear
    Do
        _Limit 60
        kk$ = InKey$
    Loop Until kk$ <> ""
    waitanykey$ = kk$
End Function
Sub brick (x, y, w, h, mwid, brickcolor As _Unsigned Long, mortarcolor As _Unsigned Long, cv)
    'draw a brick
    'each brick has color variation randomly picked in side the raneg defiend by cv
    'each brick may be randonly standard, have a lightened highlight  or a deep shadow and a higlight
    Dim tcolor As _Unsigned Long
    tred = _Red32(brickcolor) + Int(Rnd * cv) - Int(Rnd * cv)
    tgreen = _Green32(brickcolor) + Int(Rnd * cv) - Int(Rnd * cv)
    tblue = _Blue32(brickcolor) + Int(Rnd * cv) - Int(Rnd * cv)
    If tred < 0 Then tred = 0
    If tgreen < 0 Then tgreen = 0
    If tblue < 0 Then tblue = 0
    If tred > 255 Then tred = 255
    If tgreen > 255 Then tgreen = 255
    If tblue > 255 Then tblue = 255
    tcolor = _RGB32(tred, tgreen, tblue)
    Line (x, y)-(x - 1 + w, y - 1 + h), mortarcolor, BF
    Select Case Int(1 + Rnd * 14)
        Case 1, 2, 3, 4, 5, 6 'plain brick
            Line (x + mwid, y + mwid)-(x - 1 + w - mwid, y - 1 + h - mwid), tcolor, BF
        Case 7, 8 'sahdow and highlight brick
            tred = tred - Int(cv / 2 + Rnd * cv)
            tgreen = tgreen - Int(cv / 2 + Rnd * cv)
            tblue = tblue - Int(cv / 2 + Rnd * cv)
            If tred < 0 Then tred = 0
            If tgreen < 0 Then tgreen = 0
            If tblue < 255 Then tblue = 0
            tcolor = _RGB32(tred, tgreen, tblue)
            Line (x + mwid, y + mwid)-(x - 1 + w - mwid, y - 1 + h - mwid), tcolor, BF
            tred = tred + Int(2 + Rnd * cv)
            tgreen = tgreen + Int(2 + Rnd * cv)
            tblue = tblue + Int(2 + Rnd * cv)
            If tred > 255 Then tred = 255
            If tgreen > 255 Then tgreen = 255
            If tblue > 255 Then tblue = 255
            tcolor = _RGB32(tred, tgreen, tblue)
            sv = (10 + Rnd * 20) / 10
            Line (x + (mwid * sv), y + mwid)-(x - 1 + w - mwid, y - 1 + h - (mwid * sv)), tcolor, BF

        Case Else 'highlight brick
            Line (x + mwid, y + mwid)-(x - 1 + w - mwid, y - 1 + h - mwid), tcolor, BF
            tred = tred + Int(Rnd * (cv * .65))
            tgreen = tgreen + Int(Rnd * (cv * .65))
            tblue = tblue + Int(Rnd * (cv * .65))
            If tred > 255 Then tred = 255
            If tgreen > 255 Then tgreen = 255
            If tblue > 255 Then tblue = 255
            tcolor = _RGB32(tred, tgreen, tblue)
            sv = (10 + Rnd * 20) / 10
            Line (x + (mwid * sv), y + mwid)-(x - 1 + w - mwid, y - 1 + h - (mwid * sv)), tcolor, BF
    End Select
End Sub

Sub brickfill (sx, sy, ex, ey, bw, bh, mwid, brickcolor As _Unsigned Long, mortarcolor As _Unsigned Long, cv, crackrange)
    'crackrange is the raw maximum rnd range used to add cracks to the wall
    Dim zag(1 To 10, 1 To 2)
    b = 0
    For y = sy To ey Step bh
        b = b + 1
        For x = sx To ex Step bw
            If b Then
                brick x - (bw \ 2), y, bw, bh, mwid, brickcolor, mortarcolor, cv
            Else
                brick x, y, bw, bh, mwid, brickcolor, mortarcolor, cv
            End If
        Next x
        If b = 1 Then b = -1
    Next y
    cracks = Int(Rnd * crackrange)
    For c = 1 To cracks
        cx = Int(sx + Rnd * (ex - sx)): cy = Int(sy + Rnd * (ey - sy))
        zag(1, 1) = cx: zag(1, 2) = cy
        xshift = Int(-3 + Rnd * 6)
        yshift = Int(-3 + Rnd * 6)
        If xshift = 0 Then xshift = -1
        If yshift = 0 Then yshift = -1
        For z = 2 To 10
            zag(z, 1) = zag(z - 1, 1) + xshift * Int(Rnd * ((ex - sx) / 20))
            zag(z, 2) = zag(z - 1, 2) + yshift * Int(Rnd * ((ey - sy) / 20))
        Next z
        For z = 1 To 9
            If zag(z, 1) > 0 And zag(z, 2) > 0 Then
                If zag(z + 1, 1) <= ex And zag(z + 1, 2) <= ey Then Line (zag(z, 1), zag(z, 2))-(zag(z + 1, 1), zag(z + 1, 2)), _RGB32(90, 90, 90)
            End If
        Next z
    Next c
End Sub
Reply
#2
This would make very good wallpaper for my Linux desktop for a moment. Only if there were a way to change the position and/or spread of the green bricks. Also the attempt to make it look real with the "cracks". Well done.
Reply
#3
Cool, thanks for sharing. It's fast too. Holding down the spacebar shows how quick everything can be drawn. I normally rely on sprites that can be stitched together to create a brick wall like this. But sprites don't offer the variety a custom piece of code like this can.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#4
(03-10-2023, 05:03 PM)mnrvovrfc Wrote: This would make very good wallpaper for my Linux desktop for a moment. Only if there were a way to change the position and/or spread of the green bricks. Also the attempt to make it look real with the "cracks". Well done.

Thanks. Just change the variables and you and move the brickfill to anywhere on a screen or target image.
Reply
#5
(03-10-2023, 05:04 PM)TerryRitchie Wrote: Cool, thanks for sharing. It's fast too. Holding down the spacebar shows how quick everything can be drawn. I normally rely on sprites that can be stitched together to create a brick wall like this. But sprites don't offer the variety a custom piece of code like this can.

I'm planning on using it for the filled in portions of a maze. Changing the bricks form area to area or level to level will break up visual monotony.
Reply
#6
Are there any screenshots?
Tread on those who tread on you

Reply
#7
(03-10-2023, 07:03 PM)Balderdash Wrote: Are there any screenshots?

Sure, it's bricks:   
[Image: image.png]
Reply




Users browsing this thread: 1 Guest(s)