Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - bplus

Pages: 1 2 3 [4] 5 6 ... 21
46
Programs / Play Offs Chart - Recursion Example
« on: November 02, 2021, 11:29:20 am »
Get ready for Play Offs:
Code: QB64: [Select]
  1. _Title "Play Offs Chart - Recursion example" 'b+ 2021-10-27
  2. '2021-11-02 A few mods and ready for the playoffs
  3.  
  4. 'Recursion is a sub or function that calls itself until it's job is done
  5. 'Here is a grahics
  6. Const pi = _Pi
  7. Screen _NewImage(700, 700, 32)
  8.  
  9. recThis 700, 350, 350 / 2, 0
  10.  
  11. Sub recThis (x, y, arm, level As Integer)
  12.     'first thing to ask in recursive subroutine is are we done!!!
  13.     If arm < 2 Then Exit Sub ' recursion is finished
  14.     ' no not done
  15.     If level Mod 2 Then
  16.         x1 = x + 1.4 * arm * Cos(pi / 2): y1 = y + 1.4 * arm * Sin(pi / 2)
  17.         x2 = x + 1.4 * arm * Cos(1.5 * pi): y2 = y + 1.4 * arm * Sin(1.5 * pi)
  18.         Line (x1, y1)-(x2, y2)
  19.         recThis x1, y1, .7 * arm, level + 1
  20.         recThis x2, y2, .7 * arm, level + 1
  21.     Else
  22.         x1 = x: y1 = y
  23.         x2 = x + 100 * Cos(pi): y2 = y + 100 * Sin(pi)
  24.         Line (x1, y1)-(x2, y2)
  25.         recThis x2, y2, .7 * arm, level + 1
  26.     End If
  27.  

 
Play Offs Chart.PNG

47
Programs / Recursive Vrs NonRecursive test with Quicksort
« on: November 01, 2021, 08:29:10 am »
Seeing Sanmayce Quicksort test with my recursive Quicksort I had to try my own test without using the array in the parameters/arguments call, plus in the Discussion Board Recursion topic brought up some ideas from Luke on recursion that I've been curious experimenting with for some time.

So here's my quick test:
Code: QB64: [Select]
  1. _Title "Recursive Vrs NonRecursive test with Quicksort" 'b+ 2021-11-01
  2. ArrayHigh = 100000000
  3. ReDim Shared As _Unsigned _Integer64 n(1 To ArrayHigh), m(1 To ArrayHigh)
  4.  
  5. For i = 1 To ArrayHigh
  6.     n(i) = i
  7. For i = ArrayHigh To 2 Step -1 ' Fisher Yates Shuffle
  8.     Swap n(i), n(Int(Rnd * i) + 1)
  9.     m(i) = n(i) ' copy m off n
  10. Print "Test arrays ready. Testing Recursive QuickSort first."
  11. m(1) = n(1)
  12. T# = Timer(.001)
  13. QuickSort 1, ArrayHigh
  14. QuickSortT# = Timer(.001) - T#
  15. For i = 1 To ArrayHigh
  16.     If n(i) <> i Then Beep: Print "Error in QuickSort array n."
  17. Print "QuickSort checked, QuickSort time was"; QuickSortT#
  18.  
  19. T# = Timer(.001)
  20. Quicksort_QB64
  21. QuicksortQB64T# = Timer(.001) - T#
  22. For i = 1 To ArrayHigh
  23.     If m(i) <> i Then Beep: Print "Error in Quicksort__QB64 array m."
  24. Print "Quicksort_QB64 checked, Quicksort_QB64 time was"; QuicksortQB64T#
  25.  
  26. ' recursive
  27. Sub QuickSort (start As _Unsigned _Integer64, finish As _Unsigned _Integer64)
  28.     Lo = start: Hi = finish
  29.     Middle = n((Lo + Hi) \ 2)
  30.     Do
  31.         Do While n(Lo) < Middle: Lo = Lo + 1: Loop
  32.         Do While n(Hi) > Middle: Hi = Hi - 1: Loop
  33.         If Lo <= Hi Then
  34.             Swap n(Lo), n(Hi)
  35.             Lo = Lo + 1: Hi = Hi - 1
  36.         End If
  37.     Loop Until Lo > Hi
  38.     If Hi > start Then QuickSort start, Hi
  39.     If Lo < finish Then QuickSort Lo, finish
  40.  
  41. ' non recursive
  42. ' ref:  https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#QB64
  43. Sub Quicksort_QB64
  44.     Left = LBound(m)
  45.     Right = UBound(m)
  46.     LeftMargin = Left
  47.     ReDim Stack&&(Left To Right)
  48.     StackPtr = 0
  49.     StackPtr = StackPtr + 1
  50.     Stack&&(StackPtr + LeftMargin) = Left
  51.     StackPtr = StackPtr + 1
  52.     Stack&&(StackPtr + LeftMargin) = Right
  53.     Do 'Until StackPtr = 0
  54.         Right = Stack&&(StackPtr + LeftMargin)
  55.         StackPtr = StackPtr - 1
  56.         Left = Stack&&(StackPtr + LeftMargin)
  57.         StackPtr = StackPtr - 1
  58.         Do 'Until Left >= Right
  59.             Pivot~&& = m((Left + Right) \ 2)
  60.             Indx = Left
  61.             Jndx = Right
  62.             Do
  63.                 Do While (m(Indx) < Pivot~&&)
  64.                     Indx = Indx + 1
  65.                 Loop
  66.                 Do While (m(Jndx) > Pivot~&&)
  67.                     Jndx = Jndx - 1
  68.                 Loop
  69.                 If Indx <= Jndx Then
  70.                     If Indx < Jndx Then Swap m(Indx), m(Jndx)
  71.                     Indx = Indx + 1
  72.                     Jndx = Jndx - 1
  73.                 End If
  74.             Loop While Indx <= Jndx
  75.             If Indx < Right Then
  76.                 StackPtr = StackPtr + 1
  77.                 Stack&&(StackPtr + LeftMargin) = Indx
  78.                 StackPtr = StackPtr + 1
  79.                 Stack&&(StackPtr + LeftMargin) = Right
  80.             End If
  81.             Right = Jndx
  82.         Loop Until Left >= Right
  83.     Loop Until StackPtr = 0
  84.  
  85.  

Nice non recursion code, doing it with your own stack is interesting. I don't suppose there's a way to do a recursive quicksort without need of any parameters to call? Hmm...


EDIT: Dang it! I never use Call this is what I get for copying what I thought was my code from someone else post. :(
EDIT: Fix a labeling if error found in 2nd test.

48
Programs / Honeycombs _ Rosetta Code
« on: October 30, 2021, 02:33:20 pm »
ref:  http://rosettacode.org/wiki/Honeycombs

Here is updated translation and overhaul I made today:
Code: QB64: [Select]
  1. _Title "Honeycombs - Rosetta Code" ' b+ 2021-10-30 trans from
  2. ' Honeycomb Rosetta.txt for JB v2 2018-11-24 B+ finish Rosetta Challenge
  3. ' 2021-10-30 complete overhaul except for grid drawing numbers and names.
  4.  
  5. ' ===================== Try alternate Cols and Rows for HoneyComb ===============================
  6.  
  7. Const Cols = 5, Rows = 4, LMax = Cols * Rows ' hex grid  <<<<<<<<<<<<<<  as Rosetta Requires
  8. 'Const Cols = 4, Rows = 5, LMax = Cols * Rows ' hex grid <<<<<<<<<<<<<< reverse
  9. 'Const Cols = 6, Rows = 5, LMax = Cols * Rows ' hex grid <<<<<<<<<<<<<< More than just Letters
  10. 'Const Cols = 13, Rows = 2, LMax = Cols * Rows ' hex grid do exactly whole alphabet!
  11.  
  12.  
  13. ' global   SELECTED$ ' all caps for globals
  14. Const Pi3 = _Pi / 3, Sqr3 = Sqr(3), Side = 30 '           constants for Hexagon making
  15. Const Xoff = 100 - 1.5 * Side, Yoff = 100 - Side * Sqr3 ' center grid with offsets on screen
  16. Const XMax = 2 * Xoff + Cols * 1.5 * Side + 1.5 * Side '  screen display size needed
  17. Const YMax = 2 * Yoff + (Rows + 2) * Side * Sqr3
  18.  
  19. Dim Shared L$(LMax), LX(LMax), LY(LMax), LSELECTED(LMax), Selected$ ' save letter and hex center positions by index
  20.  
  21. Randomize Timer ' get new grid when we start
  22. For i = 1 To LMax ' get letters array loaded
  23.     L$(i) = Chr$(64 + i)
  24. For i = LMax To 2 Step -1 ' shuffle letters Fisher - Yates
  25.     Swap L$(i), L$(Int(Rnd * i) + 1)
  26.  
  27. Screen _NewImage(XMax, YMax, 32) 'graphics custom size RGBA colors
  28. _Delay .25 ' get screen loaded before trying to move
  29. _ScreenMove _Middle ' center  screen
  30.  
  31. f& = _LoadFont("Consolab.ttf", 40) ' >>>>>>>>>>>> from Windows 10 Fonts
  32. Color , _RGB32(200, 200, 220): Cls ' draw grid before selections
  33. For y = 1 To Rows ' initialize screen with grid
  34.     For x = 1 To Cols
  35.         n = n + 1 ' index hex buttons
  36.         If x Mod 2 = 0 Then yoff2 = .5 * Side * Sqr3 Else yoff2 = 0 ' is this column lower than first column?
  37.         cx = x * 1.5 * Side + Xoff: cy = y * Side * Sqr3 + Yoff + yoff2 ' calc hex key centers
  38.         LX(n) = cx: LY(n) = cy ' save hex key center positions
  39.         drawHex n, 0 ' draw key
  40.     Next
  41.  
  42. While _KeyDown(27) = 0 ' allow user to select letters by key or mouse
  43.     k$ = InKey$
  44.     If Len(k$) Then
  45.         For i = 1 To LMax 'if so was it selected already? or select it.
  46.             If UCase$(k$) = L$(i) And LSELECTED(i) = 0 Then drawHex i, 1: Exit For
  47.         Next
  48.     End If
  49.     While _MouseInput: Wend ' polls mouse
  50.     mx = _MouseX: my = _MouseY: mb = _MouseButton(1)
  51.     If mb Then
  52.         For i = 1 To LMax ' is distance from click within radius of non-selected button?
  53.             If Sqr((mx - LX(i)) ^ 2 + (my - LY(i)) ^ 2) <= Side * .5 * Sqr3 And LSELECTED(i) = 0 Then drawHex i, 1
  54.         Next
  55.     End If
  56.  
  57. Sub drawHex (i, selectedTF)
  58.     If selectedTF = 0 Then
  59.         Color _RGB32(255, 0, 0), _RGB32(255, 255, 0)
  60.     Else
  61.         Color _RGB32(0, 0, 0), _RGB32(255, 0, 255)
  62.     End If
  63.     For a = 0 To 6
  64.         x1 = LX(i) + Side * Cos(a * Pi3): y1 = LY(i) + Side * Sin(a * Pi3)
  65.         If a > 0 Then Line (lastx, lasty)-(x1, y1), _RGB32(0, 0, 0)
  66.         lastx = x1: lasty = y1
  67.     Next
  68.     Paint (LX(i), LY(i)), _BackgroundColor, _RGB32(0, 0, 0)
  69.     _PrintString (LX(i) - 10, LY(i) - 13), L$(i)
  70.     If selectedTF Then ' show the order of selection
  71.         Color _RGB32(0, 0, 0), _RGB32(200, 200, 220)
  72.         LSELECTED(i) = 1
  73.         Selected$ = Selected$ + L$(i)
  74.         centerText 0, _Width, _Height - 60, Mid$(Selected$, 1, Int(LMax / 2))
  75.         If Len(Selected$) > 10 Then
  76.             centerText 0, _Width, _Height - 20, Mid$(Selected$, Int(LMax / 2) + 1) 'are we done yet? are all letters selected?
  77.             If Len(Selected$) = LMax Then Beep ' all have been selected
  78.         End If
  79.     End If
  80.  
  81. Sub centerText (x1, x2, midy, s$) ' ' if you want to center fit a string between two goal posts x1, and x2
  82.     _PrintString ((x1 + x2) / 2 - _PrintWidth(s$) / 2, midy - _FontHeight(_Font) / 2), s$
  83.  

Here's what it looked like in Just Basic 3 years ago:
Code: [Select]
'Honeycomb Rosetta.txt for JB v2 2018-11-24 B+ finish Rosetta Challenge

global XMAX, YMAX, PI3, SQR3, SIDE, LMAX, SELECTED$, ROWS, YOFF  ' all caps for globals
PI3 = acs(-1)/3 : SQR3 = sqr(3) : SIDE = 30                      ' constants for Hexagon making
cols = 5 : ROWS = 4 : LMAX = cols * ROWS                         ' hex grid size
dim L$(LMAX), LX(LMAX), LY(LMAX), LSELECTED(LMAX)                ' save letter and hex center positions by index
xoff = 100 - 1.5 * SIDE : YOFF = 100 - SIDE * SQR3               ' center grid with offsets
for i = 1 to LMAX : L$(i) = chr$(64 + i) : next                  ' get letters array loaded
for i = LMAX to 2 step -1                                        ' shuffle letters Fisher - Yates
    r = int(rnd(0) * i) + 1                                      ' random number up to i place
    t$ = L$(r) : L$(r) = L$(i) : L$(i) = t$                      ' swap
next
XMAX = 2*xoff + cols*1.5*SIDE + 1.5*SIDE                         'screen display size needed
YMAX = 2*YOFF + (ROWS + 2)*SIDE*SQR3
nomainwin                                                        ' Window prep before open
WindowWidth = XMAX + 8                                           ' size
WindowHeight = YMAX + 32
UpperLeftX = (DisplayWidth - XMAX) / 2                           ' top left corner, center window
UpperLeftY = (DisplayHeight - YMAX) / 2
open "Honeycomb - Rosetta Challenge" for graphics_nsb_nf as #gr  ' open with title
#gr "setfocus"                                                   ' catch keys and mouse
#gr "trapclose quit"                                             ' set exit code sub
#gr "when leftButtonUp lButtonUp"                                ' set mouse click up sub
#gr "when characterInput charIn"                                 ' set keppress sub
#gr "font consolus bold 20"                                      ' set font
#gr "down"                                                       ' pen ready to draw
for y = 1 to ROWS                                                ' initialize screen with grid
    for x = 1 to cols
        n = n + 1                                                ' index hex buttons
        if x mod 2 = 0 then yoff2 = .5*SIDE*SQR3 else yoff2 = 0  ' is this column lower than first column?
        cx = x*1.5*SIDE + xoff : cy = y*SIDE*SQR3 + YOFF + yoff2 ' calc hex key centers
        LX(n) = cx : LY(n) = cy                                  ' save hex key center positions
        call drawHex n, "green"                                  ' draw key
    next
next
#gr "flush"
wait
sub charIn H$, c$                                                'is c$, the key pressed, one of the letters in L$?
    for i = 1 to LMAX                                            'if so was it selected already? or select it.
        if upper$(c$) = L$(i) and LSELECTED(i) = 0 then call drawHex i, "blue" : #gr "flush" : exit sub
    next
end sub
sub lButtonUp H$, mx, my                                         ' mx, my are mouse button release locations
    for i = 1 to LMAX                                            ' is distance from click within radius of non-selected button?
        if sqr((mx-LX(i))^2+(my-LY(i))^2) <= SIDE*.5*SQR3 and LSELECTED(i) = 0 then call drawHex i, "blue" : #gr "flush" : exit sub
    next
end sub
sub drawHex i, c$
    #gr "size 1"
    #gr "backcolor ";c$                                          ' color location
    #gr "place ";LX(i) + .5;" ";LY(i);"; circlefilled ";SIDE * .5 * SQR3
    #gr "color black"                                            ' color letter and grid, thick line #10
    #gr "size 10"
    call Hex LX(i), LY(i)                                        ' draw hexagon cell
    call stext LX(i) - 10, LY(i) + 11, L$(i)                     ' draw letter
    if c$ = "blue" then                                          ' update Selection tracking and display
        LSELECTED(i) = 1
        SELECTED$ = SELECTED$ + L$(i)
        call stext 0, (ROWS + 2) * SIDE * SQR3 + YOFF, SELECTED$ 'are we done yet? are all letters selected?
        if len(SELECTED$) = LMAX then notice "All the keys have been used. Goodbye" : call quit "#gr"
    end if
end sub
sub Hex x0, y0                                                    ' draw hexagon around x0, y0 with 6 lines
    for i = 0 to 6
        x1 = x0 + SIDE * cos(i * PI3) : y1 = y0 + SIDE * sin(i * PI3)
        if i > 0 then #gr "line ";lastx;" ";lasty;" ";x1;" ";y1
        lastx = x1 : lasty = y1
    next
end sub
sub stext x, y, message$                                          ' note: y is the bottom edge not top
    #gr "place ";x;" ";y;";|";message$                            ' print message at x, y
end sub
sub quit H$                                                       ' close window ie click top right x box
    close #H$ : end
end sub

Just Basic has a very different way of doing screens and graphics.

49
Programs / One Key Creep Out for Halloween
« on: October 25, 2021, 11:20:29 pm »
One Key Creep Out for Halloween - takes a little practice. With the paddle moving a Spacebar press stops paddle another press, reverses direction and moves again, another press stops, another reverses direction and moves again.

Hint: overshoot paddle target a bit if off you will start going in correct direction with next spacebar press.

Code: QB64: [Select]
  1. _Title "One Key Creep Out for Halloween" 'B+  from Creep Out started 2021-08-18  Breakout with Spiders
  2. ' 2021-08-20A more sounds for breaking bricks font to replace text sub
  3. ' 2021-10-25 fix up Menu now that I know more about Fonts, one key = Spacebar
  4. ' Spacebar toggles menu choices and moving\stopping the paddle.
  5.  
  6. ' =============================  Paddle Play with Spacebar Only! =================================
  7.  
  8. ' Paddle on each Spacebar press: Moves Stops Reverses Stops Reverses Stops Reverses Stops...
  9.  
  10. ' Hint: over shoot paddle placement so if too much you will start right up again going back!
  11.  
  12. ' ================================================================================================
  13.  
  14.  
  15. Const xmax = 700 '<==== drawing area width
  16. Const ymax = 560 '<==== drawing area height
  17.  
  18. 'colors used
  19. Const red = &HFFEE0033
  20. Const orange = &HFFFF8400
  21. Const green = &HFF008000
  22. Const yellow = &HFFFFFF00
  23. Const silver = &HFFD0C6C6
  24. Const white = &HFFFFFFFF
  25. Const black = &HFF000000
  26.  
  27. ' wall is 50 pixels X 14 columns wide = 700 make screen width
  28. ' wall is 20 pixels X 8  rows = 160 = 1/3 screen height = 480 + 20  paddle height
  29. ' under paddle track score and lifes on one line padded by blank lines 540 = 27
  30. ' so total height 480 to paddle 500 + 60 for 3 lines = 560 (text height 20)
  31.  
  32. Const br = 10 ' ball radius
  33. Const bkw = 50 ' brick width
  34. Const bkh = 20 ' brick height
  35.  
  36. Const nSpinners = 112
  37. Const air_resistance = .1
  38.  
  39. Type Object
  40.     x As Single
  41.     y As Single
  42.     dx As Single
  43.     dy As Single
  44.     sz As Single
  45.     c As _Unsigned Long
  46.     dead As Long
  47.  
  48. Dim Shared As Integer nS, pOFF, dMode
  49. Dim Shared dots(2000) As Object
  50.  
  51. ' Sound sources mainly Sound Bible picks from both johnno56 and myself here: https://soundbible.com
  52. ' from Cobalt's advanced version of my eRATication, rar here: https://www.qb64.org/forum/index.php?topic=370.msg2677#msg2677
  53. ' from Filleppes Cloned Shades here: https://www.qb64.org/forum/index.php?topic=1262.msg104706#msg104706
  54.  
  55. Dim Shared As Long mush, alive, laser, whistle, crunch, pop, uh, gong
  56. mush = _SndOpen("mush.wav") ' brick 0 breaking        johnno soundbible
  57. alive = _SndOpen("life.wav") ' brick 1 breaking       Colbalt eRATication
  58. laser = _SndOpen("laser.wav") ' brick 2 breaking      mark soundbible
  59. whistle = _SndOpen("whistle.ogg") ' brick 3 fellippe  Clone-Shades master
  60. crunch = _SndOpen("crunch.wav") ' ball over spider    johnno  soundbible
  61. pop = _SndOpen("pop.wav") ' bouce off wall or paddle  mark soundbible
  62. uh = _SndOpen("playerdie.mp3") ' paddle miss          Colbalt eRATication
  63. gong = _SndOpen("gong.wav") ' complete a screen       mark soundbible
  64. 'Print mush, alive, laser, whistle 'ok
  65. 'Print crunch, pop, uh, gong 'ok
  66. 'End
  67.  
  68. Screen _NewImage(xmax, ymax, 32)
  69.  
  70. '_FullScreen
  71. ' OR   >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You can choose Full Screen or Centered on screen 700 x 560
  72. _Delay .25
  73.  
  74. Color white, black
  75. f& = _LoadFont("spiders.ttf", 64) ' License included in zip package
  76.  
  77. Dim Shared bx, by, dx, dy, px, py, pw, plf, prt, pf, score, life, hits, obk, rbk, speedups
  78. bx = 0 ' ball x position
  79. by = 0 ' ball y position
  80. dx = 0 ' ball horizontal change
  81. dy = 0 ' ball vertical change
  82.  
  83. restart:
  84. px = 350 ' paddle x, y
  85. py = 450 ' fix paddle sticking at 480
  86. pw = 70 ' paddle width,  100 wide to start half that at certain point
  87. plf = 0 ' paddle left side
  88. prt = 0 ' paddle right side
  89. pf = 1 ' paddle fraction that gets changed according menu choice easy .75, hard .5
  90. nS = 0 ' number of spinners
  91. score = 0
  92. life = 5 '  (or balls left) only 3 allowed according to wiki
  93. hits = 0 ' bricks busted
  94. obk = 0 ' first orange brick hits bool
  95. rbk = 0 ' first red brick hit bool, when this happens paddle width is cut in half!
  96. speedups = 0 ' count, bump up dy when hits = 4 and 8, then with first orange, then with first red
  97. scrn = 0
  98.  
  99. Dim Shared wc(13, 7), wp(13, 7) 'brick wall colors, brick wall points according to color
  100. ' get 448 points clear 1 screen/wall, perfect game is clearing 2 screens/walls
  101. ReDim Shared s(1 To nSpinners) As Object
  102.  
  103. 'Menu
  104. initwall
  105. life = 0
  106. my = 0: t = Timer
  107. While life = 0
  108.     drawtable
  109.     Line (.23 * xmax, .14 * ymax)-(.77 * xmax, .81 * ymax), &HFF0000FF, BF
  110.     Line (.24 * xmax, .15 * ymax)-(.76 * xmax, .8 * ymax), &HFF880000, BF
  111.     Line (.245 * xmax, .16 * ymax)-(.755 * xmax, .79 * ymax), &HFFFFFFFF, B
  112.     Color , &HFF880000
  113.     _PrintString (.257 * xmax, .2 * ymax), "Creep Out Menu"
  114.     _PrintString (.44 * xmax, .2 * ymax + 110), "Easy"
  115.     _PrintString (.44 * xmax, .2 * ymax + 180), "Hard"
  116.     _PrintString (.44 * xmax, .2 * ymax + 250), "Quit"
  117.     Color , &HFF000000
  118.     'While _MouseInput: Wend
  119.     'mx = _MouseX: my = _MouseY: mb = _MouseButton(1)
  120.     drawSpinner xmax * .35, .2 * ymax + 75 + my * 70, .5, 0, &HFF006600
  121.     If InKey$ = " " Then
  122.         t = Timer
  123.         my = my + 1
  124.         If my > 3 Then my = 0
  125.     End If
  126.     If Timer - t > 2 And my > 0 Then
  127.         If my = 1 Then
  128.             life = 5: pf = .75
  129.         ElseIf my = 2 Then 'hard
  130.             life = 3: pf = .5
  131.         ElseIf my = 3 Then
  132.             System 'quit
  133.         End If
  134.     End If
  135.     _Limit 40
  136.     _Display
  137. Color , black
  138. drawtable
  139. initball 'set dx, dy, bx, by ball position and change
  140. updatescore
  141. dMode = 1
  142. While life And _KeyDown(27) = 0
  143.     drawtable
  144.     drawpaddle
  145.     drawball
  146.     handleSpinners
  147.     updatescore
  148.     If hits = 112 And scrn = 0 Then 'setup new
  149.         _SndPlay gong
  150.         _Delay 1
  151.         scrn = 1
  152.         speedups = 0: obk = 0: rbk = 0: pw = 50
  153.         nS = 0
  154.         ReDim s(1 To nSpinners) As Object
  155.         initwall
  156.         drawtable
  157.         initball
  158.     Else
  159.         If hits = 224 Then
  160.             _SndPlay gong
  161.             Color white
  162.             'Text 65, 170, 32, white, "Congratulations on a perfect score!!!"
  163.             _PrintString (35, 175), "Perfect Score!"
  164.             _Delay 3
  165.             Exit While
  166.         End If
  167.     End If
  168.     _Display
  169.     _Limit 20 '< adjust as needed for speed of your system
  170. Color white
  171. 'Text 260, 290, 48, white, "Game Over"
  172. centerText 0, xmax, ymax / 2, "Game Over"
  173. GoTo restart
  174.  
  175. Sub initwall
  176.     Dim cr As _Unsigned Long
  177.     For r = 0 To 7
  178.         Select Case r
  179.             Case 0, 1: cr = red: p = 7
  180.             Case 2, 3: cr = orange: p = 5
  181.             Case 4, 5: cr = green: p = 3
  182.             Case 6, 7: cr = yellow: p = 1
  183.         End Select
  184.         For c = 0 To 13
  185.             wc(c, r) = cr: wp(c, r) = p
  186.         Next
  187.     Next
  188.  
  189. Sub initball 'set ball in play with location and dx, dy
  190.     bx = 350
  191.     by = 280
  192.     dx = rand(1, 4)
  193.     If rand(0, 1) Then dx = -1 * dx
  194.     dy = (3 + speedups) * -1
  195.     px = bx
  196.  
  197. Sub drawtable ' in JB don't want to redraw this every loop
  198.     Cls
  199.     For r = 0 To 7
  200.         For c = 0 To 13
  201.             If wp(c, r) Then
  202.                 For i = 1 To 10
  203.                     'underneath
  204.                     Color _RGB32(120, 60, 60)
  205.                     Line (c * bkw + i, r * bkh + bkh + i)-(c * bkw + bkw + i, r * bkh + bkh + i)
  206.                     Color silver
  207.                     PSet (c * bkw + i, r * bkh + bkh + i)
  208.                     'ink(white)
  209.                     'sidewall
  210.                     Line (c * bkw + bkw + i, r * bkh + i)-(c * bkw + bkw + i, r * bkh + bkh + i)
  211.                 Next
  212.                 Color wc(c, r)
  213.                 Line (c * bkw, r * bkh)-(c * bkw + bkw, r * bkh + bkh), , BF
  214.                 Color white
  215.                 Line (c * bkw, r * bkh)-(c * bkw + bkw, r * bkh + bkh), , B
  216.             End If
  217.         Next
  218.     Next
  219.     'Line (0, 300)-(xmax, 490), &HFF000019, BF ' mouse paddle limit
  220.  
  221. Sub drawpaddle ' update paddle to mouseY, paddle top and bottom are global
  222.  
  223.     'While _MouseInput: Wend
  224.     'px = _MouseX 'update paddle location
  225.     'If _MouseY >= 300 And _MouseY < 480 Then py = _MouseY
  226.     py = 440
  227.     'If InKey$ = " " Then   ' reverse every time you hit spacebar
  228.     '    dMode = 1 - dMode
  229.     '    _KeyClear
  230.     'End If
  231.     'If dMode Then px = px + 10 Else px = px - 10
  232.     'If px < -pw Then px = -pw
  233.     'If px > xmax + pw Then px = xmax + pw
  234.  
  235.     ' stop reverse stop reverse stop reverse
  236.     If InKey$ = " " Then ' reverse every time you hit spacebar  1 0 -1 0 1 0 -1...
  237.         dMode = dMode + 1
  238.         If dMode = 4 Then dMode = 0
  239.         _KeyClear
  240.     End If
  241.     If dMode = 1 Then px = px - 10
  242.     If dMode = 3 Then px = px + 10
  243.     If px < -pw Then px = -pw + 1
  244.     If px > xmax + pw Then px = xmax + pw - 1
  245.  
  246.     plf = px - pw
  247.     prt = px + pw
  248.     For i = 1 To 10
  249.         Color _RGB32(120, 60, 60)
  250.         Line (px - pw + i, py + 10 + i)-(px + pw + i, py + 10 + i), _RGB32(120, 60, 60)
  251.         Color silver
  252.         'PSet (c * bkw + i, r * bkh + bkh + i)
  253.         'ink(white)
  254.         Line (px + pw + i, py + i)-(px + pw + i, py + 10 + i), silver
  255.     Next
  256.     Line (px - pw, py)-(px + pw, py + 10), &HFFAA5533, BF
  257.  
  258. Sub drawball
  259.     'update
  260.     bx = bx + dx
  261.     If bx < br Then dx = dx * -1: bx = br + 1: _SndPlay pop
  262.     If bx > xmax - br Then dx = dx * -1: bx = xmax - br - 1: _SndPlay pop
  263.  
  264.     by = by + dy
  265.     If by + br > py Then 'ball past paddle line
  266.         by = py - br 'don't let ball go into paddle or goal
  267.         If bx + br < plf Or bx - br > prt Then 'paddle miss
  268.             life = life - 1
  269.             _SndPlay uh
  270.             ' if life = 0 then end game
  271.             updatescore
  272.             silverball bx, by
  273.             _Delay 2.5 'reflect on position of ball and loss of life
  274.             CircleFill bx, by, br, black
  275.             initball 'get ball rolling again
  276.         Else 'paddle hit  ' redo according to distance from paddle center
  277.             _SndPlay pop
  278.             dy = dy * -1
  279.             If bx < px Then
  280.                 per = .5 * (px - bx) / pw
  281.                 dx = dx - 6 * per
  282.             ElseIf bx > px Then
  283.                 per = .5 * (bx - px) / pw
  284.                 dx = dx + 6 * per
  285.                 'Else  dx remains same
  286.             End If
  287.             'dx = dx + rand(-2, 2)
  288.             If dx > 7 Then: dx = 7:
  289.             If dx < -7 Then dx = -7
  290.         End If
  291.     Else
  292.         If by - br < 0 Then 'ball hits back border, reverse direction
  293.             _SndPlay pop
  294.             by = br: dy = dy * -1
  295.         Else
  296.             If by - br < 160 Then 'in wall area, what row and column?
  297.                 starthits = hits
  298.                 'maybe should check all 4 corners or smaller ball
  299.                 row = Int((by - br) / bkh): col = Int((bx - br) / bkw)
  300.                 handleBall row, col
  301.                 row = Int((by - br) / bkh): col = Int((bx + br) / bkw)
  302.                 handleBall row, col
  303.                 row = Int((by + br) / bkh): col = Int((bx - br) / bkw)
  304.                 handleBall row, col
  305.                 row = Int((by + br) / bkh): col = Int((bx + br) / bkw)
  306.                 handleBall row, col
  307.                 If hits <> starthits Then: dy = dy * -1: 'reverse ball direction
  308.             End If
  309.         End If
  310.     End If
  311.     silverball bx, by
  312.  
  313. Sub handleBall (row, col)
  314.     If 0 <= row And row <= 7 And 0 <= col And col <= 13 Then
  315.         If wp(col, row) Then 'brick just hit, lot's to do before update ball
  316.             hits = hits + 1
  317.             sp = rand(0, 3)
  318.             Select Case sp
  319.                 Case 0: _SndPlay mush
  320.                 Case 1: _SndPlay alive
  321.                 Case 2: _SndPlay laser
  322.                 Case 3: _SndPlay whistle
  323.             End Select
  324.             nS = nS + 1
  325.             newSpinner nS, col, row
  326.             If hits = 4 Or hits = 8 Or hits = 116 Or hits = 120 Then
  327.                 speedups = speedups + 1
  328.                 If dy < 0 Then dy = dy - .25 Else dy = dy + .25
  329.             End If
  330.             value = wp(col, row)
  331.             If value = 5 Then 'first orange brick
  332.                 If obk = 0 Then 'flag first orange speed increase
  333.                     obk = 1
  334.                     speedups = speedups + 1
  335.                     If dy < 0 Then: dy = dy - .25 Else dy = dy + .25
  336.                 End If
  337.             End If
  338.             If value = 7 Then 'flag first red, speed increase paddle decrease! ! ! !
  339.                 If rbk = 0 Then
  340.                     rbk = 1
  341.                     speedups = speedups + 1
  342.                     If dy < 0 Then dy = dy - .25 Else: dy = dy + .25
  343.                     pw = pf * pw
  344.                 End If
  345.             End If
  346.             score = score + wp(col, row) 'update score with point value
  347.             wp(col, row) = 0 'no points here now
  348.  
  349.  
  350.             'black out box  need this?
  351.             'Line (col * bkw, row * bkh)-(col * bkw + bkw, row * bkh + bkh), black, BF
  352.         End If
  353.     End If
  354.  
  355. Sub updatescore
  356.     centerText 0, xmax / 2, 532, "Lives" + Str$(life)
  357.     centerText xmax / 2, xmax, 532, "Score" + Str$(score)
  358.  
  359. Sub silverball (x, y)
  360.     For i = 10 To 1 Step -1
  361.         cc = 255 - i * 20
  362.         CircleFill x, y, i, _RGB32(cc, cc, cc)
  363.     Next
  364.  
  365. Function rand% (lo%, hi%)
  366.     rand% = Int(Rnd * (hi% - lo% + 1)) + lo%
  367.  
  368. Sub CircleFill (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
  369.     ' CX = center x coordinate
  370.     ' CY = center y coordinate
  371.     '  R = radius
  372.     '  C = fill color
  373.     Dim Radius As Integer, RadiusError As Integer
  374.     Dim X As Integer, Y As Integer
  375.     Radius = Abs(R)
  376.     RadiusError = -Radius
  377.     X = Radius
  378.     Y = 0
  379.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  380.     Line (CX - X, CY)-(CX + X, CY), C, BF
  381.     While X > Y
  382.         RadiusError = RadiusError + Y * 2 + 1
  383.         If RadiusError >= 0 Then
  384.             If X <> Y + 1 Then
  385.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  386.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  387.             End If
  388.             X = X - 1
  389.             RadiusError = RadiusError - X * 2
  390.         End If
  391.         Y = Y + 1
  392.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  393.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  394.     Wend
  395.  
  396. Sub newSpinner (i As Integer, col, row) 'set Spinners dimensions start angles, color?
  397.     Dim r
  398.     s(i).x = col * bkw + .5 * bkw
  399.     s(i).y = row * bkh + .5 * bkh
  400.     s(i).sz = Rnd * .65 + .1
  401.     If Rnd < .5 Then r = -1 Else r = 1
  402.     s(i).dx = (s(i).sz * Rnd * 6) * r * 2
  403.     s(i).dy = (s(i).sz * Rnd * 6) * r * 2
  404.     r = Rnd * 255
  405.     s(i).c = _RGB32(r, Rnd * .5 * r, Rnd * .25 * r)
  406.  
  407. Sub drawSpinner (x As Integer, y As Integer, scale As Single, heading As Single, c As _Unsigned Long)
  408.     Dim x1, x2, x3, x4, y1, y2, y3, y4, r, a, a1, a2, lg, d, rd
  409.     Dim cRed, Blue, cGreen
  410.     Static switch As Integer
  411.     switch = switch + 2
  412.     switch = switch Mod 16 + 1
  413.     cRed = _Red32(c): cGreen = _Green32(c): Blue = _Blue32(c)
  414.     r = 10 * scale
  415.     x1 = x + r * Cos(heading): y1 = y + r * Sin(heading)
  416.     r = 2 * r 'lg lengths
  417.     For lg = 1 To 8
  418.         If lg < 5 Then
  419.             a = heading + .9 * lg * _Pi(1 / 5) + (lg = switch) * _Pi(1 / 10)
  420.         Else
  421.             a = heading - .9 * (lg - 4) * _Pi(1 / 5) - (lg = switch) * _Pi(1 / 10)
  422.         End If
  423.         x2 = x1 + r * Cos(a): y2 = y1 + r * Sin(a)
  424.         drawLink x1, y1, 3 * scale, x2, y2, 2 * scale, _RGB32(cRed + 20, cGreen + 10, Blue + 5)
  425.         If lg = 1 Or lg = 2 Or lg = 7 Or lg = 8 Then d = -1 Else d = 1
  426.         a1 = a + d * _Pi(1 / 12)
  427.         x3 = x2 + r * 1.5 * Cos(a1): y3 = y2 + r * 1.5 * Sin(a1)
  428.         drawLink x2, y2, 2 * scale, x3, y3, scale, _RGB32(cRed + 35, cGreen + 17, Blue + 8)
  429.         rd = Int(Rnd * 8) + 1
  430.         a2 = a1 + d * _Pi(1 / 8) * rd / 8
  431.         x4 = x3 + r * 1.5 * Cos(a2): y4 = y3 + r * 1.5 * Sin(a2)
  432.         drawLink x3, y3, scale, x4, y4, scale, _RGB32(cRed + 50, cGreen + 25, Blue + 12)
  433.     Next
  434.     r = r * .5
  435.     fcirc x1, y1, r, _RGB32(cRed - 20, cGreen - 10, Blue - 5)
  436.     x2 = x1 + (r + 1) * Cos(heading - _Pi(1 / 12)): y2 = y1 + (r + 1) * Sin(heading - _Pi(1 / 12))
  437.     fcirc x2, y2, r * .2, &HFFFFAA00
  438.     x2 = x1 + (r + 1) * Cos(heading + _Pi(1 / 12)): y2 = y1 + (r + 1) * Sin(heading + _Pi(1 / 12))
  439.     fcirc x2, y2, r * .2, &HFFFFAA00
  440.     r = r * 2
  441.     x1 = x + r * .9 * Cos(heading + _Pi): y1 = y + r * .9 * Sin(heading + _Pi)
  442.     TiltedEllipseFill 0, x1, y1, r, .7 * r, heading + _Pi, _RGB32(cRed, cGreen, Blue)
  443.  
  444. Sub drawLink (x1, y1, r1, x2, y2, r2, c As _Unsigned Long)
  445.     Dim a, a1, a2, x3, x4, x5, x6, y3, y4, y5, y6
  446.     a = _Atan2(y2 - y1, x2 - x1)
  447.     a1 = a + _Pi(1 / 2)
  448.     a2 = a - _Pi(1 / 2)
  449.     x3 = x1 + r1 * Cos(a1): y3 = y1 + r1 * Sin(a1)
  450.     x4 = x1 + r1 * Cos(a2): y4 = y1 + r1 * Sin(a2)
  451.     x5 = x2 + r2 * Cos(a1): y5 = y2 + r2 * Sin(a1)
  452.     x6 = x2 + r2 * Cos(a2): y6 = y2 + r2 * Sin(a2)
  453.     fquad x3, y3, x4, y4, x5, y5, x6, y6, c
  454.     fcirc x1, y1, r1, c
  455.     fcirc x2, y2, r2, c
  456.  
  457. 'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
  458. Sub fquad (x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, x3 As Integer, y3 As Integer, x4 As Integer, y4 As Integer, c As _Unsigned Long)
  459.     ftri x1, y1, x2, y2, x4, y4, c
  460.     ftri x3, y3, x4, y4, x1, y1, c
  461.  
  462. Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
  463.     Dim a&
  464.     a& = _NewImage(1, 1, 32)
  465.     _Dest a&
  466.     PSet (0, 0), K
  467.     _Dest 0
  468.     _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
  469.     _FreeImage a& '<<< this is important!
  470.  
  471. Sub fcirc (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
  472.     Dim Radius As Integer, RadiusError As Integer
  473.     Dim X As Integer, Y As Integer
  474.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  475.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  476.     Line (CX - X, CY)-(CX + X, CY), C, BF
  477.     While X > Y
  478.         RadiusError = RadiusError + Y * 2 + 1
  479.         If RadiusError >= 0 Then
  480.             If X <> Y + 1 Then
  481.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  482.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  483.             End If
  484.             X = X - 1
  485.             RadiusError = RadiusError - X * 2
  486.         End If
  487.         Y = Y + 1
  488.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  489.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  490.     Wend
  491.  
  492. Sub TiltedEllipseFill (destHandle&, x0, y0, a, b, ang, c As _Unsigned Long)
  493.     Dim max As Integer, mx2 As Integer, i As Integer, j As Integer, k As Single, lasti As Single, lastj As Single
  494.     Dim prc As _Unsigned Long, tef As Long
  495.     prc = _RGB32(255, 255, 255, 255)
  496.     If a > b Then max = a + 1 Else max = b + 1
  497.     mx2 = max + max
  498.     tef = _NewImage(mx2, mx2)
  499.     _Dest tef
  500.     _Source tef 'point wont read without this!
  501.     For k = 0 To 6.2832 + .05 Step .1
  502.         i = max + a * Cos(k) * Cos(ang) + b * Sin(k) * Sin(ang)
  503.         j = max + a * Cos(k) * Sin(ang) - b * Sin(k) * Cos(ang)
  504.         If k <> 0 Then
  505.             Line (lasti, lastj)-(i, j), prc
  506.         Else
  507.             PSet (i, j), prc
  508.         End If
  509.         lasti = i: lastj = j
  510.     Next
  511.     Dim xleft(mx2) As Integer, xright(mx2) As Integer, x As Integer, y As Integer
  512.     For y = 0 To mx2
  513.         x = 0
  514.         While Point(x, y) <> prc And x < mx2
  515.             x = x + 1
  516.         Wend
  517.         xleft(y) = x
  518.         While Point(x, y) = prc And x < mx2
  519.             x = x + 1
  520.         Wend
  521.         While Point(x, y) <> prc And x < mx2
  522.             x = x + 1
  523.         Wend
  524.         If x = mx2 Then xright(y) = xleft(y) Else xright(y) = x
  525.     Next
  526.     _Dest destHandle&
  527.     For y = 0 To mx2
  528.         If xleft(y) <> mx2 Then Line (xleft(y) + x0 - max, y + y0 - max)-(xright(y) + x0 - max, y + y0 - max), c, BF
  529.     Next
  530.     _FreeImage tef
  531.  
  532. Sub handleSpinners
  533.     For i = 1 To nS
  534.         If s(i).dead Then
  535.             If s(i).dead < 10 * s(i).sz Then
  536.                 explode s(i).x, s(i).y, 20 * s(i).sz, s(i).dead
  537.                 s(i).dead = s(i).dead + 1
  538.             End If
  539.         Else
  540.             s(i).x = s(i).x + s(i).dx
  541.             If s(i).x < 0 Or s(i).x > xmax Then s(i).dx = -s(i).dx
  542.             s(i).y = s(i).y + s(i).dy
  543.             If s(i).y < 0 Or s(i).y > xmax Then s(i).dy = -s(i).dy
  544.             If Sqr((bx - s(i).x) ^ 2 + (by - s(i).y) ^ 2) < 1.5 * br Then
  545.                 s(i).dead = 1
  546.                 explode s(i).x, s(i).y, 20 * s(i).sz, s(i).dead
  547.                 _SndPlay crunch
  548.             Else
  549.                 drawSpinner s(i).x, s(i).y, s(i).sz, _Atan2(s(i).dy, s(i).dx), s(i).c
  550.             End If
  551.         End If
  552.     Next
  553.  
  554. Sub explode (x, y, r, frm)
  555.     maxParticles = r * 40
  556.     For i = 1 To r
  557.         NewDot i, x, y, r
  558.     Next
  559.     rounds = r
  560.     For loopCount = 0 To frm
  561.         If _KeyDown(27) Then End
  562.         For i = 1 To rounds
  563.             dots(i).x = dots(i).x + dots(i).dx
  564.             dots(i).y = dots(i).y + dots(i).dy
  565.             dots(i).dx = dots(i).dx * air_resistance
  566.             dots(i).dy = air_resistance * dots(i).dy
  567.             fcirc dots(i).x, dots(i).y, dots(i).sz / 2, dots(i).c
  568.         Next
  569.         If rounds < maxParticles Then
  570.             For i = 1 To r
  571.                 NewDot (rounds + i), x, y, r
  572.             Next
  573.             rounds = rounds + r
  574.         End If
  575.     Next
  576.  
  577. Sub NewDot (i, x, y, r)
  578.     angle = _Pi(2 * Rnd)
  579.     rd = Rnd * 30
  580.     dots(i).x = x + rd * Cos(angle)
  581.     dots(i).y = y + rd * Sin(angle)
  582.     dots(i).sz = Rnd * r * .5
  583.     rd = Rnd 'STxAxTIC recommended for rounder spreads
  584.     dots(i).dx = rd * 7 * (7 - dots(i).sz) * Cos(angle)
  585.     dots(i).dy = rd * 7 * (7 - dots(i).sz) * Sin(angle)
  586.     dots(i).c = _RGB32(140 + rd * 80, 70 + rd * 40, 0)
  587.  
  588. Sub centerText (x1, x2, midy, s$) ' ' if you want to center fit a string between two goal posts x1, and x2
  589.     _PrintString ((x1 + x2) / 2 - _PrintWidth(s$) / 2, midy - _FontHeight(_Font) / 2), s$
  590.  
  591.  


zip contaings great sound effects and Spider font with license.


50
I had a calendar making program spaced just right with Windows Font something happened when I went to make calendars for this year using QB64 v2.0, it turns out the Font change occurred in QB64 v1.5 just never caught it because was not working with fonts for awhile.

Something happened that stretches the f o n t  a l o n g  t h e  x - a x i s ??
  [ You are not allowed to view this attachment ]  

The upper is the look from QB64 v 1.5 and v 2.0

The lower is the look from QB64 v 1.4 (I had changed form arial rounded bold to just arial to make sure it wasn't just the one font acting different.

I also noticed this "spread out look" trying different fonts in the Crypt-O-Gram variations and had to stick with the default because it was spreading out so wide across the screen.

51
Programs / One Key Screen 0 Halloween Crypt-O-Gram
« on: October 21, 2021, 01:44:45 pm »
Better get this posted before I forget I have it done. This is pure Screen 0, one key, Halloween Theme Crypt-O-Gram Puzzles, no graphics. The Crypto-O-grams are all one liner Halloween jokes. While you solve the puzzle, 2 witches play Infinite Pong with a pumpkin in ASCII Art. Nice music included in zip with other sound effects and ASCII txt files.

.bas Source
Code: QB64: [Select]
  1. _Title "One Key Screen 0 Halloween Crypt-O-Gram" ' b+  2021-10-21
  2.  
  3. Const Xmax = 120 ' AKA _Width to allow jokes upto 120 chars long, longest is 110 so far.
  4. Const Ymax = 30 ' AKA _Height some
  5.  
  6. Const Orange = 12
  7. Const White = 15
  8. Const Yellow = 14
  9. Const Back = 8
  10. Const Red = 4
  11. Const Blue = 9
  12. Const Green = 10
  13. Const BB = 6
  14.  
  15. ' for Cryptogram game
  16. Dim Shared Answer$ '  beginning phrase to be guessed    '   3 stages of the Puzzle
  17. Dim Shared Coded$ '   hidden in code
  18. Dim Shared Working$ ' decoded and solved when working$ becomes = ucase$(answer$)
  19. Dim Shared Letters$(1 To 26) ' for coding and highlited letters
  20. Dim Shared LCodes$(1 To 26) '  for code and decode by number 1 to 26
  21. Dim Shared Guesses$(1 To 26) ' track all the guess to decode
  22. Dim Shared HighLited ' cursor over letters to guess
  23. Dim Shared Mode ' what are we getting a coded letter =0, a guess for that letter =1, a letter to find and decode=3
  24. Dim Shared KeyTimer ' setup for Choice$ calls
  25. Dim Shared Place ' ditto tracks highlight location from selections
  26.  
  27. 'txt image
  28. Dim Shared WitchE(1 To 7) As String * 12
  29. Dim Shared WitchW(1 To 7) As String * 12
  30. LoadWitches
  31.  
  32. ' main declares
  33. Dim jokes$(1 To 100) ' load jokes one time from data statements in program
  34. Dim As Integer i, jCount, a, test, nSplash
  35. Dim r$, k$, c$
  36. Dim HH&, SW&, WH&, ZB& ' sound and font
  37. Dim As Integer pl, pr, pt, pb, px, py, pdx, pdy, weCol, wwCol ' the pumkin as a ball
  38.  
  39. ' load sounds
  40. HH& = _SndOpen("happy-halloween-scary-creepy-music-1382.mp3")
  41. SW& = _SndOpen("smich.wav")
  42. WH& = _SndOpen("Wolves Howling.wav")
  43. ZB& = _SndOpen("Zen Bell.wav")
  44.  
  45. Width Xmax, Ymax
  46. _FullScreen 'I guess it does make it easier to tell E from F...
  47. _PaletteColor 12, _RGB32(255, 128, 0) ' Orange
  48. _PaletteColor 13, _RGB32(180, 90, 45) ' for rColors
  49. For i = 1 To 100 'ready jokes
  50.     Read r$
  51.     If r$ <> "EOD" Then jokes$(i) = r$: jCount = jCount + 1 Else Exit For
  52.  
  53. 'set pumpkin as ball boundaries
  54. pl = 19: pr = _Width - 18: pt = 1: pb = 16
  55. Splash nSplash
  56.  
  57. restart:
  58. px = 19: py = 8: weCol = 5: wwCol = _Width - 15: pdx = 1: pdy = -1
  59.  
  60. 'setup Puzzle and code it
  61. Answer$ = jokes$(Int(Rnd * jCount) + 1)
  62. For i = 1 To 26: Guesses$(i) = "-": Next 'setup the display guesses array
  63. For i = 1 To 26 ' use letters for display of letters to pick second and to create a code
  64.     Letters$(i) = Chr$(i + 64)
  65.     LCodes$(i) = Letters$(i) ' these will convert between each other by index number
  66. For i = 26 To 2 Step -1 ' shuffle the letters in LCode$()
  67.     Swap LCodes$(i), LCodes$(Int(Rnd * i) + 1)
  68. Coded$ = "": Working$ = "" ' reset for next go around
  69. For i = 1 To Len(Answer$) 'third: put the phrase in coded$ and hide it in working$
  70.     a = Asc(UCase$(Answer$), i)
  71.     If a >= 65 And a <= 90 Then
  72.         Coded$ = Coded$ + LCodes$(a - 64)
  73.         Working$ = Working$ + "*"
  74.     Else
  75.         Coded$ = Coded$ + Mid$(Answer$, i, 1)
  76.         Working$ = Working$ + Mid$(Answer$, i, 1)
  77.     End If
  78.  
  79. HighLited = 1: Mode = 0: KeyTimer = Timer
  80. _SndLoop HH& '          setup done start game
  81. loopcnt = 0
  82.     Color Orange, Back: Cls
  83.     DisplayInstructions
  84.     Update
  85.     If Rnd < .0005 Then _SndPlay WH&
  86.     If Rnd < .0005 Then _SndPlay SW&
  87.     If Mode = 3 Then
  88.         k$ = Choice$(25, 33, " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ")
  89.     Else
  90.         k$ = Choice$(25, 29, " 1 2 3 4 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ")
  91.     End If
  92.     If k$ <> "" And k$ <> " " Then
  93.         If Mode = 0 Then ' highlight a letter
  94.             'm replaces arrows and mouse select of highlited 1 to 26 for letters
  95.             test = InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", k$)
  96.             If test > 0 Then
  97.                 HighLited = test
  98.                 Mode = 1
  99.             Else
  100.                 test = InStr("1234", k$)
  101.                 If test > 0 Then
  102.                     Select Case test
  103.                         Case 1: GoSub do1
  104.                         Case 2: GoSub do2
  105.                         Case 3: Mode = 3
  106.                         Case 4: GoSub do4
  107.                     End Select
  108.                 Else
  109.                     Mode = 0
  110.                 End If
  111.             End If
  112.         ElseIf Mode = 1 Then
  113.             Select Case Asc(k$)
  114.                 Case 49: GoSub do1
  115.                 Case 50: GoSub do2
  116.                 Case 51: Mode = 3
  117.                 Case 52: GoSub do4
  118.                 Case 65 TO 90
  119.                     Guesses$(HighLited) = k$ ' for screen updates
  120.                     For i = 1 To Len(Working$)
  121.                         If Letters$(HighLited) = Mid$(Coded$, i, 1) Then Mid$(Working$, i, 1) = k$
  122.                     Next
  123.                     Mode = 0
  124.             End Select
  125.         ElseIf Mode = 3 Then
  126.             Locate 25, 44: Print Space$(31); ' clear out old line
  127.             c$ = LCodes$(Asc(k$) - 64)
  128.             Guesses$(Asc(c$) - 64) = k$
  129.             For i = 1 To Len(Working$)
  130.                 If c$ = Mid$(Coded$, i, 1) Then Mid$(Working$, i, 1) = k$
  131.             Next
  132.             Mode = 0
  133.         End If
  134.     End If
  135.     loopcnt = loopcnt + 1
  136.     If loopcnt Mod 5 = 0 Then ' move pumpkin
  137.         If px + pdx >= pl And px + pdx <= pr Then
  138.             px = px + pdx
  139.         Else
  140.             pdx = -pdx
  141.             px = px + pdx
  142.         End If
  143.         If py + pdy >= pt And py + pdy <= pb Then
  144.             py = py + pdy
  145.         Else
  146.             pdy = -pdy
  147.             py = py + pdy
  148.         End If
  149.     End If
  150.     DrawWitch "e", py - 4, weCol
  151.     DrawWitch "w", py - 4, wwCol
  152.     DrawPumpkin py, px
  153.     _Limit 30
  154.     _Display
  155. Loop Until Working$ = UCase$(Answer$)
  156. Update
  157. Color Orange, Back
  158. CP 19, "You got it!    5 secs to next puzzle..."
  159. nSplash = nSplash + 1
  160. If nSplash > 10 Then nSplash = 0
  161. Splash nSplash
  162. GoTo restart
  163.  
  164. do1: ' display answer
  165. Working$ = UCase$(Answer$) ' show the answer$ guesses correct moves to next puzzle
  166. Mode = 0
  167.  
  168. do2: ' get decode letter for highlighted Letter
  169. For i = 1 To 26
  170.     If LCodes$(i) = Letters$(HighLited) Then c$ = Chr$(i + 64): Exit For
  171. Guesses$(HighLited) = c$ ' for screen updates
  172. For i = 1 To Len(Working$)
  173.     If Letters$(HighLited) = Mid$(Coded$, i, 1) Then Mid$(Working$, i, 1) = c$
  174. Mode = 0
  175.  
  176. do4: ' clear guess letter from code letter
  177. Guesses$(HighLited) = "-"
  178. For i = 1 To Len(Working$)
  179.     If Letters$(HighLited) = Mid$(Coded$, i, 1) Then Mid$(Working$, i, 1) = "*" ' clear the letter
  180. Mode = 0
  181.  
  182. 'one liners
  183. Data "Why do ghosts go on diets? So they can keep their ghoulish figures"
  184. Data "Where does a ghost go on vacation? Mali-boo."
  185. Data "Why did the ghost go into the bar? For the Boos."
  186. Data "What is in a ghost's nose? Boo-gers."
  187. Data "Why did the policeman ticket the ghost on Halloween? It didn't have a haunting license."
  188. Data "Why do demons and ghouls hang out together? Because demons are a ghoul's best friend!"
  189. Data "Why did the ghost starch his sheet? He wanted everyone scared stiff."
  190. Data "What does a panda ghost eat? Bam-BOO!"
  191. Data "What's a ghost's favorite dessert? I-Scream!"
  192. Data "Where do ghosts buy their food? At the ghost-ery store!"
  193. Data "How do you know when a ghost is sad? He starts boo hooing."
  194. Data "Why don't mummies take time off? They're afraid to unwind."
  195. Data "Why did the headless horseman go into business? He wanted to get ahead in life."
  196. Data "What kind of music do mummies like listening to on Halloween? Wrap music."
  197. Data "Why don't mummies have friends? Because they're too wrapped up in themselves."
  198. Data "Why did the vampire read the newspaper? He heard it had great circulation."
  199. Data "How do vampires get around on Halloween? On blood vessels."
  200. Data "What's it like to be kissed by a vampire? It's a pain in the neck."
  201. Data "What's it called when a vampire has trouble with his house? A grave problem."
  202. Data "How can you tell when a vampire has been in a bakery? All the jelly has been sucked out of the jelly doughnuts."
  203. Data "What do you get when you cross a vampire and a snowman? Frostbite."
  204. Data "Why do skeletons have low self-esteem? They have no body to love."
  205. Data "Know why skeletons are so calm? Because nothing gets under their skin."
  206. Data "What do you call a cleaning skeleton? The grim sweeper."
  207. Data "What do skeletons order at a restaurant? Spare ribs."
  208. Data "What do you call a witch's garage? A broom closet."
  209. Data "What kind of food would you find on a haunted beach? A sand-witch!"
  210. Data "What was the witch's favorite subject in school? Spelling."
  211. Data "What do you call two witches who live together? Broom-mates!"
  212. Data "What's a witch's favorite makeup? Ma-scare-a."
  213. Data "Who helps the little pumpkins cross the road safely? The crossing gourd."
  214. Data "What treat do eye doctors give out on Halloween? Candy corneas."
  215. Data "What type of plants do well on all Hallow's Eve? Bam-BOO!"
  216. Data "What do birds say on Halloween? Trick or tweet!"
  217. Data "Why don't skeletons ever go trick or treating? Because they have no-body to go with."
  218. Data "Where do ghosts buy their Halloween candy? At the ghost-ery store!"
  219. Data "What do owls say when they go trick or treating? 'Happy Owl-ween!'"
  220. Data "What do ghosts give out to trick or treaters? Booberries!"
  221. Data "Who did Frankenstein go trick or treating with? His ghoul friend."
  222. Data "What Halloween candy is never on time for the party? Choco-LATE!"
  223. Data "What do witches put on to go trick or treating? Mas-scare-a."
  224. Data "What does Bigfoot say when he asks for candy?  'Trick-or-feet!'"
  225. Data "Which type of pants do ghosts wear to trick or treat? Boo jeans."
  226. Data "What makes trick or treating with twin witches so challenging? You never know which witch is which!"
  227. Data "What happens when a vampire goes in the snow? Frost bite!"
  228. Data "What do you call two witches living together? Broommates"
  229. Data "What position does a ghost play in hockey? Ghoulie."
  230. Data "What do mummies listen to on Halloween? Wrap music."
  231. Data "How do you make a skeleton laugh? You tickle his funny bone!"
  232. Data "Which Halloween monster is good at math? Count Dracula!"
  233. Data "Why did the Cyclops give up teaching? He only had one pupil!"
  234. Data "Why didn't the skeleton go to see a scary movie? He didn't have the guts."
  235. Data "What did the boy ghost say to the girl ghost? 'You sure are boo-tiful!'"
  236. Data "Where does Dracula keep his money? In a blood bank."
  237. Data "Why are ghosts terrible liars? You can see right through them!"
  238. Data "Why don't mummies take vacations? They're afraid to unwind."
  239. Data "What is a vampire's favorite holiday, besides Halloween? Fangs-giving!"
  240. Data "Where do fashionable ghosts shop? Bootiques!"
  241. Data "What's a monster's favorite play? Romeo and Ghouliet!"
  242. Data "What room does a ghost not need? A living room."
  243. Data "What monster plays tricks on Halloween? Prank-enstein!"
  244. Data "What's a ghost's favorite dessert? I scream."
  245. Data "What does the skeleton chef say when he serves you a meal? 'Bone Appetit!'"
  246. Data "What is a vampire's favorite fruit? A neck-tarine!"
  247. Data "What do witches put on their bagels? Scream cheese."
  248. Data "What do ghosts eat for dinner? Spook-ghetti!"
  249. Data "What do skeletons order at restaurants? Spare ribs."
  250. Data "What does a panda ghost eat? Bam-BOO!"
  251. Data "What tops off a mummy's ice cream sundae? Whipped scream."
  252. Data "What's a ghost's favorite yogurt flavor? Boo-berry!"
  253. Data "What's a vampire's least favorite meal? A steak!"
  254. Data "Why was the candy corn booed off the stage? All of his jokes were too corny!"
  255. Data "What happened to the cannibal who showed up late to Halloween dinner? They gave him the cold shoulder."
  256. Data "What happens if you combine a vampire and a snowman? You get frostbite."
  257. Data "Do zombies eat popcorn with their fingers? No, they like to eat the fingers separately."
  258. Data "What happened to the man who got behind on payments to his exorcist? He got repossessed."
  259. Data "Where do most ghouls and goblins live in 2019? In North and South Scarolina."
  260. Data "Why did the team of witches lose the softball game? Their bats kept flying away."
  261. Data "What do you call six witches in a jacuzzi? A self cleaning coven."
  262. Data "Why was the vampire in a bad mood? Too much B negative."
  263. Data "What did the parent say to the baby ghost? Don't spook until your spoken too."
  264. Data "What is a vampire's favorite flavor of ice cream? Veinilla."
  265. Data "What are two freshly married spiders called? Newly-webbed."
  266. Data "Why hasn't anyone ever seen ghost poop? Because it's invisible."
  267. Data "You know it's bad luck to be followed by a black cat… if you are a mouse."
  268. Data "Where do most most werewolves live in 2019? Howlywood California."
  269. Data "Why don't witches have babies? Their husbands have crystal balls."
  270. Data "Why can't the ghost have any children? He has a Halloweenie."
  271. Data "EOD"
  272.  
  273. Sub Update ' preserve from ravages of graphics effects ;-))
  274.     Dim As Integer i, spaces
  275.     Dim w$, c$, a$, h$, pc$
  276.     Color Yellow, Back
  277.     Locate 17, (120 - Len(Answer$)) / 2: Print Coded$;
  278.     Color White, Back
  279.     Locate 18, (120 - Len(Answer$)) / 2
  280.     For i = 1 To Len(Answer$)
  281.         w$ = Mid$(Working$, i, 1): c$ = Mid$(Coded$, i, 1)
  282.         a$ = Mid$(Answer$, i, 1): h$ = Letters$(HighLited)
  283.         If w$ = "*" Then
  284.             pc$ = "*": If h$ = c$ Then Color White, Green Else Color White, Back
  285.         Else
  286.             Color White, Back
  287.             If w$ = UCase$(a$) Then pc$ = a$ Else pc$ = w$
  288.         End If
  289.         Print pc$;
  290.     Next
  291.     spaces = 9
  292.     For i = 1 To 26 'blue background highlighter
  293.         If i = HighLited Then
  294.             Color Yellow, Green
  295.         Else
  296.             Color Yellow, Back
  297.         End If
  298.         Locate 21, spaces: Print Letters$(i);
  299.         If i = HighLited Then
  300.             Color Yellow, Green
  301.         Else
  302.             Color White, Back
  303.         End If
  304.         Locate 22, spaces: Print Guesses$(i);
  305.         spaces = spaces + 4
  306.     Next
  307.     If Mode = 1 Then
  308.         Color White, Back
  309.         CP 24, "  Guess Solve Letter or Menu # "
  310.     ElseIf Mode = 0 Then
  311.         Color Yellow, Back
  312.         CP 24, "  Select Code Letter or Menu # "
  313.     ElseIf Mode = 3 Then
  314.         Color White, Back
  315.         CP 24, "     Select Letter to Find     "
  316.     End If
  317.  
  318. Sub DisplayInstructions
  319.     Dim c As Integer
  320.     Color Orange, Back
  321.     CP 4, "*** Halloween Challenge - Crypt-O-Gram Puzzle ***"
  322.     'Color Red
  323.     CPRC 7, "Solve puzzle by selecting a Code letter then selecting a Guess letter for it."
  324.     CPRC 8, "All selections are made by pressing the spacebar until you are on your choice."
  325.     CPRC 9, "Use the escape key to quit immediately."
  326.     'Color 2
  327.     c = 34
  328.     LPRC 11, c, "Select 1 to get the answer and move onto next puzzle."
  329.     LPRC 12, c, "Select 2 to decode current highlighted letter."
  330.     LPRC 13, c, "Select 3 to solve a letter, then select letter to find."
  331.     LPRC 14, c, "Select 4 to clear a guess at highlighted Code letter."
  332.  
  333.  
  334. Sub CP (row, s$) ' center text on text screen
  335.     Locate row, (_Width - Len(s$)) / 2: Print s$;
  336.  
  337. Sub CPRC (row, s$) ' center text on text screen and print in random colors
  338.     Dim As Integer i, col
  339.     col = (_Width - Len(s$)) / 2
  340.     For i = 1 To Len(s$)
  341.         Color rColor, Back
  342.         Locate row, col + i - 1: Print Mid$(s$, i, 1);
  343.     Next
  344.  
  345. Sub LP (row, col, s$) ' Locate and Print
  346.     Locate row, col: Print s$;
  347.  
  348. Sub LPRC (row, col, s$) ' Locate and print with Random colors
  349.     Dim i As Integer
  350.     For i = 1 To Len(s$)
  351.         Color rColor, Back
  352.         Locate row, col + i - 1: Print Mid$(s$, i, 1);
  353.     Next
  354.  
  355. Function Choice$ (row, col, selection$)
  356.     Dim As _Unsigned Long fg, bg
  357.     Dim As Integer saveRow, saveCol, i
  358.     Dim k&
  359.     fg~& = _DefaultColor: bg~& = _BackgroundColor
  360.     saveRow = CsrLin: saveCol = Pos(0)
  361.     If _KeyDown(27) Then System ' emergency exit
  362.     GoSub show:
  363.     k& = _KeyHit
  364.     If k& = 32 Then KeyTimer = Timer: Place = (Place + 1) Mod Len(selection$)
  365.     If Timer - KeyTimer >= 3 Then Choice$ = Mid$(selection$, Place + 1, 1): Place = 0
  366.     Locate saveRow, saveCol: Exit Function
  367.  
  368.     show:
  369.     Locate row, col
  370.     For i = 1 To Len(selection$)
  371.         If i = Place + 1 Then Color bg~&, fg~& Else Color fg~&, bg~&
  372.         Locate row, col - 1 + i: Print Mid$(selection$, i, 1);
  373.     Next
  374.     '_Display will take place in loop that called Choice$
  375.     Color fg~&, bg~&
  376.     Return
  377.  
  378. Function rColor%
  379.     Dim rc
  380.     rc = Rnd
  381.     If rc > .66 Then
  382.         rColor% = 4
  383.     ElseIf rc > .33 Then
  384.         rColor% = 13
  385.     ElseIf rc > .16 Then
  386.         rColor% = 2
  387.     ElseIf rc > .08 Then
  388.         rColor% = 6
  389.     Else
  390.         rColor% = 12
  391.     End If
  392.  
  393. Sub Splash (n As Integer)
  394.     Dim As Integer i, first, last, nlines, startRow
  395.     Dim s$
  396.     Color 12, Back: Cls
  397.  
  398.     If n = 0 Then
  399.         Open "ASCII by snd.txt" For Input As #1
  400.         For i = 1 To 20
  401.             Line Input #1, s$
  402.             LP i + 5, 41, RTrim$(s$)
  403.         Next
  404.         Close #1
  405.     ElseIf n > 0 Then
  406.         Open "10 Halloweens.txt" For Input As #1
  407.         If n = 1 Then
  408.             first = 11: last = 32: GoSub getText
  409.         ElseIf n = 2 Then
  410.             first = 32: last = 46: GoSub getText
  411.         ElseIf n = 3 Then
  412.             first = 46: last = 57: GoSub getText
  413.         ElseIf n = 4 Then
  414.             first = 57: last = 75: GoSub getText
  415.         ElseIf n = 5 Then
  416.             first = 75: last = 100: GoSub getText
  417.         ElseIf n = 6 Then
  418.             first = 100: last = 111: GoSub getText
  419.         ElseIf n = 7 Then
  420.             first = 111: last = 130: GoSub getText
  421.         ElseIf n = 8 Then
  422.             first = 131: last = 159: GoSub getText
  423.         ElseIf n = 9 Then
  424.             first = 159: last = 180: GoSub getText
  425.         ElseIf n = 10 Then
  426.             first = 180: last = 193: GoSub getText
  427.         End If
  428.     End If
  429.     CP 29, "... Spacebar Only! ..."
  430.     _Display
  431.     _KeyClear
  432.     While _KeyHit <> 32: Wend
  433.     Exit Sub
  434.  
  435.     getText:
  436.     nlines = last - first
  437.     startRow = Int((30 - nlines) / 2)
  438.     For i = 1 To 193
  439.         Line Input #1, s$
  440.         If i >= first And i <= last Then LP i - first + startRow + 1, 25, RTrim$(s$)
  441.         If i > last Then Exit For
  442.     Next
  443.     Close #1
  444.     Return
  445.  
  446. Sub LoadWitches
  447.     Dim w, i, s$
  448.     For w = 1 To 2
  449.         If w = 1 Then Open "witchE.txt" For Input As #1 Else Open "witchW.txt" For Input As #1
  450.         For i = 1 To 7
  451.             Line Input #1, s$
  452.             If w = 1 Then WitchE(i) = s$ Else WitchW(i) = s$
  453.         Next
  454.         Close #1
  455.     Next
  456.  
  457. Sub DrawWitch (WhichWitch$, row, col) ' 7 lines 12 cols  set color
  458.     Dim r, c
  459.     Color 5, Back
  460.     For r = 1 To 7
  461.         If row + r - 1 > 0 And row + r - 1 <= _Height Then
  462.             For c = 1 To 12
  463.                 If col + c - 1 > 0 And col + c - 1 <= _Width Then
  464.                     If UCase$(WhichWitch$) = "E" Then
  465.                         If Mid$(WitchE(r), c, 1) <> " " Then LP row + r - 1, col + c - 1, Mid$(WitchE(r), c, 1)
  466.                     Else
  467.                         Color 7
  468.                         If Mid$(WitchW(r), c, 1) <> " " Then LP row + r - 1, col + c - 1, Mid$(WitchW(r), c, 1)
  469.                     End If
  470.                 End If
  471.             Next
  472.         End If
  473.     Next
  474.  
  475. Sub DrawPumpkin (row, col) ' mid pumkin
  476.     Dim p$, r, c
  477.     Color 12, Back
  478.     p$ = "((|))"
  479.     For r = -1 To 0
  480.         If row + r > 0 And row + r <= _Height Then
  481.             For c = -2 To 2
  482.                 If col + c > 0 And col + c <= _Width Then
  483.                     If r = -1 And c = 0 Then
  484.                         LP row + r, col, ","
  485.                     ElseIf r = 0 Then
  486.                         LP row + r, col + c, Mid$(p$, c + 3, 1)
  487.                     End If
  488.                 End If
  489.             Next
  490.         End If
  491.     Next
  492.  

 
One Key Screen 0 Halloween Crypt-O-Gram.PNG

52
Programs / One Key Connect 4 (8x8) Halloween Style
« on: October 19, 2021, 08:09:18 pm »
I was curious how long this would take to convert and modify:

Code: QB64: [Select]
  1. Option _Explicit ' One Key Connect 4 (8x8) Halloween Style - bplus 2021-10-19
  2. Const SQ = 60 '       square or grid cell
  3. Const NumCols = 8 '   number of columns
  4. Const NumRows = 8 '   you guessed it
  5. Const NCM1 = NumCols - 1 ' NumCols minus 1
  6. Const NRM1 = NumRows - 1 ' you can guess surely
  7. Const SW = SQ * (NumCols + 2) '  screen width
  8. Const SH = SQ * (NumRows + 3) '  screen height
  9. Const P = 1 '       Player is 1 on grid
  10. Const AI = -1 '     AI is -1 on grid
  11. Const XO = SQ '     x offset for grid
  12. Const YO = 2 * SQ ' y offset for grid
  13.  
  14. ReDim Shared Grid(NCM1, NRM1) ' 0 = empty  P=1 for Player,  AI=-1  for AI so -4 is win for AI..
  15. ReDim Shared DX(7), DY(7) ' Directions
  16. DX(0) = 1: DY(0) = 0 ': DString$(0) = "East"
  17. DX(1) = 1: DY(1) = 1 ': DString$(1) = "South East"
  18. DX(2) = 0: DY(2) = 1 ': DString$(2) = "South"
  19. DX(3) = -1: DY(3) = 1 ': DString$(3) = "South West"
  20. DX(4) = -1: DY(4) = 0 ': DString$(4) = "West"
  21. DX(5) = -1: DY(5) = -1 ': DString$(5) = "North West"
  22. DX(6) = 0: DY(6) = -1 ': DString$(6) = "North"
  23. DX(7) = 1: DY(7) = -1 ' : DString$(7) = "North East"
  24. ReDim Shared Scores(NCM1) ' rating column for AI and displaying them
  25. ReDim Shared AIX, AIY ' last move of AI for highlighting in display
  26. ReDim Shared WinX, WinY, WinD ' display Winning Connect 4
  27. ReDim Shared GameOn, Turn, GoFirst, PlayerLastMoveCol, PlayerLastMoveRow, MoveNum ' game tracking
  28. ReDim Shared Record$(NCM1, NRM1)
  29. Dim Shared sx ' for pumpkin recursion shifty eyes
  30. Dim place, k$, t, r, s$, pr, d
  31.  
  32. Screen _NewImage(SW, SH, 32)
  33. _ScreenMove 360, 60
  34.  
  35. _Title "One Key Connect 4 (8x8) Halloween Style"
  36. d = 1
  37. While _KeyDown(32) = 0
  38.     Cls
  39.     pumpkin 0, _Width / 2, _Height / 2, _Height / 2.3, 3
  40.     sx = sx + d
  41.     If sx > 10 Then d = -d: sx = 10
  42.     If sx < -10 Then d = -d: sx = -10
  43.     Color &HFFFFFFFF, &HFF000000:
  44.     Locate 40, 33: Print "Spacebar Only"
  45.     _Display
  46.     _Limit 20
  47. GameOn = -1: GoFirst = AI: Turn = AI: MoveNum = 0
  48. ShowGrid
  49. place = -1
  50. t = Timer
  51. pr = (SQ - 6) / 2
  52. While GameOn
  53.     Cls
  54.     If Turn = P Then
  55.         k$ = InKey$
  56.         If k$ = Chr$(27) Then System ' emergency exit
  57.  
  58.         If k$ = " " Then
  59.             t = Timer: place = place + 1
  60.             If place >= NumCols Then place = -1
  61.         Else ' watch out for midnight!
  62.             If Timer - t > 4 And place <> -1 Then ' col selected
  63.                 r = GetOpenRow(place)
  64.                 If r <> NumRows Then
  65.                     Grid(place, r) = P: Turn = AI: PlayerLastMoveCol = place: PlayerLastMoveRow = r: MoveNum = MoveNum + 1
  66.                     place = -1 ' reset back to hold area
  67.                 End If
  68.             End If
  69.         End If
  70.     Else
  71.         AIMove
  72.         Turn = P: MoveNum = MoveNum + 1: t = Timer
  73.     End If
  74.     ShowGrid
  75.     If Turn = P Then
  76.         If place = -1 Then
  77.             s$ = "Holding area, press spacebar until over column to play."
  78.         Else
  79.             s$ = "Press Spacebar, if don't want to play" + Str$(place) + " column."
  80.         End If
  81.         Color &HFFFFFFFF, 0
  82.         _PrintString (XO, YO - SQ - 16), s$
  83.     End If
  84.     pumpkin 0, place * SQ + XO + SQ / 2, SQ + SQ / 2, pr, 2
  85.     sx = Rnd * 6 - 3
  86.     _Display
  87.     _Limit 15
  88.  
  89. Sub AIMove
  90.     ' What this sub does in English:
  91.     ' This sub assigns the value to playing each column, then plays the best value with following caveats:
  92.     ' + If it finds a winning move, it will play that immediately.
  93.     ' + If it finds a spoiler move, it will play that if no winning move was found.
  94.     ' + It will poisen the column's scoring, if opponent can play a winning move if AI plays this column,
  95.     '   but it might be the only legal move left.  We will have to play it if no better score was found.
  96.  
  97.     Dim c, r, d, cntA, cntP, bestScore, startR, startC, iStep, test, goodF, i
  98.     Dim openRow(NCM1) ' find open rows once
  99.     ReDim Scores(NCM1) ' evaluate each column's potential
  100.     AIX = -1: AIY = -1 ' set these when AI makes move, they are signal to display procedure AI's move.
  101.     For c = 0 To NCM1
  102.         openRow(c) = GetOpenRow(c)
  103.         r = openRow(c)
  104.         If r <> NumRows Then
  105.             For d = 0 To 3 ' 4 directions to build connect 4's that use cell c, r
  106.                 startC = c + -3 * DX(d): startR = r + -3 * DY(d)
  107.                 For i = 0 To 3 ' here we backup from the potential connect 4 in opposite build direction of c, r
  108.                     cntA = 0: cntP = 0: goodF = -1 ' reset counts and flag for good connect 4
  109.                     'from this start position run 4 steps forward to count all connects involving cell c, r
  110.                     For iStep = 0 To 3 ' process a potential connect 4
  111.                         test = GR(startC + i * DX(d) + iStep * DX(d), startR + i * DY(d) + iStep * DY(d))
  112.                         If test = NumRows Then goodF = 0: Exit For 'cant get connect4 from here
  113.                         If test = AI Then cntA = cntA + 1
  114.                         If test = P Then cntP = cntP + 1
  115.                     Next iStep
  116.                     If goodF Then 'evaluate the Legal Connect4 we could build with c, r
  117.                         If cntA = 3 Then ' we are done!  winner!
  118.                             AIX = c: AIY = r ' <<< this is the needed 4th cell to win tell ShowGrid last cell
  119.                             Grid(c, r) = AI '  <<< this is the needed 4th cell to win, add to grid this is AI move
  120.                             Scores(c) = 1000
  121.                             Exit Sub
  122.                         ElseIf cntP = 3 Then 'next best move spoiler!
  123.                             AIX = c: AIY = r 'set the move but don't exit there might be a winner
  124.                             Scores(c) = 900
  125.                         ElseIf cntA = 0 And cntP = 2 Then
  126.                             Scores(c) = Scores(c) + 8
  127.                         ElseIf cntA = 2 And cntP = 0 Then ' very good offense or defense
  128.                             Scores(c) = Scores(c) + 4 'play this to connect 3 or prevent player from Connect 3
  129.                         ElseIf cntA = 0 And cntP = 1 Then
  130.                             Scores(c) = Scores(c) + 4
  131.                         ElseIf (cntA = 1 And cntP = 0) Then 'good offense or defense
  132.                             Scores(c) = Scores(c) + 2 ' play this to connect 2 or prevent player from Connect 2
  133.                         ElseIf (cntA = 0 And cntP = 0) Then ' OK it's not a wasted move as it has potential for connect4
  134.                             Scores(c) = Scores(c) + 1 ' this is good move because this can still be a Connect 4
  135.                         End If
  136.                     End If ' in the board
  137.                 Next i
  138.             Next d
  139.             If Stupid(c, r) Then Scores(c) = -1000 + Scores(c) ' poison because if played the human can win
  140.         End If
  141.     Next
  142.     If AIX <> -1 Then ' we found a spoiler so move there since we haven't found a winner
  143.         Grid(AIX, AIY) = AI ' make move on grid and done!
  144.         Exit Sub
  145.     Else
  146.         If GetOpenRow(PlayerLastMoveCol) < NumRows Then 'all things being equal play on top of player's last move
  147.             bestScore = Scores(PlayerLastMoveCol): AIY = PlayerLastMoveRow - 1: AIX = PlayerLastMoveCol
  148.         Else
  149.             bestScore = -1000 ' a negative score indicates that the player can beat AI with their next move
  150.         End If
  151.         For c = 0 To NCM1
  152.             r = openRow(c)
  153.             If r <> NumRows Then
  154.                 If Scores(c) > bestScore Then bestScore = Scores(c): AIY = r: AIX = c
  155.             End If
  156.         Next
  157.         If AIX <> -1 Then
  158.             Grid(AIX, AIY) = AI ' make first best score move we found
  159.         Else 'We have trouble!  Oh but it could be there are no moves!!!
  160.             ' checkWin is run after every move by AI or Player if there were no legal moves left it should have caught that.
  161.             ' Just in case it didn't here is an error stop!
  162.             Beep: Locate 4, 2: Print "AI has failed to find a proper move, press any to end..."
  163.             Sleep ' <<< pause until user presses a key
  164.             End
  165.         End If
  166.     End If
  167.  
  168. Function GetOpenRow (forCol)
  169.     Dim i
  170.     GetOpenRow = NumRows 'assume none open
  171.     If forCol < 0 Or forCol > NCM1 Then Exit Function
  172.     For i = NRM1 To 0 Step -1
  173.         If Grid(forCol, i) = 0 Then GetOpenRow = i: Exit Function
  174.     Next
  175.  
  176. Function Stupid (c, r)
  177.     Dim pr
  178.     Grid(c, r) = AI
  179.     pr = GetOpenRow(c)
  180.     If pr <> NumRows Then
  181.         Grid(c, pr) = P
  182.         If CheckWin = 4 Then Stupid = -1
  183.         Grid(c, pr) = 0
  184.     End If
  185.     Grid(c, r) = 0
  186.  
  187. Function GR (c, r) ' if c, r are out of bounds returns N else returns grid(c, r)
  188.     ' need to check the grid(c, r) but only if c, r is on the board
  189.     If c < 0 Or c > NCM1 Or r < 0 Or r > NRM1 Then GR = NumRows Else GR = Grid(c, r)
  190.  
  191. Sub ShowGrid
  192.     Static lastMoveNum
  193.     Dim i, r, c, check, s$, k$
  194.     If MoveNum <> lastMoveNum Then ' file newest move
  195.         If MoveNum = 1 Then ReDim Record$(NCM1, NRM1)
  196.         If Turn = -1 Then
  197.             Record$(PlayerLastMoveCol, PlayerLastMoveRow) = _Trim$(Str$(MoveNum)) + " " + "P"
  198.         Else
  199.             Record$(AIX, AIY) = _Trim$(Str$(MoveNum)) + " " + "A"
  200.         End If
  201.         lastMoveNum = MoveNum
  202.     End If
  203.     'cls
  204.     Line (XO, YO)-Step(NumCols * SQ, NumRows * SQ), &HFF004400, BF
  205.     For i = 0 To NumCols 'grid
  206.         Line (SQ * i + XO, YO)-Step(0, NumRows * SQ), &HFFFFFFFF
  207.     Next
  208.     For i = 0 To NumRows
  209.         Line (XO, SQ * i + YO)-Step(NumCols * SQ, 0), &HFFFFFFFF
  210.     Next
  211.     For r = NRM1 To 0 Step -1 ''in grid rows are reversed 0 is top row
  212.         For c = 0 To NCM1
  213.             If Grid(c, r) = P Then
  214.                 Line (c * SQ + XO + 3, r * SQ + YO + 3)-Step(SQ - 6, SQ - 6), &HFF000000, BF
  215.                 pumpkin 0, c * SQ + XO + SQ / 2, r * SQ + YO + SQ / 2, (SQ - 6) / 2, 2
  216.  
  217.             ElseIf Grid(c, r) = AI Then
  218.                 If c = AIX And r = AIY Then 'highlite last AI move
  219.                     Line (c * SQ + XO + 3, r * SQ + YO + 3)-Step(SQ - 6, SQ - 6), &HFF8888FF, BF
  220.                 Else
  221.                     Line (c * SQ + XO + 3, r * SQ + YO + 3)-Step(SQ - 6, SQ - 6), &HFF4444FF, BF
  222.                 End If
  223.                 drawSpinner c * SQ + XO + SQ / 2, r * SQ + YO + SQ / 2, .5, _Pi(-c / 8), _RGB32(Rnd * 30 + 40, Rnd * 15 + 20, Rnd * 6 + 10)
  224.             End If
  225.             s$ = _Trim$(Str$(Scores(c)))
  226.             _PrintString (XO + c * SQ + (60 - Len(s$) * 8) / 2, YO + SQ * NumRows + 22), s$
  227.         Next
  228.     Next
  229.     '_Display
  230.     check = CheckWin
  231.     If check Then 'report end of round ad see if want to play again
  232.         If check = 4 Or check = -4 Then
  233.             For i = 0 To 3
  234.                 Line ((WinX + i * DX(WinD)) * SQ + XO + 5, (WinY + i * DY(WinD)) * SQ + YO + 5)-Step(SQ - 10, SQ - 10), &HFFFFFF00, B
  235.             Next
  236.         End If
  237.         For r = 0 To NRM1
  238.             For c = 0 To NCM1
  239.                 If Record$(c, r) <> "" Then
  240.                     s$ = Mid$(Record$(c, r), 1, InStr(Record$(c, r), " ") - 1)
  241.                     If Right$(Record$(c, r), 1) = "A" Then Color &HFFFFFFFF, &HFF000000 Else Color &HFFFFFFFF, &HFF000000
  242.                     _PrintString (SQ * c + XO + (SQ - Len(s$) * 8) / 2, SQ * r + YO + 22), s$
  243.                 End If
  244.             Next
  245.             Color , &HFF000000
  246.         Next
  247.         If check = -4 Then
  248.             s$ = " AI is Winner!"
  249.         ElseIf check = 4 Then
  250.             s$ = " Human is Winner!"
  251.         ElseIf check = NumRows Then
  252.             s$ = " Board is full, no winner." ' keep Turn the same
  253.         End If
  254.         Locate 2, ((SW - Len(s$) * 8) / 2) / 8: Print s$
  255.         s$ = " Play again? press spacebar, escape to quit... "
  256.         Locate 4, ((SW - Len(s$) * 8) / 2) / 8: Print s$
  257.         _Display
  258.         keywait:
  259.         While Len(k$) = 0
  260.             k$ = InKey$
  261.             _Limit 200
  262.         Wend
  263.         If k$ = " " Then
  264.             ReDim Grid(NCM1, NRM1), Scores(NCM1)
  265.             If GoFirst = P Then GoFirst = AI Else GoFirst = P
  266.             Turn = GoFirst: MoveNum = 0
  267.         ElseIf Asc(k$) = 27 Then
  268.             System
  269.         Else
  270.             k$ = "": GoTo keywait:
  271.         End If
  272.     End If
  273.  
  274. Function CheckWin ' return WinX, WinY, WinD along with +/- 4, returns NumRows if grid full, 0 if no win and grid not full
  275.     Dim gridFull, r, c, s, i
  276.     gridFull = NumRows
  277.     For r = NRM1 To 0 Step -1 'bottom to top
  278.         For c = 0 To NCM1
  279.             If Grid(c, r) Then ' check if c starts a row
  280.                 If c < NCM1 - 2 Then
  281.                     s = 0
  282.                     For i = 0 To 3 ' east
  283.                         s = s + Grid(c + i, r)
  284.                     Next
  285.                     If s = 4 Or s = -4 Then WinX = c: WinY = r: WinD = 0: CheckWin = s: Exit Function
  286.                 End If
  287.                 If r > 2 Then ' check if c starts a col
  288.                     s = 0
  289.                     For i = 0 To 3 ' north
  290.                         s = s + Grid(c, r - i)
  291.                     Next
  292.                     If s = 4 Or s = -4 Then WinX = c: WinY = r: WinD = 6: CheckWin = s: Exit Function
  293.                 End If
  294.                 If r > 2 And c < NCM1 - 2 Then 'check if c starts diagonal up to right
  295.                     s = 0
  296.                     For i = 0 To 3 ' north  east
  297.                         s = s + Grid(c + i, r - i)
  298.                     Next
  299.                     If s = 4 Or s = -4 Then WinX = c: WinY = r: WinD = 7: CheckWin = s: Exit Function
  300.                 End If
  301.                 If r > 2 And c > 2 Then 'check if c starts a diagonal up to left
  302.                     s = 0
  303.                     For i = 0 To 3 ' north west
  304.                         s = s + Grid(c - i, r - i)
  305.                     Next
  306.                     If s = 4 Or s = -4 Then WinX = c: WinY = r: WinD = 5: CheckWin = s: Exit Function
  307.                 End If
  308.             Else
  309.                 gridFull = 0 ' at least one enpty cell left
  310.             End If 'grid is something
  311.         Next
  312.     Next
  313.     CheckWin = gridFull
  314.  
  315.  
  316. Sub pumpkin (dh&, cx, cy, pr, limit)
  317.     Dim lastr, u, dx, i, tx1, tx2, tx3, ty1, ty2, ty3, ty22, sxs
  318.     'carve this!
  319.     Color &HFFFF0000
  320.     fEllipse cx, cy, pr, 29 / 35 * pr
  321.     Color &HFF000000
  322.     lastr = 2 / 7 * pr
  323.     Do
  324.         ellipse cx, cy, lastr, 29 / 35 * pr
  325.         lastr = .5 * (pr - lastr) + lastr + 1 / 35 * pr
  326.         If pr - lastr < 1 / 80 * pr Then Exit Do
  327.     Loop
  328.  
  329.     ' 'flickering candle light
  330.     'Color _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  331.  
  332.     ' eye sockets
  333.     ftri2 dh&, cx - 9 * pr / 12, cy - 2 * pr / 12, cx - 7 * pr / 12, cy - 6 * pr / 12, cx - 3 * pr / 12, cy - 0 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  334.     ftri2 dh&, cx - 7 * pr / 12, cy - 6 * pr / 12, cx - 3 * pr / 12, cy - 0 * pr / 12, cx - 2 * pr / 12, cy - 3 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  335.     ftri2 dh&, cx + 9 * pr / 12, cy - 2 * pr / 12, cx + 7 * pr / 12, cy - 6 * pr / 12, cx + 3 * pr / 12, cy - 0 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  336.     ftri2 dh&, cx + 7 * pr / 12, cy - 6 * pr / 12, cx + 3 * pr / 12, cy - 0 * pr / 12, cx + 2 * pr / 12, cy - 3 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  337.  
  338.     ' nose
  339.     ftri2 dh&, cx, cy - rand%(2, 5) * pr / 12, cx - 2 * pr / 12, cy + 2 * pr / 12, cx + rand%(1, 2) * pr / 12, cy + 2 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  340.  
  341.     ' evil grin
  342.     ftri2 dh&, cx - 9 * pr / 12, cy + 1 * pr / 12, cx - 7 * pr / 12, cy + 7 * pr / 12, cx - 6 * pr / 12, cy + 5 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  343.     ftri2 dh&, cx + 9 * pr / 12, cy + 1 * pr / 12, cx + 7 * pr / 12, cy + 7 * pr / 12, cx + 6 * pr / 12, cy + 5 * pr / 12, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  344.  
  345.     ' moving teeth/talk/grrrr..
  346.     u = rand%(4, 8)
  347.     dx = pr / u
  348.     For i = 1 To u
  349.         tx1 = cx - 6 * pr / 12 + (i - 1) * dx
  350.         tx2 = tx1 + .5 * dx
  351.         tx3 = tx1 + dx
  352.         ty1 = cy + 5 * pr / 12
  353.         ty3 = cy + 5 * pr / 12
  354.         ty2 = cy + (4 - Rnd) * pr / 12
  355.         ty22 = cy + (6 + Rnd) * pr / 12
  356.         ftri2 dh&, tx1, ty1, tx2, ty2, tx3, ty3, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  357.         ftri2 dh&, tx1 + .5 * dx, ty1, tx2 + .5 * dx, ty22, tx3 + .5 * dx, ty3, _RGB(Rnd * 55 + 200, Rnd * 55 + 200, 120)
  358.     Next
  359.     If limit Then
  360.         'shifty eyes
  361.         If limit = 3 Then sxs = sx Else sxs = .1 * limit * sx
  362.         pumpkin dh&, sxs + cx - 5 * pr / 12, cy - 2.5 * pr / 12, .15 * pr, Int(limit - 1)
  363.         pumpkin dh&, sxs + cx + 5 * pr / 12, cy - 2.5 * pr / 12, .15 * pr, Int(limit - 1)
  364.     End If
  365.  
  366. Sub fEllipse (CX As Long, CY As Long, xRadius As Long, yRadius As Long)
  367.     Dim scale As Single, x As Long, y As Long
  368.     scale = yRadius / xRadius
  369.     Line (CX, CY - yRadius)-(CX, CY + yRadius), , BF
  370.     For x = 1 To xRadius
  371.         y = scale * Sqr(xRadius * xRadius - x * x)
  372.         Line (CX + x, CY - y)-(CX + x, CY + y), , BF
  373.         Line (CX - x, CY - y)-(CX - x, CY + y), , BF
  374.     Next
  375.  
  376. Sub ellipse (CX As Long, CY As Long, xRadius As Long, yRadius As Long)
  377.     Dim scale As Single, xs As Long, x As Long, y As Long
  378.     Dim lastx As Long, lasty As Long
  379.     scale = yRadius / xRadius: xs = xRadius * xRadius
  380.     PSet (CX, CY - yRadius): PSet (CX, CY + yRadius)
  381.     lastx = 0: lasty = yRadius
  382.     For x = 1 To xRadius
  383.         y = scale * Sqr(xs - x * x)
  384.         Line (CX + lastx, CY - lasty)-(CX + x, CY - y)
  385.         Line (CX + lastx, CY + lasty)-(CX + x, CY + y)
  386.         Line (CX - lastx, CY - lasty)-(CX - x, CY - y)
  387.         Line (CX - lastx, CY + lasty)-(CX - x, CY + y)
  388.         lastx = x: lasty = y
  389.     Next
  390.  
  391. Sub ftri2 (returnDest&, x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
  392.     Dim a&
  393.     a& = _NewImage(1, 1, 32)
  394.     _Dest a&
  395.     PSet (0, 0), K
  396.     _Dest returnDest&
  397.     _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
  398.     _FreeImage a& '<<< this is important!
  399.  
  400. Function rand% (lo%, hi%)
  401.     rand% = Int(Rnd * (hi% - lo% + 1)) + lo%
  402.  
  403.  
  404.  
  405. Sub drawLink (x1, y1, r1, x2, y2, r2, c As _Unsigned Long)
  406.     Dim a, a1, a2, x3, x4, x5, x6, y3, y4, y5, y6
  407.     a = _Atan2(y2 - y1, x2 - x1)
  408.     a1 = a + _Pi(1 / 2)
  409.     a2 = a - _Pi(1 / 2)
  410.     x3 = x1 + r1 * Cos(a1): y3 = y1 + r1 * Sin(a1)
  411.     x4 = x1 + r1 * Cos(a2): y4 = y1 + r1 * Sin(a2)
  412.     x5 = x2 + r2 * Cos(a1): y5 = y2 + r2 * Sin(a1)
  413.     x6 = x2 + r2 * Cos(a2): y6 = y2 + r2 * Sin(a2)
  414.     fquad x3, y3, x4, y4, x5, y5, x6, y6, c
  415.     Fcirc x1, y1, r1, c
  416.     Fcirc x2, y2, r2, c
  417.  
  418. 'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
  419. Sub fquad (x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, x3 As Integer, y3 As Integer, x4 As Integer, y4 As Integer, c As _Unsigned Long)
  420.     ftri x1, y1, x2, y2, x4, y4, c
  421.     ftri x3, y3, x4, y4, x1, y1, c
  422.  
  423. Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
  424.     Dim a&
  425.     a& = _NewImage(1, 1, 32)
  426.     _Dest a&
  427.     PSet (0, 0), K
  428.     _Dest 0
  429.     _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
  430.     _FreeImage a& '<<< this is important!
  431.  
  432. Sub TiltedEllipseFill (destHandle&, x0, y0, a, b, ang, c As _Unsigned Long)
  433.     Dim max As Integer, mx2 As Integer, i As Integer, j As Integer, k As Single, lasti As Single, lastj As Single
  434.     Dim prc As _Unsigned Long, tef As Long
  435.     prc = _RGB32(255, 255, 255, 255)
  436.     If a > b Then max = a + 1 Else max = b + 1
  437.     mx2 = max + max
  438.     tef = _NewImage(mx2, mx2)
  439.     _Dest tef
  440.     _Source tef 'point wont read without this!
  441.     For k = 0 To 6.2832 + .05 Step .1
  442.         i = max + a * Cos(k) * Cos(ang) + b * Sin(k) * Sin(ang)
  443.         j = max + a * Cos(k) * Sin(ang) - b * Sin(k) * Cos(ang)
  444.         If k <> 0 Then
  445.             Line (lasti, lastj)-(i, j), prc
  446.         Else
  447.             PSet (i, j), prc
  448.         End If
  449.         lasti = i: lastj = j
  450.     Next
  451.     Dim xleft(mx2) As Integer, xright(mx2) As Integer, x As Integer, y As Integer
  452.     For y = 0 To mx2
  453.         x = 0
  454.         While Point(x, y) <> prc And x < mx2
  455.             x = x + 1
  456.         Wend
  457.         xleft(y) = x
  458.         While Point(x, y) = prc And x < mx2
  459.             x = x + 1
  460.         Wend
  461.         While Point(x, y) <> prc And x < mx2
  462.             x = x + 1
  463.         Wend
  464.         If x = mx2 Then xright(y) = xleft(y) Else xright(y) = x
  465.     Next
  466.     _Dest destHandle&
  467.     For y = 0 To mx2
  468.         If xleft(y) <> mx2 Then Line (xleft(y) + x0 - max, y + y0 - max)-(xright(y) + x0 - max, y + y0 - max), c, BF
  469.     Next
  470.     _FreeImage tef
  471.  
  472. Sub Fcirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
  473.     Dim Radius As Long, RadiusError As Long
  474.     Dim X As Long, Y As Long
  475.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  476.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  477.     Line (CX - X, CY)-(CX + X, CY), C, BF
  478.     While X > Y
  479.         RadiusError = RadiusError + Y * 2 + 1
  480.         If RadiusError >= 0 Then
  481.             If X <> Y + 1 Then
  482.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  483.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  484.             End If
  485.             X = X - 1
  486.             RadiusError = RadiusError - X * 2
  487.         End If
  488.         Y = Y + 1
  489.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  490.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  491.     Wend
  492.  
  493. Sub drawSpinner (x As Integer, y As Integer, scale As Single, heading As Single, c As _Unsigned Long)
  494.     Dim x1, x2, x3, x4, y1, y2, y3, y4, r, a, a1, a2, lg, d, rd
  495.     Dim rred, bblue, ggreen
  496.     Static switch As Integer
  497.     switch = switch + 2
  498.     switch = switch Mod 16 + 1
  499.     rred = _Red32(c): ggreen = _Green32(c): bblue = _Blue32(c)
  500.     r = 10 * scale
  501.     x1 = x + r * Cos(heading): y1 = y + r * Sin(heading)
  502.     r = 2 * r 'lg lengths
  503.     For lg = 1 To 8
  504.         If lg < 5 Then
  505.             a = heading + .9 * lg * _Pi(1 / 5) + (lg = switch) * _Pi(1 / 10)
  506.         Else
  507.             a = heading - .9 * (lg - 4) * _Pi(1 / 5) - (lg = switch) * _Pi(1 / 10)
  508.         End If
  509.         x2 = x1 + r * Cos(a): y2 = y1 + r * Sin(a)
  510.         drawLink x1, y1, 3 * scale, x2, y2, 2 * scale, _RGB32(rred + 20, ggreen + 10, bblue + 5)
  511.         If lg = 1 Or lg = 2 Or lg = 7 Or lg = 8 Then d = -1 Else d = 1
  512.         a1 = a + d * _Pi(1 / 12)
  513.         x3 = x2 + r * 1.5 * Cos(a1): y3 = y2 + r * 1.5 * Sin(a1)
  514.         drawLink x2, y2, 2 * scale, x3, y3, scale, _RGB32(rred + 35, ggreen + 17, bblue + 8)
  515.         rd = Int(Rnd * 8) + 1
  516.         a2 = a1 + d * _Pi(1 / 8) * rd / 8
  517.         x4 = x3 + r * 1.5 * Cos(a2): y4 = y3 + r * 1.5 * Sin(a2)
  518.         drawLink x3, y3, scale, x4, y4, scale, _RGB32(rred + 50, ggreen + 25, bblue + 12)
  519.     Next
  520.     r = r * .5
  521.     Fcirc x1, y1, r, _RGB32(rred - 20, ggreen - 10, bblue - 5)
  522.     x2 = x1 + (r + 1) * Cos(heading - _Pi(1 / 12)): y2 = y1 + (r + 1) * Sin(heading - _Pi(1 / 12))
  523.     Fcirc x2, y2, r * .2, &HFF000000
  524.     x2 = x1 + (r + 1) * Cos(heading + _Pi(1 / 12)): y2 = y1 + (r + 1) * Sin(heading + _Pi(1 / 12))
  525.     Fcirc x2, y2, r * .2, &HFF000000
  526.     r = r * 2
  527.     x1 = x + r * .9 * Cos(heading + _Pi): y1 = y + r * .9 * Sin(heading + _Pi)
  528.     TiltedEllipseFill 0, x1, y1, r, .7 * r, heading + _Pi, _RGB32(rred, ggreen, bblue)
  529.  
  530.  

Attached is Source and .exe for Windows (no assets program)
 
One Key Connect 4 (8x8) Halloween Style.PNG


53
QB64 Discussion / On Clearing a STATIC array
« on: October 07, 2021, 04:09:14 pm »
Should Erase work for clearing values in a STATIC array? I tried it here
https://www.qb64.org/forum/index.php?topic=4266.msg136545#msg136545

Got errors, so I ended up doing it the hard way ie, resetting each and every value back to 0 in 2 index For loops for 2D arrays. I am wondering if I just screwed up with Erase.

54
Programs / DrawWorms code test and demo
« on: October 07, 2021, 11:59:19 am »
This is graphics effect test and demo, I am hoping to add to Crypt-O-Gram Puzzle.

All White and Yellow colors are poison to worms, still they will nibble because where ever they go they lay down a black track on the screen.

If the program hangs just press esc, there is a problem sometimes that hangs often at start new WormYard init.

Should be fun to watch, the first test makes sure worms stay in WormYard and that White really discourages worms from proceeding further in their current direction that ran into White. ha! sometimes a white box is laid over worm and then it's trapped (but could hang program? not being able to find direction to go).

Code: QB64: [Select]
  1. _Title "DrawWorms Test and Demo, worms should avoid Yellow and White" 'b+ 2021-10-06
  2. ' This is intended for Crypt-O-Gram Puzzle but may use else where also.
  3. ' This needs to be done in background on the side and updated with main loop in program using it.
  4.  
  5. ' Use general Object
  6. Type object
  7.     x As Single
  8.     y As Single
  9.     w As Single
  10.     h As Single
  11.     dx As Single
  12.     dy As Single
  13.     dir As Single
  14.     sz As Single
  15.     c As _Unsigned Long
  16.  
  17. Const nWorms = 30
  18. Const xmax = 800, ymax = 600
  19. Dim Shared Worms(1 To nWorms) As object
  20. Dim Shared WormYard As object
  21. Screen _NewImage(xmax, ymax, 32)
  22. _Delay .25
  23. Color &HFFDDDDDD, &HFF442211
  24. Cls 'set backcolor
  25. NewWormYard _Width / 4, _Height / 4, _Width / 2, _Height / 2 ' for this demo the middle of the screen
  26. init = -1
  27.     'sample main loop action
  28.     lc = lc + 1
  29.     If lc Mod 200 = 199 Then init = -1: Cls
  30.     Locate 1, 1: Print lc
  31.     If Rnd < .5 Then c~& = _RGB32(255, 255, 255) Else c~& = _RGB32(255, 0, 0)
  32.     Line (Rnd * _Width, Rnd * _Height)-Step(Rnd * 50, Rnd * 50), c~&, BF
  33.     DrawWorms init
  34.     _Limit 10
  35.  
  36. _Delay .25
  37. '_ScreenMove _Middle
  38. _PutImage , sc&, 0
  39. ' end perfect
  40. _Delay .25 ' <<<< possible racing problem with change of screen size and _width adn Height update
  41. NewWormYard 0, 0, _Width, _Height ' <<< update WornYard to new screen size
  42. _PutImage , sc&, 0
  43. init = -1 'only way to see sc& ??????????????/
  44.     DrawWorms init
  45.     _Limit 10
  46.  
  47. Sub DrawWorms (DrawReset) ' one frame in main loop
  48.     Static x(1 To nWorms, 1 To 20), y(1 To nWorms, 1 To 20)
  49.     If DrawReset Then
  50.         For i = 1 To nWorms
  51.             NewWorm i
  52.             For j = 1 To 20
  53.                 x(i, j) = 0: y(i, j) = 0
  54.             Next
  55.         Next
  56.         DrawReset = 0
  57.     End If
  58.     For i = 1 To nWorms
  59.         If _KeyDown(27) Then Exit Sub
  60.         For j = 1 To Worms(i).sz ' blackout old segments
  61.             If x(i, j) And y(i, j) Then fcirc x(i, j), y(i, j), 8, &HFF000000
  62.         Next
  63.         tryAgain:
  64.         If _KeyDown(27) Then Exit Sub
  65.         If Rnd < .3 Then Worms(i).dx = Worms(i).dx + .8 * Rnd - .4 Else Worms(i).dy = Worms(i).dy + .8 * Rnd - .4
  66.         If Abs(Worms(i).dx) > 2 Then Worms(i).dx = Worms(i).dx * .5
  67.         If Abs(Worms(i).dy) > 2 Then Worms(i).dy = Worms(i).dy * .5
  68.         x = Worms(i).x + Worms(i).dx * 2.0: y = Worms(i).y + Worms(i).dy * 2.0
  69.         good = -1
  70.         If x >= WormYard.x + 6 And x <= WormYard.x + WormYard.w - 6 Then
  71.             If y >= WormYard.y + 6 And y <= WormYard.y + WormYard.h - 6 Then
  72.                 For yy = y - 6 To y + 6
  73.                     For xx = x - 6 To x + 6
  74.                         If Point(xx, yy) = _RGB32(255, 255, 255) Or Point(xx, yy) = _RGB32(255, 255, 0) Then good = 0: Exit For
  75.                     Next
  76.                     If good = 0 Then Exit For
  77.                 Next
  78.             Else
  79.                 good = 0
  80.             End If
  81.         Else
  82.             good = 0
  83.         End If
  84.         If good = 0 Then 'turn the worm
  85.             'Beep: Locate 1, 1: Print x, y
  86.             'Input "enter >", w$
  87.             If Rnd > .5 Then 'change dx
  88.                 If Worms(i).dx Then
  89.                     Worms(i).dx = -Worms(i).dx
  90.                 Else
  91.                     If Rnd > .5 Then Worms(i).dx = 1 Else Worms(i).dx = -1
  92.                 End If
  93.             Else
  94.                 If Worms(i).dy Then
  95.                     Worms(i).dy = -Worms(i).dy
  96.                 Else
  97.                     If Rnd > .5 Then Worms(i).dy = 1 Else Worms(i).dy = -1
  98.                 End If
  99.             End If
  100.             GoTo tryAgain
  101.         End If
  102.         For j = Worms(i).sz To 2 Step -1
  103.             x(i, j) = x(i, j - 1): y(i, j) = y(i, j - 1)
  104.             If x(i, j) And y(i, j) Then drawBall x(i, j), y(i, j), 6, Worms(i).c
  105.         Next
  106.         x(i, 1) = x: y(i, 1) = y
  107.         drawBall x(i, 1), y(i, 1), 6, Worms(i).c
  108.         Worms(i).x = x: Worms(i).y = y
  109.     Next i 'worm index
  110.  
  111. Sub NewWormYard (x, y, w, h)
  112.     WormYard.x = x: WormYard.y = y: WormYard.w = w: WormYard.h = h
  113.     For i = 1 To nWorms
  114.         NewWorm i
  115.     Next
  116.  
  117. Sub NewWorm (i)
  118.     'pick which side to enter, for dx, dy generally headed towards inner screen
  119.     side = Int(Rnd * 4)
  120.     Select Case side
  121.         Case 0 ' left side
  122.             Worms(i).x = WormYard.x + 6
  123.             Worms(i).y = WormYard.y + 6 + (WormYard.h - 12) * Rnd
  124.             Worms(i).dx = 1
  125.             Worms(i).dy = 0
  126.         Case 1 'right side
  127.             Worms(i).x = WormYard.x + WormYard.w - 6
  128.             Worms(i).y = WormYard.y + 6 + (WormYard.h - 12) * Rnd
  129.             Worms(i).dx = -1
  130.             Worms(i).dy = 0
  131.         Case 2 ' top
  132.             Worms(i).y = WormYard.y + 6
  133.             Worms(i).x = WormYard.x + 6 + (WormYard.w - 12) * Rnd
  134.             Worms(i).dx = 0
  135.             Worms(i).dy = 1
  136.         Case 3 'bottom
  137.             Worms(i).y = WormYard.y + WormYard.h - 6
  138.             Worms(i).x = WormYard.x + 6 + (WormYard.w - 12) * Rnd
  139.             Worms(i).dx = 0
  140.             Worms(i).dy = -1
  141.     End Select
  142.     Worms(i).sz = Int(Rnd * 11) + 10
  143.     side = Int(Rnd * 4): lev = Int(Rnd * 10)
  144.     If side = 0 Then
  145.         Worms(i).c = _RGB32(255 - 20 * lev + 50, 180 - 15 * lev, 180 - 15 * lev)
  146.     ElseIf side = 1 Then
  147.         Worms(i).c = _RGB32(255 - 20 * lev, 180 - 15 * lev + 50, 180 - 15 * lev)
  148.     ElseIf side = 2 Then
  149.         Worms(i).c = _RGB32(255 - 20 * lev, 180 - 15 * lev, 180 - 15 * lev + 20)
  150.     ElseIf side = 3 Then
  151.         Worms(i).c = _RGB32(255 - 20 * lev, 180 - 15 * lev, 180 - 15 * lev)
  152.     End If
  153.  
  154. Sub fcirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
  155.     Dim Radius As Long, RadiusError As Long
  156.     Dim X As Long, Y As Long
  157.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  158.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  159.     Line (CX - X, CY)-(CX + X, CY), C, BF
  160.     While X > Y
  161.         RadiusError = RadiusError + Y * 2 + 1
  162.         If RadiusError >= 0 Then
  163.             If X <> Y + 1 Then
  164.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  165.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  166.             End If
  167.             X = X - 1
  168.             RadiusError = RadiusError - X * 2
  169.         End If
  170.         Y = Y + 1
  171.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  172.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  173.     Wend
  174.  
  175. Sub drawBall (x, y, r, c As _Unsigned Long)
  176.     Dim rred As Long, grn As Long, blu As Long, rr As Long, f
  177.     rred = _Red32(c): grn = _Green32(c): blu = _Blue32(c)
  178.     For rr = r To 0 Step -1
  179.         f = 1.25 - rr / r
  180.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  181.     Next
  182.  
  183.  

Oh! I should lay down a black circle for first x,y of newly init worm!


55
Programs / Fade In & Out Tests
« on: October 05, 2021, 01:51:13 am »
I have 3 ideas I'm testing for an effect of having a background image over which I want to Fade In and then Fade Out a Loaded Image.

Here is what @Cobalt  suggested, I probably did something wrong in applying the procedure but don't know what:
Code: QB64: [Select]
  1. _Title "Fade In-Out Tests Cobalts suggestion" 'b+ 2021-10-04
  2. ' Steve suggest: _SetAlpha alphaLevel255& [, color1&][ TO color2&] [, iHdl&]
  3. ' bplus idea random fill overlapping at low to middle transparencey using Point probably very lumpy image
  4. Screen _NewImage(120 * 8, 30 * 16, 32) ' current screen size for Halloween Crypt-O-Gram
  5. fio& = _LoadImage("haunted heads.jpg", 32)
  6. '_PutImage , fio&, 0 ' OK image works
  7.  
  8. ' make sample image
  9. snap& = _NewImage(_Width, _Height, 32)
  10. For i = 1 To _Width * _Height * .00035
  11.     Line (Rnd * _Width, Rnd * _Height)-Step(Rnd * 50, Rnd * 50), _RGB32(Rnd * 255, Rnd * 255, Rnd * 255, Rnd * 255), BF
  12. _PutImage , 0, snap&
  13.  
  14. '  Test Cobalt's suggestion
  15. Dim Shared Layer(0 To 100) As Long ' <<<< Cobalt didn't mention this
  16. 'Layer(0) = snap&  ' <<<<  un comment and just get background, comment and get back ground then only fade in of heads
  17. Fade_In fio& ' where's my background image???
  18.  
  19.  
  20. ' Fade_In, Fade_Out, DarkenImage From Cobalt 2021-10-04  https://www.qb64.org/forum/index.php?topic=4254.msg136319#msg136319
  21. Sub Fade_Out (L&)
  22.     For n! = 1 To 0.00 Step -0.05
  23.         i2& = _CopyImage(L&)
  24.         DarkenImage i2&, n!
  25.         _PutImage (0, 0), i2&, Layer(0)
  26.         _FreeImage i2&
  27.         _Delay .06
  28.     Next
  29.  
  30. Sub Fade_In (L&)
  31.     For n! = 0.01 To 1 Step 0.05
  32.         i2& = _CopyImage(L&)
  33.         DarkenImage i2&, n!
  34.         _PutImage (0, 0), i2&, Layer(0)
  35.         _FreeImage i2&
  36.         _Delay .06
  37.     Next
  38.  
  39. Sub DarkenImage (Image As Long, Value_From_0_To_1 As Single)
  40.     If Value_From_0_To_1 <= 0 Or Value_From_0_To_1 >= 1 Or _PixelSize(Image) <> 4 Then Exit Sub
  41.     Dim Buffer As _MEM: Buffer = _MemImage(Image) 'Get a memory reference to our image
  42.     Dim Frac_Value As Long: Frac_Value = Value_From_0_To_1 * 65536 'Used to avoid slow floating point calculations
  43.     Dim O As _Offset, O_Last As _Offset
  44.     O = Buffer.OFFSET 'We start at this offset
  45.     O_Last = Buffer.OFFSET + _Width(Image) * _Height(Image) * 4 'We stop when we get to this offset
  46.     'use on error free code ONLY!
  47.     Do
  48.         _MemPut Buffer, O, _MemGet(Buffer, O, _Unsigned _Byte) * Frac_Value \ 65536 As _UNSIGNED _BYTE
  49.         _MemPut Buffer, O + 1, _MemGet(Buffer, O + 1, _Unsigned _Byte) * Frac_Value \ 65536 As _UNSIGNED _BYTE
  50.         _MemPut Buffer, O + 2, _MemGet(Buffer, O + 2, _Unsigned _Byte) * Frac_Value \ 65536 As _UNSIGNED _BYTE
  51.         O = O + 4
  52.     Loop Until O = O_Last
  53.     'turn checking back on when done!
  54.     _MemFree Buffer
  55.  
  56.  
  57.  
  58.  

I either just get all background Or all background until Fade_IN applied then ONLY image fade in very fast.

56
QB64 Discussion / Looking for Way to Fade In (or Out) an Image
« on: October 04, 2021, 10:16:51 am »
This question came up last night with Dav's Pipe Connecting Puzzle:

Is there a way to fade an image in or out without having to resort to a pixel at a time with Point?

That would be so nice to use for ghostly faces and other Halloween tricks.

57
Programs / Crypt-O-Gram Puzzle - Halloween Challenge
« on: October 04, 2021, 09:18:14 am »
The Cryptogram Puzzle I presented in Discussion Board was really sadistic in the way you had to input letters. I came up with a better algorithm yesterday morning, I call it "Binary Select".

Here is the test code you can play with to get familiar with inputting letters. It's like the computer is playing a little guessing game showing you a group of letters and you press spacebar (or any key in test demo) if your letter is in the group... just wait if it's not your letter in group, repeat... until computer knows your letter. The computer then displays letter on next line and on next line you must confirm YN (another little guessing game pressing spacebar (or any key) on the Y display to confirm, indeed, that is the intended letter or not ie, you did not confirm the Y display.

So here is the main engine for getting user input for the game:
Code: QB64: [Select]
  1. _Title "Binary Select test demo" 'b+ 2021-10-03  Aha!
  2.  
  3.     test$ = bChoice$(10, 5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234")
  4.     Cls
  5.     Print "bChoice$ returned > "; test$
  6.     Do
  7.         Print: Print "Do you want to try another?"
  8.         test$ = ""
  9.         test$ = bChoice$(CsrLin, 1, "YNM")
  10.     Loop Until Len(test$)
  11.     Cls
  12. Loop Until test$ <> "Y"
  13.  
  14. Function bChoice$ (row, col, select$) ' this is a wrapper function for confirming the BinarySelect$ choice since so easy to mess up
  15.     'this function needs 3 lines from screen, starting at row parameter.
  16.     'the first line, at row, is dedicated to the BinarySelect$ function
  17.     'the next 2 lines confirm the select
  18.     ' All this makes it possible to continuous poll for a selection, if the user is away from computer for awhile
  19.     ' no problem because he wont be there to confirm the choice and so no progress is made in program using BinarySelect$
  20.  
  21.     'clear 3 dedicated lines
  22.     copySelect$ = select$
  23.     For i = 0 To 2 ' clear lines
  24.         Locate row + i, col: Print Space$(_Width - col + 1);
  25.     Next
  26.     c$ = BinarySelect$(row, col, copySelect$) ' the last choice is like a cancel
  27.     'Locate 20, 10: Print "debug print c$: "; c$;
  28.     Locate row + 1, col: Print "Confirming your choice > " + c$;
  29.     check$ = BinarySelect$(row + 2, col, "YN")
  30.     If check$ = "Y" Then bChoice$ = c$
  31.     Print "bChoice$ = "; c$
  32.  
  33.  
  34. Function BinarySelect$ (row, col, select$) ' this is recursive part of Binary Select Algo
  35.     ls = Len(select$)
  36.     If ls = 0 Then Beep: Exit Function ' no choices
  37.     If ls = 1 Then BinarySelect$ = select$: Exit Function ' only one choice
  38.     hls = Int(ls / 2)
  39.     s1$ = Mid$(select$, 1, hls): s2$ = Mid$(select$, hls + 1)
  40.     Locate row, col: Print Space$(_Width - col + 1);
  41.     Locate row, col: Print "Press key if you see your choice > " + s1$;
  42.     t = Timer(.001)
  43.     _KeyClear
  44.     k$ = InKey$
  45.     While Len(k$) = 0 And Timer(.001) - t < 3 + .25 * Len(s1$)
  46.         k$ = InKey$
  47.         _Limit 60
  48.     Wend
  49.     If Len(k$) Then
  50.         If Len(s1$) = 1 Then BinarySelect$ = s1$ Else BinarySelect$ = BinarySelect$(row, col, s1$)
  51.     Else
  52.         BinarySelect$ = BinarySelect$(row, col, s2$)
  53.     End If
  54.  
  55.  

It's still something that needs a little practice with but it is much easier to use than my first idea.

On the confirmation for trying again, it still catches me by surprise the first Y for confirming to try again.
No worries, if there is no confirmation for any letter, it will cycle around and offer the groups over and over again.

For the Game, I put an Ecs keypress in there to quit game as it was in Full Screen mode with no top right window X to close.



58
Programs / Marvin has a ball
« on: September 29, 2021, 09:21:11 pm »
My first use of an image sheet I think.

Here is what the SdlBasic code looked like 6 years ago!
Code: [Select]
'
'    Marvin has a ball.sdlbas 2015-05-29 j&m
'
'    needs: Marvin32x48.png  y=0 back, y=1 right, y=2 front, y=3 left
'
sub rball(x)
for i=12 to 0 step -1
ink(rgb(255-i*21,0,0))
fillcircle(x,388,i)
next
end sub
sub bball(x,y)
for i=125 to 0 step -1
ink(rgb(255-i*2,0,0))
fillcircle(x,y,i)
next
end sub
'========================================================== main
setDisplay(600,500,32,1)
autoback(-2)
loadImage("marvin32x48.png",1)
x = 0:y=0:imx=0:imy=1:marvinX=425:marvinY=352
ink(rgb(0,215,65))
bar(0,400,screenwidth, screenheight)
ink(rgb(0,185,255))
bar(0,0,screenwidth,400)
while 0=0
    ink(rgb(0,185,255))
bar(0,0,screenwidth,400)
    blt(1,(marvinX mod 4)*32,imy*48,32,48,marvinX,marvinY)
rball(marvinX+35)
    screenswap
    wait(50)
    marvinX = marvinX + 1
    if marvinX>600 then: exit while: end if
wend
imy=3
while 0=0
    ink(rgb(0,185,255))
bar(0,0,screenwidth,400)
    blt(1,(marvinX mod 4)*32,imy*48,32,48,marvinX,marvinY)
bball(marvinX+130,275)
    screenswap
    marvinX = marvinX - 21
    if marvinX<-295 then
text(270,120,16,"wait. . .  ")
screenswap
wait(3000)
exit while
end if
wend
imy=2:marvinX=300:marvinY=400:ballY=marvinY+173
while 0=0
    ink(rgb(0,185,255))
bar(0,0,screenwidth,400)
bball(marvinX+16,ballY)
blt(1,(marvinY mod 4)*32,imy*48,32,48,marvinX,marvinY)
ink(rgb(0,215,65))
bar(0,400,screenwidth, screenheight)
    screenswap
    marvinY = marvinY -1
if marvinY<-80 then
end
    elseif marvinY=0 then
marvinY=-49
elseif marvinY<90 then
ballY =290 :imy=0
else
ballY= marvinY+173
end if
wend


Today's tranlation and mods for QB64:
Code: QB64: [Select]
  1. _Title "Marvin has a ball" ' b+ trans from SdlBasic 2021-09-29
  2. ' needs: Marvin32x48.png  y=0 back, y=1 right, y=2 front, y=3 left
  3. Screen _NewImage(800, 800 * ratio, 32)
  4. m& = _LoadImage("marvin32x48.png")
  5. x = 0: y = 0: imx = 0: imy = 1: marvinX = 425: marvinY = 352
  6. Line (0, 400)-(_Width, _Height), _RGB32(0, 215, 65), BF
  7. Line (0, 0)-(_Width, 400), _RGB32(0, 185, 255), BF
  8. While _KeyDown(27) = 0 ' sisyphus
  9.     Line (0, 0)-(_Width, 400), _RGB32(0, 185, 255), BF
  10.     blt m&, (marvinX Mod 4) * 32, imy * 48, 32, 48, marvinX, marvinY
  11.     drawBall marvinX + 35, 400 - 12, 12, &HFFFF0000
  12.     _Display
  13.     _Limit 40
  14.     marvinX = marvinX + 1
  15.     If marvinX > _Width Then Exit While
  16. imy = 3: l = 5
  17. While _KeyDown(27) = 0 ' run!
  18.     Line (0, 0)-(_Width, 400), _RGB32(0, 185, 255), BF
  19.     blt m&, (marvinX Mod 4) * 32, imy * 48, 32, 48, marvinX, marvinY
  20.     drawBall marvinX + 130, 275, 125, &HFFFF0000
  21.     _Display
  22.     marvinX = marvinX - 21
  23.     If marvinX < -295 Then
  24.         _Display
  25.         _Delay 3
  26.         Exit While
  27.     End If
  28.     _Limit l
  29.     l = l + .5
  30. imy = 2: marvinX = _Width / 2 - 16: marvinY = 400: ballY = marvinY + 173
  31. While _KeyDown(27) = 0 ' conquer
  32.     Line (0, 0)-(_Width, 400), _RGB32(0, 185, 255), BF
  33.     drawBall marvinX + 16, ballY, 125, &HFFFF0000
  34.     blt m&, (marvinY Mod 4) * 32, imy * 48, 32, 48, marvinX, marvinY
  35.     Line (0, 400)-(_Width, _Height), _RGB32(0, 215, 65), BF
  36.     _Display
  37.     _Limit 10
  38.     marvinY = marvinY - 1
  39.     If marvinY < -80 Then
  40.         End
  41.     ElseIf marvinY = 0 Then
  42.         marvinY = -49
  43.     ElseIf marvinY < 90 Then
  44.         ballY = 280: imy = 0
  45.     Else
  46.         ballY = marvinY + 173
  47.     End If
  48.  
  49. Sub blt (imageHandle&, sx, sy, sw, sh, dx, dy) ' for johnno to trans SdlBasic to QB64
  50.     _PutImage (dx, dy), imageHandle&, 0, (sx, sy)-Step(sw, sh)
  51.  
  52. Sub drawBall (x, y, r, c As _Unsigned Long)
  53.     Dim rred As Long, grn As Long, blu As Long, rr As Long, f
  54.     rred = _Red32(c): grn = _Green32(c): blu = _Blue32(c)
  55.     For rr = r To 0 Step -1
  56.         f = 1 - rr / r
  57.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  58.     Next
  59.  
  60. Sub fcirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
  61.     Dim Radius As Long, RadiusError As Long
  62.     Dim X As Long, Y As Long
  63.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  64.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  65.     Line (CX - X, CY)-(CX + X, CY), C, BF
  66.     While X > Y
  67.         RadiusError = RadiusError + Y * 2 + 1
  68.         If RadiusError >= 0 Then
  69.             If X <> Y + 1 Then
  70.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  71.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  72.             End If
  73.             X = X - 1
  74.             RadiusError = RadiusError - X * 2
  75.         End If
  76.         Y = Y + 1
  77.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  78.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  79.     Wend
  80.  

@johnno56  you might be interested in the blt translation for use in QB64, after I looked up SdlBasic docs for the argument variables it was easy to translate and it worked the first time!

Attached is the zip of code with image.

59
Programs / Iterated digits squaring - Rosetta Code
« on: September 04, 2021, 02:28:15 pm »
ref http://rosettacode.org/wiki/Iterated_digits_squaring

Who tried this recursively?
Code: QB64: [Select]
  1. _Title "Iterated digits squaring - Rosetta Code" ' b+ try 2021-09-04
  2. ' ref http://rosettacode.org/wiki/Iterated_digits_squaring
  3. start = Timer(.001)
  4. For i& = 1 To 100000000 ' 100 million
  5.     If i& Mod 1000000 = 0 Then Print i&, c& ' progress almost 8 minutes on my system
  6.     c& = c& + sumSQRdigitsIs89%(i&)
  7. Print c&, Timer(.001) - start
  8.  
  9. Function sumSQRdigitsIs89% (number&)
  10.     n$ = _Trim$(Str$(number&))
  11.     For i = 1 To Len(n$)
  12.         sum& = sum& + Val(Mid$(n$, i, 1)) ^ 2
  13.     Next
  14.     If sum& = 89 Then
  15.         sumSQRdigitsIs89% = 1
  16.     ElseIf sum& <> 1 Then
  17.         sumSQRdigitsIs89% = sumSQRdigitsIs89%(sum&)
  18.     End If
  19.  

Of course there must be faster ways even for QB64 ;-))

Hint get rid of the string stuff :)

60
Programs / Stem and leaf - Rosetta Code
« on: September 04, 2021, 12:58:28 pm »
ref: http://rosettacode.org/wiki/Stem-and-leaf_plot

Not bad for not having built in sort:
Code: QB64: [Select]
  1. _Title "Stem and leaf - Rosetta Code" ' b+ start 2021-09-04
  2. Screen 12: Color 2 ' ref: http://rosettacode.org/wiki/Stem-and-leaf_plot
  3. d$ = "12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104"
  4. d$ = d$ + " 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127"
  5. d$ = d$ + " 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 "
  6. d$ = d$ + "124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 "
  7. d$ = d$ + "120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146 "
  8. For stem = 0 To 14
  9.     If stem = 0 Then s$ = "" Else s$ = _Trim$(Str$(stem))
  10.     ReDim sort$(0): b$ = ""
  11.     For i = 1 To Len(d$) ' read through data and pick numbers builds b$
  12.         If Mid$(d$, i, 1) = " " Then
  13.             If Mid$(b$, 1, Len(b$) - 1) = s$ Then loadSort Right$(b$, 1), sort$()
  14.             b$ = ""
  15.         Else
  16.             b$ = b$ + Mid$(d$, i, 1)
  17.         End If
  18.     Next
  19.     Print Space$(4); Right$(Str$(stem), 2); " & ";
  20.     For i = 1 To UBound(sort$)
  21.         Color Val(sort$(i)) + 5: Print sort$(i); " ";
  22.     Next
  23.     Color 2: Print
  24.  
  25. Sub loadSort (insertN As String, dynArr() As String) '  version 2020-06-07
  26.     'note this leaves dynArr(0) empty! so ubound of array is also number of items in list
  27.     ub = UBound(dynArr) + 1
  28.     ReDim _Preserve dynArr(LBound(dynArr) To ub) As String
  29.     For j = 1 To ub - 1
  30.         If insertN < dynArr(j) Then '  GT to LT according to descending or ascending sort
  31.             For k = ub To j + 1 Step -1
  32.                 dynArr(k) = dynArr(k - 1)
  33.             Next
  34.             Exit For
  35.         End If
  36.     Next
  37.     dynArr(j) = insertN
  38.  

Colorized
 
Stem and leaf.PNG

Pages: 1 2 3 [4] 5 6 ... 21