例如我有这样一个游戏:
这个游戏就是用键盘或鼠标移动一只鸟
(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)))
如何保存游戏运行时小鸟的位置以及位置变化
最简单的方法就是使用标准的读写:
#!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)
将读取该文件,可能在您启动时读取。
你可能需要检查它是否存在,但这基本上是你需要读取/存储状态