4848(defun handle-mouse-button (button xm ym down)
4949 (if down
5050 (when (= button 1 )
51- (clear-selected-planet)
52- (dolist (planet *planet-list* )
53- (with-accessors ((x x) (y y) (r r) (s selected-p)) planet
54- (when (distance-less-than-p x y (global-x xm) (global-y ym) r)
55- (setf s t )
56- (setq *selected-planet* planet)
57- (if (sdl :key-down-p :sdl-key-space )
58- (spawn planet))
59- (return ))))
60- (when (and *selected-planet*
61- (eq (player *selected-planet* ) *player1* ))
62- (setq *show-arrow* t )))
51+ (clear-selected-planet)
52+ (dolist (planet *planet-list* )
53+ (with-accessors ((x x) (y y) (r r) (s selected-p)) planet
54+ (when (distance-less-than-p x y (global-x xm) (global-y ym) r)
55+ (setf s t )
56+ (setq *selected-planet* planet)
57+ (if (sdl :key-down-p :sdl-key-space )
58+ (spawn planet))
59+ (return ))))
60+ (when (and *selected-planet*
61+ (eq (player *selected-planet* ) *player1* ))
62+ (setq *show-arrow* t )))
6363 ; ; button up
6464 (when (= button 1 )
65- (when *show-arrow*
66- (dolist (planet *planet-list* )
67- (when (and (mouse-inside-planet-p planet)
68- (not (eq planet *selected-planet* )))
69- ; ; trying to move a star to another planet
70- (transport-stars *selected-planet* planet)
71- (return ))))
72- (setq *show-arrow* nil ))))
65+ (when *show-arrow*
66+ (dolist (planet *planet-list* )
67+ (when (and (mouse-inside-planet-p planet)
68+ (not (eq planet *selected-planet* )))
69+ ; ; trying to move a star to another planet
70+ (transport-stars *selected-planet* planet)
71+ (return ))))
72+ (setq *show-arrow* nil ))))
7373
7474(defun increase-star-speed ()
7575 (let ((speed (+ star-speed 0.5 )))
7676 (if (> speed star-speed-max)
77- (setq speed star-speed-max))
77+ (setq speed star-speed-max))
7878 (setq star-speed speed ))
7979 (setq *news* (format nil " Increase speed to ~a x" star-speed)))
8080(defun decrease-star-speed ()
8181 (let ((speed (- star-speed 0.5 )))
8282 (if (< speed star-speed-min)
83- (setq speed star-speed-min))
83+ (setq speed star-speed-min))
8484 (setq star-speed speed ))
8585 (setq *news* (format nil " Decrease speed to ~a x" star-speed)))
8686
9090 (sdl :push-quit-event))
9191 (:sdl-key-p
9292 (setf *paused*
93- (not *paused* ))
93+ (not *paused* ))
9494 (set-game-running *paused* ))
9595 (:sdl-key-r
9696 (clear-global-vars))
106106; ; draw the information line by line
107107(defun draw-information (&rest infos)
108108 (let ((x 10 )
109- (start-y 10 )
110- (step-y 15 ))
109+ (start-y 10 )
110+ (step-y 15 ))
111111 (dolist (info infos)
112112 (sdl :draw-string-solid-* info x start-y)
113113 (setq start-y (+ step-y start-y)))))
131131(defun move-screen-on-worldmap ()
132132 (let ((x (sdl :mouse-x)) (y (sdl :mouse-y)))
133133 (when (or (<= x margin-left)
134- (sdl :key-down-p :sdl-key-left ))
134+ (sdl :key-down-p :sdl-key-left ))
135135 (scroll-screen :left ))
136136 (when (or (>= x margin-right)
137- (sdl :key-down-p :sdl-key-right ))
137+ (sdl :key-down-p :sdl-key-right ))
138138 (scroll-screen :right ))
139139 (when (or (<= y margin-top)
140- (sdl :key-down-p :sdl-key-up ))
140+ (sdl :key-down-p :sdl-key-up ))
141141 (scroll-screen :up ))
142142 (when (or (>= y margin-bottom)
143- (sdl :key-down-p :sdl-key-down ))
143+ (sdl :key-down-p :sdl-key-down ))
144144 (scroll-screen :down )))
145145 (fix-screen-pos-overflow))
146146
147147(defun draw-game-frame ()
148148 (sdl :draw-rectangle-* (rx world-leftmost)
149- (ry world-topmost)
150- (- world-rightmost world-leftmost)
151- (- world-bottommost world-topmost)
152- :color game-frame-color))
149+ (ry world-topmost)
150+ (- world-rightmost world-leftmost)
151+ (- world-bottommost world-topmost)
152+ :color game-frame-color))
153153
154154(defun draw-planet-list ()
155155 (dolist (planet *planet-list* )
163163(defun draw-arrow (start end &key (color sdl :*white* ))
164164 (sdl :draw-line start end :color color)
165165 (let* ((rad (vec2rad (vector- end start)))
166- (v1 (rad2vec (rad-fix (+ rad 0.35 pi ))))
167- (v2 (rad2vec (rad-fix (- rad 0.35 pi ))))
168- (p1 (vector+ end (vector* (vector-norm v1) 20 )))
169- (p2 (vector+ end (vector* (vector-norm v2) 20 ))))
166+ (v1 (rad2vec (rad-fix (+ rad 0.35 pi ))))
167+ (v2 (rad2vec (rad-fix (- rad 0.35 pi ))))
168+ (p1 (vector+ end (vector* (vector-norm v1) 20 )))
169+ (p2 (vector+ end (vector* (vector-norm v2) 20 ))))
170170 (sdl :draw-line end (vector-floor p1) :color color)
171171 (sdl :draw-line end (vector-floor p2) :color color)))
172172
173173(defun draw-indication-arrow ()
174174 (when (and *show-arrow*
175- (not (mouse-inside-planet-p *selected-planet* )))
175+ (not (mouse-inside-planet-p *selected-planet* )))
176176 (let ((start (rvec (vect *selected-planet* )))
177- ; ; if target point inside a planet, directly arrow to
178- ; ; that
179- (end (dolist (planet *planet-list*
180- (vector (sdl :mouse-x) (sdl :mouse-y)))
181- (when (mouse-inside-planet-p planet)
182- (return (rvec (vect planet)))))))
177+ ; ; if target point inside a planet, directly arrow to
178+ ; ; that
179+ (end (dolist (planet *planet-list*
180+ (vector (sdl :mouse-x) (sdl :mouse-y)))
181+ (when (mouse-inside-planet-p planet)
182+ (return (rvec (vect planet)))))))
183183 (draw-arrow start end :color sdl :*white* ))))
184184
185185(defun draw-stars ()
@@ -198,49 +198,49 @@ the outter rect. the rect is filled by VALUE/FULL-VALUE"
198198 (let ((p (vector- pos (vector width height))))
199199 (sdl :draw-rectangle-* (vx p) (vy p) width height))
200200 (let* ((per (/ value full-value))
201- (w (* (- width 10 ) per))
202- (h (- height 10 ))
203- (p (vector- pos (vector 5 5 )))
204- (plist (list (vector+ p (vector (- w) (- h)))
205- (vector+ p (vector 0 (- h)))
206- p
207- (vector+ p (vector (- w) 0 )))))
201+ (w (* (- width 10 ) per))
202+ (h (- height 10 ))
203+ (p (vector- pos (vector 5 5 )))
204+ (plist (list (vector+ p (vector (- w) (- h)))
205+ (vector+ p (vector 0 (- h)))
206+ p
207+ (vector+ p (vector (- w) 0 )))))
208208 (sdl :draw-filled-polygon plist :color sdl :*white* )))
209209
210210(defun display-planet-life ()
211211 " if one planet is selected, display its life on the screen"
212212 (if *selected-planet*
213213 (display-percentage-rectangle (vector (- screen-width 60 )
214- (- screen-height 20 ))
215- (r *selected-planet* ) 24
216- (life *selected-planet* )
217- (r *selected-planet* ))))
214+ (- screen-height 20 ))
215+ (r *selected-planet* ) 24
216+ (life *selected-planet* )
217+ (r *selected-planet* ))))
218218
219219(defun winner-p ()
220220 " detect if there is winner"
221221 (let ((p1 nil )
222- (p2 nil ))
222+ (p2 nil ))
223223 (dolist (planet *planet-list* )
224224 (when (eq (player planet) *player1* )
225- (setq p1 t ))
225+ (setq p1 t ))
226226 (when (eq (player planet) *player2* )
227- (setq p2 t )))
227+ (setq p2 t )))
228228 (if (null p1)
229- *player2*
230- (if (null p2)
231- *player1*
232- nil ))))
229+ *player2*
230+ (if (null p2)
231+ *player1*
232+ nil ))))
233233
234234(defun draw-game-over ()
235235 (let ((win (eq (winner-p) *player1* )))
236236 (sdl :draw-string-solid-* (concatenate ' string
237- (if win
238- " You WIN!"
239- " You LOSE!" )
240- " Press [SPACE] to start new game!" )
241- (- (/ screen-width 2 ) 170 )
242- (/ screen-height 2 )
243- :color sdl :*yellow* )))
237+ (if win
238+ " You WIN!"
239+ " You LOSE!" )
240+ " Press [SPACE] to start new game!" )
241+ (- (/ screen-width 2 ) 170 )
242+ (/ screen-height 2 )
243+ :color sdl :*yellow* )))
244244
245245(defun update-AI ()
246246 (dolist (player *players* )
@@ -257,68 +257,75 @@ the outter rect. the rect is filled by VALUE/FULL-VALUE"
257257(defun main ()
258258 ; ; this might be useful when making binary images
259259 (in-package :org.xzpeter.game.starwar )
260- (sdl :with-init (sdl :sdl-init-video)
260+ (sdl :with-init (sdl :sdl-init-video sdl :sdl-init-audio )
261261 (setq sdl :*default-font* (sdl :initialise-font sdl :*font-8x13o* ))
262262 ; ; (sdl:initialise-default-font sdl:*font-9x18b*)
263263 (clear-global-vars)
264264 (format t " fullscreen: ~a~% " fullscreen)
265265 (sdl :window screen-width screen-height
266- :fullscreen fullscreen
267- :title-caption " Star War"
268- :icon-caption " Star War" )
266+ :fullscreen fullscreen
267+ :title-caption " Star War"
268+ :icon-caption " Star War" )
269269 (set-game-running t )
270- (sdl :with-events ()
271- (:quit-event () (prog1 t
272- (setf *running* nil )
273- (format t " Quit." )))
274- (:key-down-event (:key key)
275- (handle-key key))
276- (:mouse-button-down-event (:button button :x x :y y)
277- (handle-mouse-button button x y t ))
278- (:mouse-button-up-event (:button button :x x :y y)
279- (handle-mouse-button button x y nil ))
280- (:idle ()
281- (unless *game-over*
282- (sdl :clear-display bg-color)
283-
284- (unless *paused*
285- (update-planet-list))
286- (move-screen-on-worldmap)
287-
288- ; ; do all the drawings here
289- (draw-background *bg-stars* )
290- (draw-planet-list)
291- (draw-stars)
292- (draw-indication-arrow)
293- (draw-game-frame)
294- (display-planet-life)
295-
296- ; ; if the player is COM, update AI control
297- (update-AI)
298-
299- ; ; whether there is a winner
300- (let ((winner (winner-p)))
301- (when winner
302- (set-game-running nil )
303- (setq *game-over* t )
304- (draw-game-over)))
305-
306- (draw-information " Welcome Game StarWar"
307- (format nil " CurPos: (~a ,~a )"
308- (+ *screen-pos-x* (sdl :mouse-x))
309- (+ *screen-pos-y* (sdl :mouse-y)))
310- (format nil " CurStars: ~a /~a "
311- *star-count*
312- star-max-amount)
313- (concatenate ' string
314- (if *paused*
315- " [P] to UNPAUSE"
316- " [P] to PAUSE" )
317- " , [ESC] to quit" )
318- *news*
319- *debug-string* )
320-
321- (sdl :update-display))))))
270+ ; ; music background
271+ (sdl-mixer :OPEN-AUDIO)
272+ (let ((music (sdl-mixer :load-music " background.mp3" )))
273+ (sdl-mixer :play-music music :loop t )
274+ (sdl :with-events ()
275+ (:quit-event () (sdl-mixer :Halt-Music)
276+ (sdl-mixer :free music)
277+ (sdl-mixer :close-audio)
278+ (prog1 t
279+ (setf *running* nil )
280+ (format t " Quit." )))
281+ (:key-down-event (:key key)
282+ (handle-key key))
283+ (:mouse-button-down-event (:button button :x x :y y)
284+ (handle-mouse-button button x y t ))
285+ (:mouse-button-up-event (:button button :x x :y y)
286+ (handle-mouse-button button x y nil ))
287+ (:idle ()
288+ (unless *game-over*
289+ (sdl :clear-display bg-color)
290+
291+ (unless *paused*
292+ (update-planet-list))
293+ (move-screen-on-worldmap)
294+
295+ ; ; do all the drawings here
296+ (draw-background *bg-stars* )
297+ (draw-planet-list)
298+ (draw-stars)
299+ (draw-indication-arrow)
300+ (draw-game-frame)
301+ (display-planet-life)
302+
303+ ; ; if the player is COM, update AI control
304+ (update-AI)
305+
306+ ; ; whether there is a winner
307+ (let ((winner (winner-p)))
308+ (when winner
309+ (set-game-running nil )
310+ (setq *game-over* t )
311+ (draw-game-over)))
312+
313+ (draw-information " Welcome Game StarWar"
314+ (format nil " CurPos: (~a ,~a )"
315+ (+ *screen-pos-x* (sdl :mouse-x))
316+ (+ *screen-pos-y* (sdl :mouse-y)))
317+ (format nil " CurStars: ~a /~a "
318+ *star-count*
319+ star-max-amount)
320+ (concatenate ' string
321+ (if *paused*
322+ " [P] to UNPAUSE"
323+ " [P] to PAUSE" )
324+ " , [ESC] to quit" )
325+ *news*
326+ *debug-string* )
327+
328+ (sdl :update-display)))))))
322329
323330(defun run ()
324331 (run-game #' main))
0 commit comments