举个例子,我有这样一个游戏:
这个游戏是在键盘或鼠标上移动一只鸟
(define-struct estado (ANCHO ALTO vel tiempo mov punto))
(define mapa (bitmap "mapa.png"))
(define Projo (bitmap "Projo.png"))
(define mario (bitmap "mario.png"))
(define (crear-mundo estado)
(big-bang estado
[to-draw pintar]
[on-tick tiempo-nuevo]
[on-mouse click]
[on-key cambiar-al-nuevo-mundo-teclado]
[stop-when fin-juego]))
(define (pintar nuevo-mundo)
(cond
[(colisión nuevo-mundo area)
(place-image Projo
(posn-x (estado-punto nuevo-mundo))
(posn-y (estado-punto nuevo-mundo))
(place-image (text (string-append "Tiempo: " (number->string (quotient (estado-tiempo nuevo-mundo) 28))) 12 "red") 40 20 mapa)
)]
[else (place-image Projo
(posn-x (estado-punto nuevo-mundo))
(posn-y (estado-punto nuevo-mundo))
;(place-image mario 750 500 (empty-scene 800 600))
;(place-image mario 750 500 mapa)
(place-image (text (string-append "Tiempo: " (number->string (quotient (estado-tiempo nuevo-mundo) 28))) 12 "green") 40 20 mapa)
)]
)
)
创建游戏的新状态,其中make-posn是鸟的位置
(crear-mundo (make-estado 800 600 10 0 0 (make-posn 100 100)))
当游戏运行时,我如何用鸟的位置保存游戏状态,以及什么位置发生了变化
发布于 2014-06-10 20:26:35
最简单的方法就是使用标准的写和读:
#!racket
(define file "file.rc")
(define (save config)
(with-output-to-file file
(lambda () (write config))
#:mode 'text
#:exists 'truncate)
config)
(define (read)
(with-input-from-file file
(lambda () (read))))
因此,假设你有一个循环,在这里你将新的状态传递给下一次迭代。只需将其包装在(save state)
中,就可以保存上一个新文件。(read)
可能会在您启动时读取该文件。
您可能需要检查它是否存在,但这基本上是您读取/存储状态所需的。
https://stackoverflow.com/questions/24134484
复制相似问题