Update Project Example for Latest Version of Ronin
This commit is contained in:
parent
e375668143
commit
2d2588426b
@ -1,23 +1,65 @@
|
|||||||
; objects
|
(clear)
|
||||||
(def box {:x 90 :y 0 :vy 0 :vx 5 :w 20 :h 20})
|
|
||||||
(def floor {:x 225 :y 580 :vx 0 :vy 0 :w 130 :h 40 :hit false})
|
; Game Objects
|
||||||
(def boundaries {:max-x 560 :min-x 10 :max-y 800})
|
(def ball:x 20)
|
||||||
|
(def ball:y 0)
|
||||||
|
(def ball:vy 0)
|
||||||
|
(def ball:vx 4)
|
||||||
|
(def ball:w 20)
|
||||||
|
(def ball:h 20)
|
||||||
|
|
||||||
|
(def platform:x 280)
|
||||||
|
(def platform:y 580)
|
||||||
|
(def platform:vx 0)
|
||||||
|
(def platform:vy 0)
|
||||||
|
(def platform:w 140)
|
||||||
|
(def platform:h 40)
|
||||||
|
(def platform:hit false)
|
||||||
|
(def boundaries:max-x 560)
|
||||||
|
(def boundaries:min-x 10)
|
||||||
|
(def boundaries:max-y 800)
|
||||||
|
|
||||||
(def walls
|
(def walls
|
||||||
(rect boundaries:min-x 10 boundaries:max-x 640))
|
(rect boundaries:min-x 10 boundaries:max-x 640))
|
||||||
; "physics"
|
|
||||||
|
|
||||||
|
; Physics
|
||||||
(def gravity .2)
|
(def gravity .2)
|
||||||
(def friction .99)
|
(def friction .99)
|
||||||
(def elastic-coefficient 1.1)
|
(def elastic-coefficient 1.1)
|
||||||
(def player-friction .90)
|
(def player-friction .90)
|
||||||
(def player-velocity 1.2)
|
(def player-velocity 1)
|
||||||
; controls
|
|
||||||
(def controls {:left false, :right false})
|
|
||||||
|
; Controls
|
||||||
|
(def right-arrow-1:key "ArrowRight")
|
||||||
|
(def right-arrow-1:type "keydown")
|
||||||
|
(def right-arrow-1:direction "right")
|
||||||
|
(def right-arrow-1:activate true)
|
||||||
|
(def right-arrow-2:key "ArrowRight")
|
||||||
|
(def right-arrow-2:type "keyup")
|
||||||
|
(def right-arrow-2:direction "right")
|
||||||
|
(def right-arrow-2:activate false)
|
||||||
|
|
||||||
|
(def left-arrow-1:key "ArrowLeft")
|
||||||
|
(def left-arrow-1:type "keydown")
|
||||||
|
(def left-arrow-1:direction "left")
|
||||||
|
(def left-arrow-1:activate true)
|
||||||
|
(def left-arrow-2:key "ArrowLeft")
|
||||||
|
(def left-arrow-2:type "keyup")
|
||||||
|
(def left-arrow-2:direction "left")
|
||||||
|
(def left-arrow-2:activate false)
|
||||||
|
|
||||||
(def commands
|
(def commands
|
||||||
({:key "ArrowRight" :type "keydown" :direction "right" :activate true} {:key "ArrowRight" :type "keyup" :direction "right" :activate false} {:key "ArrowLeft" :type "keydown" :direction "left" :activate true} {:key "ArrowLeft" :type "keyup" :direction "left" :activate false}))
|
(right-arrow-1 right-arrow-2 left-arrow-1 left-arrow-2))
|
||||||
;
|
|
||||||
(def state {:score 0})
|
; Game State:
|
||||||
;
|
(def controls:left false)
|
||||||
|
(def controls:right false)
|
||||||
|
(def state:score 0)
|
||||||
|
|
||||||
(defn rect-colliding
|
(defn rect-colliding
|
||||||
|
"Detect if there is a collision between two rectangles."
|
||||||
(r1 r2)
|
(r1 r2)
|
||||||
(and
|
(and
|
||||||
(lt r1:x
|
(lt r1:x
|
||||||
@ -27,24 +69,26 @@
|
|||||||
(lt r1:y
|
(lt r1:y
|
||||||
(add r2:y r2:h))
|
(add r2:y r2:h))
|
||||||
(gt
|
(gt
|
||||||
(add r1:y r1:h) r2:y )))
|
(add r1:y r1:h) r2:y)))
|
||||||
;
|
|
||||||
(defn check-collisions
|
(defn check-collisions
|
||||||
(b floor)
|
"Update ball and platform states on collision."
|
||||||
|
(b platform)
|
||||||
(
|
(
|
||||||
(set floor "hit" false)
|
(set platform "hit" false)
|
||||||
(if
|
(if
|
||||||
(rect-colliding b floor)
|
(rect-colliding b platform)
|
||||||
(
|
(
|
||||||
(set floor "hit" true)
|
(set platform "hit" true)
|
||||||
(set b "y"
|
(set b "y"
|
||||||
(sub floor:y box:h))
|
(sub platform:y ball:h))
|
||||||
(set b "vy"
|
(set b "vy"
|
||||||
(mul b:vy -1))
|
(mul b:vy -1))
|
||||||
(set b "vy"
|
(set b "vy"
|
||||||
(mul b:vy elastic-coefficient))))))
|
(mul b:vy elastic-coefficient))))))
|
||||||
;
|
|
||||||
(defn move-ball
|
(defn move-ball
|
||||||
|
"Update the state of the ball"
|
||||||
(b)
|
(b)
|
||||||
(
|
(
|
||||||
(set b "vy"
|
(set b "vy"
|
||||||
@ -55,15 +99,20 @@
|
|||||||
(add b:y b:vy))
|
(add b:y b:vy))
|
||||||
(set b "x"
|
(set b "x"
|
||||||
(add b:x b:vx))))
|
(add b:x b:vx))))
|
||||||
;
|
|
||||||
(defn check-reset-ball
|
(defn check-reset-ball
|
||||||
|
"Reset the position of the ball and increment score."
|
||||||
(b)
|
(b)
|
||||||
(
|
(
|
||||||
(if
|
(if
|
||||||
(gt box:y boundaries:max-y)
|
(gt b:y boundaries:max-y)
|
||||||
(reset))))
|
(
|
||||||
;
|
(set b "y" -100)
|
||||||
|
(set state "score"
|
||||||
|
(add state:score 1))))))
|
||||||
|
|
||||||
(defn draw
|
(defn draw
|
||||||
|
"Main drawing loop for world elements."
|
||||||
()
|
()
|
||||||
(
|
(
|
||||||
(clear)
|
(clear)
|
||||||
@ -73,25 +122,20 @@
|
|||||||
(concat "" state:score) "left" "monospace") "white" 3)
|
(concat "" state:score) "left" "monospace") "white" 3)
|
||||||
;draw-walls
|
;draw-walls
|
||||||
(stroke walls "white")
|
(stroke walls "white")
|
||||||
;draw-floor
|
;draw-platform
|
||||||
(stroke
|
(stroke
|
||||||
(rect floor:x floor:y floor:w floor:h) "white")
|
(rect platform:x platform:y platform:w platform:h) "white")
|
||||||
(if
|
(if
|
||||||
(eq floor:hit true)
|
(eq platform:hit true)
|
||||||
(fill
|
(fill
|
||||||
(rect floor:x floor:y floor:w floor:h) "white"))
|
(rect platform:x platform:y platform:w platform:h) "white"))
|
||||||
;draw-box
|
;draw-ball
|
||||||
(fill
|
(fill
|
||||||
(rect box:x box:y box:w box:h) "white")))
|
(rect ball:x ball:y ball:w ball:h) "white")))
|
||||||
;
|
|
||||||
(defn reset
|
|
||||||
()
|
|
||||||
(
|
|
||||||
(set box "y" -100)
|
|
||||||
(set state "score"
|
|
||||||
(add state:score 1))))
|
|
||||||
;
|
|
||||||
(defn on-hit-boundary
|
(defn on-hit-boundary
|
||||||
|
"On boundary hit call provided functions."
|
||||||
(thing on-hit-left on-hit-right)
|
(thing on-hit-left on-hit-right)
|
||||||
(
|
(
|
||||||
(if
|
(if
|
||||||
@ -101,8 +145,10 @@
|
|||||||
(if
|
(if
|
||||||
(lt thing:x boundaries:min-x)
|
(lt thing:x boundaries:min-x)
|
||||||
(on-hit-left))))
|
(on-hit-left))))
|
||||||
;
|
|
||||||
|
|
||||||
(defn move-player
|
(defn move-player
|
||||||
|
"Update the position of the player controlled platform"
|
||||||
(p)
|
(p)
|
||||||
(
|
(
|
||||||
(if
|
(if
|
||||||
@ -116,40 +162,49 @@
|
|||||||
(set p "vx"
|
(set p "vx"
|
||||||
(sub p:vx player-velocity))))
|
(sub p:vx player-velocity))))
|
||||||
(set p "x"
|
(set p "x"
|
||||||
(add floor:x floor:vx))
|
(add p:x p:vx))
|
||||||
(set p "vx"
|
(set p "vx"
|
||||||
(mul p:vx player-friction))))
|
(mul p:vx player-friction))))
|
||||||
;
|
|
||||||
|
|
||||||
(defn invert-vx
|
(defn invert-vx
|
||||||
(thing pos)
|
(thing pos)
|
||||||
(
|
(
|
||||||
(set thing "vx"
|
(set thing "vx"
|
||||||
(mul thing:vx -1))
|
(mul thing:vx -1))
|
||||||
(set thing "x" pos)))
|
(set thing "x" pos)))
|
||||||
;
|
|
||||||
|
|
||||||
(defn move
|
(defn move
|
||||||
|
"Update position and velocities for all elements."
|
||||||
()
|
()
|
||||||
(
|
(
|
||||||
(move-player floor)
|
(move-player platform)
|
||||||
(move-ball box)
|
(move-ball ball)
|
||||||
(check-collisions box floor)
|
(check-collisions ball platform)
|
||||||
(check-reset-ball box)
|
(check-reset-ball ball)
|
||||||
(on-hit-boundary box
|
(on-hit-boundary ball
|
||||||
'(invert-vx box boundaries:min-x)
|
(λ ()
|
||||||
'(invert-vx box
|
(invert-vx ball boundaries:min-x))
|
||||||
(sub boundaries:max-x box:w)))
|
(λ ()
|
||||||
(on-hit-boundary floor
|
(invert-vx ball
|
||||||
'(invert-vx floor boundaries:min-x)
|
(sub boundaries:max-x ball:w))))
|
||||||
'(invert-vx floor
|
(on-hit-boundary platform
|
||||||
(sub boundaries:max-x floor:w)))))
|
(λ ()
|
||||||
;
|
(invert-vx platform boundaries:min-x))
|
||||||
|
(λ ()
|
||||||
|
(invert-vx platform
|
||||||
|
(sub boundaries:max-x platform:w))))))
|
||||||
|
|
||||||
|
|
||||||
(defn update
|
(defn update
|
||||||
|
"Main update loop"
|
||||||
()
|
()
|
||||||
(
|
(
|
||||||
(move)
|
(move)
|
||||||
(draw)))
|
(draw)))
|
||||||
;
|
|
||||||
;
|
|
||||||
(defn handle-key
|
(defn handle-key
|
||||||
(e)
|
(e)
|
||||||
(
|
(
|
||||||
@ -161,7 +216,8 @@
|
|||||||
(eq e:key p:key)
|
(eq e:key p:key)
|
||||||
(eq e:type p:type))
|
(eq e:type p:type))
|
||||||
(set controls p:direction p:activate))))))
|
(set controls p:direction p:activate))))))
|
||||||
;
|
|
||||||
|
|
||||||
(on "animate" update)
|
(on "animate" update)
|
||||||
(on "key-down" handle-key)
|
(on "key-down" handle-key)
|
||||||
(on "key-up" handle-key)"key-up" handle-key)
|
(on "key-up" handle-key)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user