#!/usr/bin/env bqn # TODO: score, bugfixes ⟨c⇐color⋄d⇐draw⋄k⇐key⋄win⇐window⟩ ← r ← •Import "./rayed-bqn/rayed.bqn" MT ← •MonoTime Collide ← {[a1⋄a2]𝕊[b1⋄b2]:¬∨´(a1∾b1)>(b2∾a2)} Paddle ← {up‿down𝕊x: # pos‿bbox init ⇐ 0.5‿@ Draw ⇐ {𝕊·‿bbox:c.white d.Rectangle bbox} Update ⇐ {ws‿dt𝕊pos‿bbox: cu‿cd ← ⟨∧´⥊bbox>0⋄∧´⥊bbox<1⊑ws⟩ u‿d ← ⟨cu×k.IsDown up⋄cd×k.IsDown down⟩ ⟨np←pos+dt×d-u⋄⍉⌊ws×x‿np+0.015≍○(¯1‿1⊸×)0.1⟩ } } c1 ← k.w ‿k.s Paddle 0.1 c2 ← k.up ‿k.down Paddle 0.9 ball ← { # pos‿vel‿bbox init ⇐ ⟨0.5‿0.5⋄1‿1⋄@⟩ Draw ⇐ {𝕊·‿·‿bbox:c.white d.Ellipse bbox} Update ⇐ {ws‿dt‿p1‿p2𝕊pos‿vel‿bbox: nb ← ¯16‿16+2‿2⥊ws×np ← pos+4÷˜dt×vx‿vy ← vel ceil‿floor ← -⟜¬∘(∧´⥊)¨((0⊸<)⋈(1⊑ws)⊸>) nb cp1‿cp2 ← -⟜¬∘Collide⟜nb∘(1⊸⊑)¨p1‿p2 np‿⟨vx×cp1×cp2⋄vy×floor×ceil⟩‿nb } } world ← { init ⇐ c1.init‿c2.init‿ball.init Update ⇐ {ws‿dt𝕊p1‿p2‿b: ⟨ np1 ← 𝕨 c1.Update p1 np2 ← 𝕨 c2.Update p2 ws‿dt‿np1‿np2 ball.Update b ⟩} Draw ⇐ {wx‿wy𝕊p1‿p2‿b: c.red‿c.red‿c.white d.Line¨ (∾˘⟜(0≍wy))∘(wx⊸×)¨ 0.1‿0.9‿0.5 c1.Draw p1⋄c2.Draw p2⋄ball.Draw b } } Consts ← {𝕊: font ⇐ r.font.LoadBQN@ Text ⇐ {c.white‿font‿𝕨 d.Text 𝕩} } Ticks ← {𝕨𝕊size‿debug‿time‿dt: ⟨ ⊢◶size‿win.GetSize r.raylib.IsWindowResized⟨⟩ debug≠'d'=𝕨 ⋄ nt ← MT@ ⋄ nt - time ⟩} Start ← {𝕊:⟨0⋄'d'Ticks(win.GetSize@)‿0‿(MT@)‿0.0016⋄world.init⟩} Frame ← {q𝕊state‿tick‿w: size‿debug‿time‿dt ← ntick ← tick Ticks˜ kp ← k.PressedChar@ ((16⊸q.Text)¨∘(⋈˜¨⟜(10⊸∾¨(16⊸×(1⊸+↕∘≠)))))⍟debug⟨ "FPS"∾˜•Repr win.GetFPS@ "Time: "∾•Repr time "Delta Time: "∾•Repr dt ⟩ ⊢◶{𝕊: 64‿32 q.Text¨⥊⋈˘⍉[64‿64⋈64‿128⋄"PONG"‿"press x to start"] ntick‿w∾˜'x'=kp }‿{𝕊: size world.Draw nw ← size‿dt world.Update w 1‿ntick‿nw } state } d._withCanvas_ c.black (Consts Frame•_While_(¬win.ShouldClose)Start)win._openAs "gametest"