読者です 読者をやめる 読者になる 読者になる

`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

Lisp Game Programming <Step9-2>

2番目はenemy1、enemy2、enemy3をランダムに発生させるプログラム

enemy1はランダムに設定したウィンドウ上の位置から垂直に下降
enemy2はランダムに設定したウィンドウ下の位置が真ん中より右の時は左上に上昇し、左の時は右上に上昇
enemy3はランダムに設定したウィンドウ左右の位置で画面の半分より上の位置から真横に移動

;; Step9 <Set Enemy>
;; -----------------------------------------------------------------------------------------------
(defgeneric Set-enemy (timing foe))

(defmethod Set-enemy (timing foe)
  "enemy appear position set"
  (when (= (mod (scroll timing) (interval timing)) 0)
    (let ((enemy (make-instance 'entity :id (+ (random 3) 2) :state 0)))
      (push enemy (enemy-list foe)))
      (dolist (enemy (enemy-list foe))
        (when (= (state enemy) 0)
          (setf (state enemy) 1) ; set enemy state alive
          (ecase (id enemy)
            (2 (progn ; enemy type1 appears from top
              (setf (x enemy) (random 448))
              (setf (y enemy) 0)
              (setf (dx enemy) 0)
              (setf (dy enemy) 2)))
            (3 (progn ; enemy type2 appears from bottom
              (setf (x enemy) (random 448))
              (setf (y enemy) *graphic-height*)
              (if (> (x enemy) (- (/ *graphic-width* 2) 16))
                (setf (dx enemy) -2) ; if enemy type2 appears right half
              (setf (dx enemy) 2)) ; if enemy type3 appears left half
              (setf (dy enemy) -1)))
            (4 (progn ; enemy type3 appears from left or right
              (ecase (random 2)
                (0 (progn ; from left
                  (setf (x enemy) -32)
                  (setf (dx enemy) 3)))
                (1 (progn ; from right
                  (setf (x enemy) *graphic-width*)
                  (setf (dx enemy) -3)
                  (setf (y enemy) (random (/ *graphic-height* 2)))
                  (setf (dy enemy) 1)))))))))))

f:id:tomekame0126:20140714211443j:plain