Lisp Game Programming <Step4>
64×64の大きさのmapchipを、(0,-64)の位置から横8個、縦に9個並べ、1dotずつ縦にスクロールさせていき、mapchipが96個分動いたら0にリセット
追加するプログラムは以下
;; step 4 <Scroll>
;; -----------------------------------------------------------------------------------------------
(defvar *scroll* 0) ; scroll counter
(defun Scroll (obj)
"draw background"
(setf (y obj) (+ -64 (mod *scroll* 64))) ; scroll start from y(-64) to y(0)
(incf *scroll*) ; scroll counter
(when (= *scroll* 3072)
(setf *scroll* 0)) ; mapchip 96 pieces move
(dotimes (i 9)
(setf (x obj) 0)
(dotimes (j 8)
(Draw obj)
(incf (x obj) 64))
(incf (y obj) 64)))
メインルーチンは以下
(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 2
(Open-sound) ; open audio and load sound data
; step 2
(Play-music *bg-music*) ; play music
; step 3 - 4
(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)))
(sdl:update-display)
(sdl:with-events (:poll)
(:quit-event ()
; step 2
(Stop-sound) ; sample and music stop
(Close-sound) ; close audio
t)
(:idle ()
; Game body
; step 4
(Scroll mapchip) ; scroll background
; step 3
(when (= (state ship) 1)
(Draw ship)) ; draw ship
(sdl:update-display))))))
以外と簡単に背景画面が縦スクロールするプログラムが完成した