Scheme 如何保存游戏方案的状态
例如,我有一个游戏: 这个游戏就是用键盘或鼠标移动一只鸟Scheme 如何保存游戏方案的状态,scheme,racket,Scheme,Racket,例如,我有一个游戏: 这个游戏就是用键盘或鼠标移动一只鸟 (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-d
(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))))
因此,假设您在某个地方有一个循环,将新状态传递给下一个迭代。只需将其包装为保存状态,即可在最后一段时间内保存一个新文件。read将读取该文件,可能是在启动时
您可能需要检查它是否存在,但这基本上是您需要读取/存储状态的内容