Lisp Game Programming <Step9-3>
3番目はenemyのstateが0になった場合にenemy-listからdeleteするプログラム
;; Step9 <Remove Dead Enemy>
;; -----------------------------------------------------------------------------------------------
(defgeneric Remove-dead-enemy (foe))
(defmethod Remove-dead-enemy (foe)
"dead enemy remove from list"
(setf (enemy-list foe)
(delete-if #'(lambda (enemy) (= (state enemy) 0)) (enemy-list foe))))
メインルーチンは以下
(defun Common-shooter ()
"main routine"
(sdl:with-init (sdl:sdl-init-video sdl:sdl-init-audio) ; use video and audio
(sdl:window 640 480 :position #(192 50) ; size 640*480, position x(192) y(50)
:title-caption "THE SHOOTER"
:icon-caption "THE SHOOTER"
:double-buffer T)
; :fullscreen T)
; step 3
(Initialize) ; graphics initialize
; step 5
(Set-font) ; set font
; step 2
(Open-sound) ; open audio and load sound data
; step 2
(Play-music *bg-music*) ; play music
; step 3 - 9
(let((ship (make-instance 'entity :imageid 0 :id 0 :x 224 :y 416 :width 32 :height 32 :dx 4 :dy 4 :state 1))
(mapchip (make-instance 'object :imageid 1 :id 0))
(timing (make-instance 'timing)) ; step4 add sentence
(score-ship (make-instance 'object :imageid 0 :id 0 :x 496 :y 128)) ;step5 add sentence
(score (make-instance 'score)) ; step5 add sentence
(keystate (make-instance 'keystate)) ; step6 add sentence
(game-field (make-instance 'game-field)) ; step7 add sentence
(shot (make-instance 'entity :imageid 0 :height 32 :dy 16 :id 1 :state 0)) ; step8 add sentence
(foe (make-instance 'foe))) ; step9 add sentence
(sdl:update-display)
(sdl:with-events (:poll)
(:quit-event ()
; step 2
(Stop-sound) ; sample and music stop
(Close-sound) ; close audio
t)
; step 6
(:key-down-event (:key key)
(if (sdl:key= key :SDL-KEY-ESCAPE)
(sdl:push-quit-event)
(Update-keystate key t keystate)))
(:key-up-event (:key key)
(Update-keystate key nil keystate))
(:idle ()
; Game body
; step 6
(Move-ship ship keystate)
; step 7
(Fix-ship-position ship game-field)
; step 8
(Move-shot shot)
(Set-shot shot ship keystate)
; step 4
(Scroll-background mapchip timing) ; scroll background
; step 3
(when (= (state ship) 1)
(Draw ship)) ; draw ship
; step 8
(when (= (state shot) 1)
(Draw shot)) ; draw shot
; step 9
(Move-enemy ship foe)
(Set-enemy timing foe)
(dolist (enemy (enemy-list foe))
(when (= (state enemy) 1)
(Draw enemy))) ; draw enemy
(Remove-dead-enemy foe)
; step 5
(Score-panel score score-ship) ; draw score panel
; step 4
(Scroll-counter timing)
(sdl:update-display))))))