Skip to content

Instantly share code, notes, and snippets.

@mikemar10
Last active March 5, 2021 01:44
Show Gist options
  • Save mikemar10/72db7de78151072e5629d3526e44bb4e to your computer and use it in GitHub Desktop.
Save mikemar10/72db7de78151072e5629d3526e44bb4e to your computer and use it in GitHub Desktop.
A very basic pong game written for https://rxi.itch.io/cel7
(= title "Pong")
(= width 32)
(= height 24)
; macros
(= incr (mac (x) (list '= x (list '+ x 1))))
(= decr (mac (x) (list '= x (list '- x 1))))
(= negate (mac (x) (list '= x (list '* x -1))))
(= apply (mac (f args) (list f args)))
(= <<= (mac (a b c) (list 'and (list '<= a b) (list '<= b c))))
(= hit-paddle (fn ()
(or
(and (is bx 0) (<<= (- ly (// ph 2)) by (+ ly (// ph 2))))
(and (is bx (- width 1)) (<<= (- ry (// ph 2)) by (+ ry (// ph 2))))
nil
)
))
(= keydown (fn (k)
(if
(is k "escape") (quit)
(is k "s") (incr ly)
(is k "w") (decr ly)
(is k "down") (incr ry)
(is k "up") (decr ry)
(is k "left") (decr ph)
(is k "right") (incr ph)
(is k "r") (init)
)
))
(= step (fn ()
; clear screen
(fill 0 0 width height " ")
(if debug (do
(put 0 0 ph)
(put 0 1 vx)
(put 0 2 vy)))
(color 69) ; nice!
(put (// width 4) 0 ls)
(put (- width (// width 4)) 0 rs)
(color 1)
(let i (- ly (// ph 2)))
(while (<= i (+ ly (// ph 2)))
(put lx i "|")
(incr i))
(let i (- ry (// ph 2)))
(while (<= i (+ ry (// ph 2)))
(put rx i "|")
(incr i))
(color (rand 8))
(put __bx __by ".")
(color (rand 8))
(put _bx _by "o")
(color 2)
(put bx by "0")
(if
(<= win ls) (do
(color 23)
(put 0 0 "LEFT PLAYER WINS!")
(put 0 1 "PRESS R TO RESTART!")
)
(<= win rs) (do
(color 23)
(put 0 0 "RIGHT PLAYER WINS!")
(put 0 1 "PRESS R TO RESTART!")
)
)
(= __bx _bx)
(= __by _by)
(= _bx bx)
(= _by by)
(= bx (+ vx bx))
(= by (+ vy by))
(if (<= bx 0) (do
(negate vx)
(if (hit-paddle) (negate vy) (incr rs))))
(if (<= (- width 1) bx) (do
(negate vx)
(if (hit-paddle) (negate vy) (incr ls))))
(if (<= by 0) (negate vy))
(if (<= (- height 1) by) (negate vy))
))
(= init (fn ()
; paddle coords
(= lx 0)
(= ly (// height 2))
(= rx (- width 1))
(= ry (// height 2))
; paddle height
(= ph 3)
; ball coords
(= bx (// width 2))
(= by (// height 2))
(= _bx bx)
(= _by by)
(= __bx bx)
(= __by by)
; score
(= ls 0)
(= rs 0)
; win score
(= win 10)
(if (is (rand 2) 0) (= vx 1) (= vx -1))
(if (is (rand 2) 0) (= vy 1) (= vy -1))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment