Created
March 3, 2021 18:54
-
-
Save uvtc/ad745eb69ed16567d8099f606edd4e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env janet | |
(import jaylib :as jl) | |
(def screen-width 600) | |
(def screen-height 300) | |
(def pl-sz 20) | |
# Start the player (the square) in the middle of the screen, | |
# with some known initial velocity. | |
(def pl @{:x (/ screen-width 2) | |
:y (/ screen-height 2) | |
:vx 2 | |
:vy 4}) | |
(defn init-game | |
[] | |
(jl/init-window screen-width | |
screen-height | |
"Bouncing Square") | |
(jl/set-target-fps 60) | |
(jl/hide-cursor)) | |
(defn update-game | |
[] | |
# First, move the square further whichever | |
# way it's already going. | |
(+= (pl :x) (pl :vx)) | |
(+= (pl :y) (pl :vy)) | |
# If it has hit a wall, reverse its | |
# direction away from that wall. | |
(cond | |
(> (pl :x) (- screen-width pl-sz)) | |
(do (put pl :x (- screen-width pl-sz)) | |
(put pl :vx (- (pl :vx)))) | |
(< (pl :x) 0) | |
(do (put pl :x 0) | |
(put pl :vx (- (pl :vx)))) | |
(> (pl :y) (- screen-height pl-sz)) | |
(do (put pl :y (- screen-height pl-sz)) | |
(put pl :vy (- (pl :vy)))) | |
(< (pl :y) 0) | |
(do (put pl :y 0) | |
(put pl :vy (- (pl :vy)))))) | |
(defn draw-game | |
[] | |
(jl/draw-fps 10 10) | |
(jl/draw-text (string "x: " (pl :x)) | |
10 30 16 :blue) | |
(jl/draw-text (string "y: " (pl :y)) | |
10 50 16 :green) | |
(jl/draw-text (string "v_x: " (pl :vx)) | |
10 70 16 :blue) | |
(jl/draw-text (string "v_y: " (pl :vy)) | |
10 90 16 :green) | |
# Draw the player. | |
(jl/draw-rectangle (pl :x) (pl :y) pl-sz pl-sz :red)) | |
(defn main | |
[& args] | |
(init-game) | |
(while (not (jl/window-should-close)) | |
(jl/begin-drawing) | |
(jl/clear-background [0 0 0]) | |
(update-game) | |
(draw-game) | |
(jl/end-drawing)) | |
(jl/close-window)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment