Author Topic: Jazzy Blocks 🟦🟥🟪🟩🟨🟧🎵🎶🔊  (Read 3667 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Jazzy Blocks 🟦🟥🟪🟩🟨🟧🎵🎶🔊
« on: March 25, 2022, 09:20:28 am »
Here's Jazzy Blocks. You can run the code below (available here just for reference) without the files in the zip, but it won't be half as jazzy.

Inspired by this: https://www.reddit.com/r/oddlysatisfying/comments/tm37bn/these_squares_bouncing/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

Piano notes from https://archive.org/details/24-piano-keys

Unpack to your QB64 folder, both the .bas and the notes subfolder.

 
Captura de Tela 2022-03-25 às 11.46.02.png


Code: QB64: [Select]
  1. Type Box
  2.     As Single x, y, xv, yv, w, h, lastPlayed
  3.     As Long sound
  4.  
  5. Dim As Box box(1 To 13), this
  6.  
  7. Screen _NewImage(600, 600, 32)
  8. _Title "Jazzy Blocks"
  9.  
  10. size = 40
  11. initialX = 0
  12. initialY = _Height - size
  13. Restore notes
  14. For i = 1 To 13
  15.     box(i).w = size
  16.     box(i).h = size
  17.     box(i).x = initialX
  18.     box(i).y = initialY
  19.     box(i).xv = map(i, 1, 13, .5, 2.5) 'going right
  20.     box(i).yv = map(i, 1, 13, -.5, -3) 'going up
  21.     box(i).color = hsb(map(i, 1, 13, 0, 180), 255, 127, 255)
  22.     Read note
  23.     fileName$ = "./24-piano-keys/key" + Right$("0" + LTrim$(Str$(note)), 2) + ".ogg"
  24.     If _FileExists(fileName$) Then
  25.         box(i).sound = _SndOpen(fileName$)
  26.     End If
  27.  
  28. notes:
  29. 'notes files are named key##.ogg, ranging from
  30. '01 (F) to 24 (E, two octaves up); these are
  31. 'the desired notes, but you can change the
  32. 'scale to play here, if you know what you're
  33. 'after.
  34. Data 1,3,5,6,8,10,12,13,15,17,18,20,22
  35.  
  36.     Line (0, 0)-(_Width - 1, _Height - 1), _RGB32(51), BF
  37.  
  38.     For i = 1 To 12
  39.         this = box(i)
  40.         Line (this.x + this.w / 2, this.y + this.h / 2)-(box(i + 1).x + box(i).w / 2, box(i + 1).y + box(i).h / 2), _RGB32(0)
  41.     Next
  42.  
  43.     For i = 1 To 13
  44.         this = box(i)
  45.         Line (this.x, this.y)-Step(this.w, this.h), this.color, BF
  46.         Line (this.x, this.y)-Step(this.w, this.h), _RGB32(0), B
  47.  
  48.         If timeElapsedSince(this.lastPlayed) < 1 Then
  49.             ripple = map(timeElapsedSince(this.lastPlayed), 0, 1, 1, 15)
  50.             rippleAlpha = map(ripple, 1, 15, 127, 0)
  51.             Line (this.x - ripple, this.y - ripple)-Step(this.w + ripple * 2, this.h + ripple * 2), _RGB32(255, rippleAlpha), B
  52.  
  53.             ripple = map(timeElapsedSince(this.lastPlayed), 0, .5, 1, 15)
  54.             rippleAlpha = map(ripple, 1, 15, 64, 0)
  55.             Line (this.x - ripple, this.y - ripple)-Step(this.w + ripple * 2, this.h + ripple * 2), _RGB32(255, rippleAlpha), B
  56.         End If
  57.  
  58.         this.x = this.x + this.xv
  59.         this.y = this.y + this.yv
  60.  
  61.         bump = 0
  62.         If this.x + this.w > _Width Then this.x = _Width - this.w: this.xv = this.xv * -1: bump = -1
  63.         If this.x < 0 Then this.x = 0: this.xv = this.xv * -1: bump = -1
  64.         If this.y + this.h > _Height Then this.y = _Height - this.h: this.yv = this.yv * -1: bump = -1
  65.         If this.y < 0 Then this.y = 0: this.yv = this.yv * -1: bump = -1
  66.         If bump <> 0 And this.sound <> 0 Then _SndPlayCopy this.sound: this.lastPlayed = Timer
  67.  
  68.         box(i) = this
  69.     Next
  70.     _Display
  71.     _Limit 60
  72.  
  73. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  74.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  75.  
  76. Function hsb~& (__H As _Float, __S As _Float, __B As _Float, A As _Float)
  77.     Dim H As _Float, S As _Float, B As _Float
  78.  
  79.     H = map(__H, 0, 255, 0, 360)
  80.     S = map(__S, 0, 255, 0, 1)
  81.     B = map(__B, 0, 255, 0, 1)
  82.  
  83.     If S = 0 Then
  84.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  85.         Exit Function
  86.     End If
  87.  
  88.     Dim fmx As _Float, fmn As _Float
  89.     Dim fmd As _Float, iSextant As Integer
  90.     Dim imx As Integer, imd As Integer, imn As Integer
  91.  
  92.     If B > .5 Then
  93.         fmx = B - (B * S) + S
  94.         fmn = B + (B * S) - S
  95.     Else
  96.         fmx = B + (B * S)
  97.         fmn = B - (B * S)
  98.     End If
  99.  
  100.     iSextant = Int(H / 60)
  101.  
  102.     If H >= 300 Then
  103.         H = H - 360
  104.     End If
  105.  
  106.     H = H / 60
  107.     H = H - (2 * Int(((iSextant + 1) Mod 6) / 2))
  108.  
  109.     If iSextant Mod 2 = 0 Then
  110.         fmd = (H * (fmx - fmn)) + fmn
  111.     Else
  112.         fmd = fmn - (H * (fmx - fmn))
  113.     End If
  114.  
  115.     imx = _Round(fmx * 255)
  116.     imd = _Round(fmd * 255)
  117.     imn = _Round(fmn * 255)
  118.  
  119.     Select Case Int(iSextant)
  120.         Case 1
  121.             hsb~& = _RGBA32(imd, imx, imn, A)
  122.         Case 2
  123.             hsb~& = _RGBA32(imn, imx, imd, A)
  124.         Case 3
  125.             hsb~& = _RGBA32(imn, imd, imx, A)
  126.         Case 4
  127.             hsb~& = _RGBA32(imd, imn, imx, A)
  128.         Case 5
  129.             hsb~& = _RGBA32(imx, imn, imd, A)
  130.         Case Else
  131.             hsb~& = _RGBA32(imx, imd, imn, A)
  132.     End Select
  133.  
  134. Function timeElapsedSince! (startTime!)
  135.     If startTime! > Timer Then startTime! = startTime! - 86400
  136.     timeElapsedSince! = Timer - startTime!
  137.  
* jazzyblocks.zip (Filesize: 1.07 MB, Downloads: 164)
« Last Edit: March 25, 2022, 01:07:06 pm by FellippeHeitor »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Jazzy Blocks 🟦🟥🟪🟩🟨🟧🎵🎶🔊
« Reply #1 on: March 25, 2022, 09:43:52 am »
  • Best Answer
  • saw this on reddit. nice.
    You're not done when it works, you're done when it's right.