get player moving around the level

Press any key to quit" 10 10))
This commit is contained in:
Travis Shears 2026-04-06 20:51:00 +02:00
parent 9a79400080
commit 06d101cb08
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469

View file

@ -4,6 +4,7 @@
(var dust-sprite nil) ; 35x35 pixels
(local game-state {
:player-pos [0 0]
:player-dir :n ; n = north, s = south, e = east, w = west
:level 1
:world []
})
@ -69,8 +70,9 @@ while 1 do love.event.push('stdin', io.read('*line')) end") :start))
[false _] (do
(love.graphics.setColor 1 1 1) ; reset color to white (no tinting)
(love.graphics.draw dust-sprite (+ 45 (* 25 (- x 1))) (+ 45 (* 25 (- y 1)))))
[true :wall] (love.graphics.rectangle "fill" (+ 50 (* 25 (- x 1))) (+ 50 (* 25 (- y 1))) 25 25)
[true :wall] (do
(love.graphics.setColor (unpack black))
(love.graphics.rectangle "fill" (+ 50 (* 25 (- x 1))) (+ 50 (* 25 (- y 1))) 25 25))
)
)
)
@ -80,11 +82,30 @@ while 1 do love.event.push('stdin', io.read('*line')) end") :start))
; (print (fennel.view game-state))
(fn draw-player []
(love.graphics.setColor 1 1 1) ; reset color to white (no tinting)
(let [[player-x player-y] (. game-state :player-pos)]
(let [
[player-x player-y] (. game-state :player-pos)
rot (case (. game-state :player-dir)
:n 0
:s (* math.pi 1)
:e (* math.pi 0.5)
:w (* math.pi 1.5))
x-offset (case (. game-state :player-dir)
:n 0
:s 25
:e 25
:w 0)
y-offset (case (. game-state :player-dir)
:n 0
:s 25
:e 0
:w 25)
]
(love.graphics.draw player-sprite
(* 25 player-x)
(* 25 player-y)
0 1.25 1.25)))
(+ (* 25 player-x) x-offset 25)
(+ (* 25 player-y) y-offset 25)
rot 1.25 1.25)))
(fn draw-ghost-grid []
(love.graphics.setColor (unpack black-half-tone))
@ -109,4 +130,23 @@ while 1 do love.event.push('stdin', io.read('*line')) end") :start))
; (love.graphics.print "Hello from Fennel!\nPress any key to quit" 10 10))
(fn love.keypressed [key]
(love.event.quit))
(let [
[player-x player-y] (. game-state :player-pos)
[next-player-x next-player-y] (case key
:left [(- player-x 1) player-y]
:up [player-x (- player-y 1)]
:right [(+ player-x 1) player-y]
:down [player-x (+ player-y 1)]
_ [player-x player-y])
next-cell (. game-state :world next-player-y next-player-x)
valid-move (not= :wall (. next-cell :type))
]
(when valid-move
(tset game-state :player-pos [next-player-x next-player-y]))
(tset game-state :world next-player-y next-player-x :revealed true)
(case key
:left (tset game-state :player-dir :w)
:up (tset game-state :player-dir :n)
:right (tset game-state :player-dir :e)
:down (tset game-state :player-dir :s)))
)