计划中的太空飞船游戏

时间:2013-02-03 01:45:00

标签: scheme racket

下面的方案代码是关于一艘带有太空飞船的游戏,现在它没有考虑太空船何时耗尽燃料,这正是我想要做的。这是代码:
;;这是问题rrset的代码 - Lunar Lander

(define (update ship-state fuel-burn-rate)  
 (make-ship-state  
   (+ (height ship-state) (* (velocity ship-state) dt)) ; height  
   (+ (velocity ship-state)  
      (* (- (* engine-strength fuel-burn-rate) gravity)  
         dt))                                           ; velocity  
   (cond (<= (fuel 0)  
        ((write-line "no fuel left")   
          'game-over)  
         (else  
           (- (fuel ship-state) (* fuel-burn-rate dt))))))       ; fuel  

(define (lander-loop ship-state)  
  (show-ship-state ship-state)  
  (if (landed? ship-state)  
  (end-game ship-state)  
  (lander-loop (update ship-state (get-burn-rate)))))  

(define (show-ship-state ship-state)  
  (write-line   
    (list 'height (height ship-state)  
          'velocity (velocity ship-state)  
          'fuel (fuel ship-state))))  

(define (landed? ship-state)  
  (<= (height ship-state) 0))  

(define (end-game ship-state)  
  (let ((final-velocity (velocity ship-state)))  
       (write-line final-velocity)  
       (cond ((>= final-velocity safe-velocity)  
               (write-line "good landing")  
               'game-over)  
             (else  
               (write-line "you crashed!")  
               'game-over))))  

(define (get-burn-rate)  
  (if (= (player-input) burn-key)  
      1  
      0))  

(define (play) (lander-loop (initial-ship-state)))  

(define (initial-ship-state)  
  (make-ship-state 50       ; 50 km high  
                   0        ; not moving (0 km/sec)  
                   20))     ; 20 kg of fuel left  

(define dt 1)               ; 1 second interval of simulation  

(define gravity 0.5)        ; 0.5 km/sec/sec  

(define safe-velocity -0.5) ; 0.5 km/sec or faster is a crash  

(define engine-strength 1)  ; 1 kilonewton-second  

(define (player-input)  
  (char->integer (prompt-for-command-char " action: ")))  

(define burn-key 32)   ;space key  

(define (make-ship-state height velocity fuel)  
  (list 'HEIGHT   height  
        'VELOCITY velocity  
        'FUEL     fuel))  

(define (height state) (second state))  
(define (velocity state) (fourth state))  
(define (fuel state) (sixth state))  

(define (second l) (cadr l))  
(define (fourth l) (cadr (cddr l)))  
(define (sixth l) (cadr (cddr (cddr l))))  

; Users of DrScheme or DrRacket: add these for compatibility with MIT Scheme...  
(define (write-line x)  
  (display x)  
  (newline))  

(define (prompt-for-command-char prompt)  
  (display prompt)  
  (read-char))  

要修改它,我修改了“更新”程序,所以现在有一个条件语句:

(define (update ship-state fuel-burn-rate)  
  (make-ship-state  
   (+ (height ship-state) (* (velocity ship-state) dt)) ; height  
   (+ (velocity ship-state)  
      (* (- (* engine-strength fuel-burn-rate) gravity)  
         dt))                                           ; velocity  
   (cond (<= (fuel 0)  
        ((write-line "no fuel left")   
          'game-over)  
         (else  
           (- (fuel ship-state) (* fuel-burn-rate dt))))))  

然而这不起作用。

1 个答案:

答案 0 :(得分:1)

您可能希望将条件添加到lander-loop过程。这是代码的一部分,用于检查游戏是否超过每个周期。

(define (lander-loop ship-state)  
  (show-ship-state ship-state)
  (if (or (landed? ship-state) (<= (fuel ship-state) 0))
    (end-game ship-state)
    (lander-loop (update ship-state (get-burn-rate)))))

然后您可以将您的燃料消息添加到end-game程序。