Skip to content

Instantly share code, notes, and snippets.

@tankorsmash
Last active March 11, 2025 04:52
Show Gist options
  • Save tankorsmash/d1a6648cf8f4edc46dcd87aae09bf8eb to your computer and use it in GitHub Desktop.
Save tankorsmash/d1a6648cf8f4edc46dcd87aae09bf8eb to your computer and use it in GitHub Desktop.
Bouncing Tsoding using BQN and Rayed-BQN https://github.com/Brian-ED/rayed-bqn/
⟨color,window,draw, key⟩ ← r ← •Import "./rayed.bqn" # run from rayed-bqn root
fntSz‿spd ← 50‿3
boxClr ← 255‿0‿0‿255
pos‿dir ← ⟨0‿0, 1‿1⟩
w‿h ← ⟨1920÷2, 1080÷2⟩
PerFrame ← {𝕊⟨font⟩:
fps ← " FPS"∾˜•Repr window.GetFPS@ # display FPS
color.white‿font‿fntSz draw.Text r.mouse.GetPos⊸⋈fps
boxClr draw.Rectangle 100⊸+⊸≍˜pos # display rect
window.Close⍟⊢ (∨´ key.IsPressed¨ ⟨key.q,key.escape⟩) # close if ESC
# bounce off walls
FlipIfBounce ← { ¯1⊸×⍟((𝕩>𝕨-100) ∨ (𝕩<0))1}
newDir ← dir × w‿h FlipIfBounce pos
# change color and dir if bounce
{𝕊:dir ↩ newDir⋄boxClr↩(3 •rand.Range 255)∾255}⍟⊢ newDir≢dir
pos (dir×spd)⊸+↩ # apply speed and direction
𝕩
} draw._withCanvas⟜((3⥊16)∾255)
App ← {𝕊:
window.SetSize w‿h
fonts ← ⟨ r.font.LoadRaylib@ ⟩
PerFrame •_While_(¬window.ShouldClose) fonts
}
App window._openAs "Bouncing Tsoding"
@tankorsmash
Copy link
Author

I referenced examples/2_text/textTests.bqn, examples/14_dasher/dasher.bqn, and examples/4_games/snake.bqn to try to figure out how this all is meant to work. It was relatively straight forward, and runs great in WSL2 on Windows 10!

(caveat: I had to hardcode the 144hz, because raylib was incorrectly reporting a 60fps cap here: https://github.com/Brian-ED/rayed-bqn/blob/master/rayed.bqn#L273)

Learned about how you can increment a value as you instantiate them in the snake example to look up their indices in the state, even if I didn't end up needing it.

Happy to know what I could do better here, whether its style, tacitness or anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment