Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draw Worms Study
#13
About to leave for a new years day cookout, but wanted to share a little worm saver before leaving.  

Hope you guys have a great day!

- Dav

Code: (Select All)

Option _Explicit

Randomize Timer

Screen _NewImage(1000, 640, 32)

Dim worms, wormmin, wormmax, i

worms = 50 'number of worms on screen
wormmin = 7 'smallest size a worm can get
wormmax = 16 'largest size an worm can get

Dim wormx(worms), wormy(worms), wormsiz(worms), wormgro(worms), wormdir(worms)
Dim wormred(worms), wormgrn(worms), wormblu(worms)

'generate worm values
For i = 1 To worms
    wormx(i) = Rnd * _Width 'x pos
    wormy(i) = Rnd * _Height 'y pos
    wormsiz(i) = wormmin + (Rnd * wormmax - wormmin) 'worm size
    wormgro(i) = Int(Rnd * 2) 'way worm is changing, 0=shrinking, 1=growing
    wormdir(i) = Int(Rnd * 4) 'random direction worm can drift (4 different ways)
    wormred(i) = Rnd * 255 'red color
    wormgrn(i) = Rnd * 255 'grn color
    wormblu(i) = Rnd * 255 'blu color
Next



Do

    For i = 1 To worms

        fc wormx(i), wormy(i), wormsiz(i), _RGB(wormred(i), wormgrn(i), wormblu(i)), 1

        '== change worm values

        'if worm is shrinking, subtract from size, else add to it
        If wormgro(i) = 0 Then
            wormsiz(i) = wormsiz(i) - .1
        Else
            wormsiz(i) = wormsiz(i) + .1
        End If

        'if worm reaches maximum size, switch growth value to 0 to start shrinking now
        If wormsiz(i) >= wormmax Then wormgro(i) = 0
        'if worm reaches minimum size, switch growth value to 1 to start growing now
        If wormsiz(i) <= wormmin Then wormgro(i) = 1

        'move worm in  1 of 4 directions we generated, and +x,-x,+y,-y to it.
        If wormdir(i) = 0 Then wormx(i) = wormx(i) + 2 'drift right
        If wormdir(i) = 1 Then wormx(i) = wormx(i) - 2 'drift left
        If wormdir(i) = 2 Then wormy(i) = wormy(i) + 2 'drift down
        If wormdir(i) = 3 Then wormy(i) = wormy(i) - 2 'drift up

        'change drifting direction now and then
        If Int(Rnd * 25) = 1 Then wormdir(i) = Int(Rnd * 4)

        'this creates the shakiness. randomly adjust x/y positions by +/-2 each step
        If Int(Rnd * 2) = 0 Then wormx(i) = wormx(i) + 2 Else wormx(i) = wormx(i) - 2
        If Int(Rnd * 2) = 0 Then wormy(i) = wormy(i) + 2 Else wormy(i) = wormy(i) - 2

        'below handles if worm goes off screen, let it dissapear completely
        If wormx(i) > _Width + wormsiz(i) Then wormx(i) = -wormsiz(i)
        If wormx(i) < -wormsiz(i) Then wormx(i) = _Width + wormsiz(i)
        If wormy(i) > _Height + wormsiz(i) Then wormy(i) = -wormsiz(i)
        If wormy(i) < -wormsiz(i) Then wormy(i) = _Height + wormsiz(i)

    Next

    _Display
    _Limit 30

Loop Until InKey$ <> ""


Sub fc (cx As Integer, cy As Integer, radius As Integer, clr~&, grad)

    Dim red, grn, blu, alpha, r2, y, x, i, dis, red2, grn2, blu2, clr2~&


    If radius = 0 Then Exit Sub ' safety bail
    If grad = 1 Then
        red = _Red32(clr~&)
        grn = _Green32(clr~&)
        blu = _Blue32(clr~&)
        alpha = _Alpha32(clr~&)
    End If
    r2 = radius * radius
    For y = -radius To radius
        x = Sqr(r2 - y * y)
        ' If doing gradient
        If grad = 1 Then
            For i = -x To x
                dis = Sqr(i * i + y * y) / radius
                red2 = red * (1 - dis) + (red / 2) * dis
                grn2 = grn * (1 - dis) + (grn / 2) * dis
                blu2 = blu * (1 - dis) + (blu / 2) * dis
                clr2~& = _RGBA(red2, grn2, blu2, alpha)
                Line (cx + i, cy + y)-(cx + i, cy + y), clr2~&, BF
            Next
        Else
            Line (cx - x, cy + y)-(cx + x, cy + y), clr~&, BF
        End If
    Next
End Sub

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
Draw Worms Study - by bplus - 01-01-2026, 02:02 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 02:16 PM
RE: Draw Worms Study - by SMcNeill - 01-01-2026, 02:32 PM
RE: Draw Worms Study - by Dav - 01-01-2026, 02:43 PM
RE: Draw Worms Study - by SMcNeill - 01-01-2026, 02:46 PM
RE: Draw Worms Study - by Dav - 01-01-2026, 02:53 PM
RE: Draw Worms Study - by SMcNeill - 01-01-2026, 02:53 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 03:15 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 03:00 PM
RE: Draw Worms Study - by Dav - 01-01-2026, 03:01 PM
RE: Draw Worms Study - by SMcNeill - 01-01-2026, 03:23 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 03:35 PM
RE: Draw Worms Study - by Dav - 01-01-2026, 03:46 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 05:39 PM
RE: Draw Worms Study - by Pete - 01-01-2026, 07:18 PM
RE: Draw Worms Study - by bplus - 01-01-2026, 08:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Another way to draw rounded rectangles James D Jarvis 4 1,342 10-09-2024, 07:11 PM
Last Post: James D Jarvis
  Draw that Circle James D Jarvis 17 3,318 08-28-2022, 06:29 AM
Last Post: justsomeguy
  Draw circles James D Jarvis 5 1,501 06-16-2022, 12:09 PM
Last Post: James D Jarvis

Forum Jump:


Users browsing this thread: 1 Guest(s)