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

`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

Lisp Game Programming <Step7>

ゲームフィールドクラスをつくり、shipがゲーム画面からはみ出さないようにするメソットを追加

;; step7 <Game Field>
;; -----------------------------------------------------------------------------------------------
(defclass game-field ()
 ((%field-x :initarg :field-x :initform 0 :reader field-x)
  (%field-y :initarg :field-y :initform 0 :reader field-y)
  (%width :initarg :width :initform 480 :reader width)
  (%height :initarg :height :initform 480 :reader height))
 (:documentation "The Game-field Class"))

(defgeneric Fix-ship-position (ship game-field))
(defmethod Fix-ship-position (ship game-field)
  (when (< (x ship) (field-x game-field)) (setf (x ship) (field-x game-field)))
  (when (< (y ship) (field-y game-field)) (setf (y ship) (field-y game-field)))
  (when (> (x ship) (- (width game-field) 32)) (setf (x ship) (- (width game-field) 32)))
  (when (> (y ship) (- (height game-field) 32)) (setf (y ship) (- (height game-field) 32))))

メインルーチンは以下

(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 - 7
  (let ((keystate (make-instance 'keystate)) ; step 6 add sentence
         (game-field (make-instance 'game-field)) ; step 7 add sentence
         (ship (make-instance 'entity :imageid 0 :id 0 :x 224 :y 416
                                                     :width 32 :height 32 :dx 4 :dy 4 :state 1))(
         (score-ship (make-instance 'object :imageid 0 :id 0 :x 496 :y 128))
         (mapchip (make-instance 'object :imageid 1 :id 0)))

  (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 4
    (Scroll mapchip) ; scroll background
    ; step 3
    (when (= (state ship) 1)
      (Draw ship)) ; draw ship
    ; step 5
    (Score-panel score-ship) ; draw score panel

  (sdl:update-display))))))