gpt4 book ai didi

clojure - 我如何用这个 Clojure 代码重写 (def)?

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:52 24 4
gpt4 key购买 nike

我已经根据 deWitter's game loop 编写了一个游戏循环.

但是,我不确定如何将其转换为更具功能性的状态。我意识到代码中可能需要保留一些可变状态,但是是否有清理无关的 def 的一般原则?

(ns beepboop.core)

(def ticks-per-second 25)
(def skip-ticks (/ 1000 ticks-per-second))
(def max-frameskip 5)

(defn update []
(println "Updating."))

(defn display [delta]
(println "Displaying with delta: " delta))

(defn -main []
(def *next-tick* (System/currentTimeMillis))
(while true
(def *loops* 0)
(while (and
(> (System/currentTimeMillis)
*next-tick*)
(< *loops*
max-frameskip))
(update)
(def *next-tick* (+ *next-tick* skip-ticks))
(def *loops* (+ *loops* 1)))
(display
(/ (+ (System/currentTimeMillis) skip-ticks (* -1 *next-tick*))
skip-ticks))))

最佳答案

您应该使用looprecur 来更新您的循环变量:

(defn -main []
(loop [next-tick (System/currentTimeMillis)]
(let [next-next
(loop [next-tick next-tick
loops 0]
(if (and (> (System/currentTimeMillis) next-tick)
(< loops max-frameskip))
(do (update)
(recur (+ next-tick skip-ticks) (+ loops 1)))
next-tick))]
(display (/ (+ (System/currentTimeMillis) skip-ticks (- next-next))
skip-ticks))
(recur next-next))))

关于clojure - 我如何用这个 Clojure 代码重写 (def)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com