-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplong.lisp
306 lines (239 loc) · 9.81 KB
/
plong.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
;;; plong.lisp --- a simple remixable ball-and-paddle example for Xelf
;; Copyright (C) 2014 David O'Toole
;; Author: David O'Toole <[email protected]>
;; Keywords: games
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Lesser General Public License for more details.
;; You should have received a copy of the GNU Lesser General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
;; First we must define a new package for our game, and import the
;; Xelf symbols. In this case the only exported function is PLONG,
;; which starts the game when called. (We'll show the definition of
;; PLONG later.)
(defpackage :plong
(:use :cl :xelf)
(:export plong))
(in-package :plong)
;; Here we define an arbitrary measurement unit used throughout, and
;; set up some variables to hold the height and width of the game
;; world.
(defparameter *unit* 16)
(defun units (n) (* *unit* n))
(defparameter *width* 640)
(defparameter *height* 480)
;; Now it's time to define some game objects. Xelf game objects are
;; called "nodes", and they can interact by being grouped into
;; "buffers" of different kinds. Naturally there are base classes
;; called NODE and BUFFER. These classes define the basic behaviors of
;; the game engine. Nodes are endowed with such properties as an (X Y
;; Z) position, width, height, and image to be displayed, and so on. The
;; default node behaviors also hook all game objects into buffer
;; features, such as collision detection, pathfinding, and
;; serialization.
;; To define nodes of your own, use DEFCLASS and give NODE as a
;; superclass. You can override the default values of NODE slots, as
;; well as add your own.
(defclass ball (node)
((height :initform (units 1))
(width :initform (units 1))
(color :initform "white")
(speed :initform 6)
(heading :initform (direction-heading :downright))))
;; The generic function UPDATE is called on each object once during
;; each game loop.
(defmethod update ((ball ball))
(with-slots (heading speed) ball
(move ball heading speed)))
;; Now we need walls around the game world in order to contain the
;; ball.
(defclass wall (node)
((color :initform "gray50")))
;; We want the ball to bounce off of the walls. The COLLIDE method is
;; called for every frame on all pairs of objects whose bounding boxes
;; collide during that frame.
(defmethod collide ((ball ball) (wall wall))
(with-slots (heading speed x y) ball
;; back away from wall
(move ball (opposite-heading heading) speed)
;; point toward player. (The function PADDLE is defined later.)
(aim ball (heading-between ball (paddle)))
;; sometimes choose another direction to prevent getting stuck
(percent-of-time 10 (incf heading (radian-angle 90)))))
;; The ball should emit a retro beep when colliding with any node. We
;; use DEFRESOURCE to let Xelf know about the sound file. (The Xelf
;; reference gives more information about DEFRESOURCE.)
(defresource "bip.wav" :volume 20)
(defmethod collide :after ((ball ball) (node node))
(play-sample "bip.wav"))
;; Now it's time to bash some bricks! First we define the dimensions
;; of a brick and create a class.
(defparameter *brick-width* (units 2))
(defparameter *brick-height* (units 1.2))
(defclass brick (node)
((color :initform "gray60")
(height :initform *brick-height*)
(width :initform *brick-width*)))
;; Here's how we can add color to bricks when they're being created.
(defmethod initialize-instance :after ((brick brick) &key color)
(when color
(setf (slot-value brick 'color) color)))
;; Finally, the ball should bounce off the bricks and break them.
(defmethod collide ((ball ball) (brick brick))
(with-slots (heading) ball
(destroy brick)
(incf heading (radian-angle 90))))
;; Now we define some useful shorthand functions to refer to the ball and
;; paddle.
(defun ball () (slot-value (current-buffer) 'ball))
(defun paddle () (slot-value (current-buffer) 'paddle))
;; (We'll set up the CURRENT-BUFFER later so that its SLOT-VALUEs
;; are indeed set to right objects.)
;; The player controls a rectangular paddle which can move left or
;; right within the buffer.
(defclass paddle (node)
((direction :initform nil)
(height :initform (units 1))
(width :initform (units 8))
(color :initform "white")))
(defparameter *paddle-speed* 3)
;; Now we define some handy functions to check whether the player is
;; pressing left or right on the keyboard. Numeric keypad is also
;; supported---it's a good idea to check both when using arrows to
;; control your game.
(defun holding-left-arrow ()
(or (keyboard-down-p :kp4)
(keyboard-down-p :left)))
(defun holding-right-arrow ()
(or (keyboard-down-p :kp6)
(keyboard-down-p :right)))
(defun find-joystick-direction ()
(let ((heading (when (left-analog-stick-pressed-p)
(left-analog-stick-heading))))
(when heading
(if (and (> heading (/ pi 2))
(< heading (* 3 (/ pi 2))))
:left
:right))))
(defun find-direction ()
(or (when (plusp (number-of-joysticks))
(find-joystick-direction))
(cond ((holding-left-arrow) :left)
((holding-right-arrow) :right))))
;; In the paddle's UPDATE method, we read the inputs and move the
;; paddle accordingly.
(defmethod update ((paddle paddle))
(with-slots (direction) paddle
(setf direction (find-direction))
(when direction
(move paddle (direction-heading direction) *paddle-speed*))))
;; The paddle should bounce back from the walls, too.
(defmethod collide ((paddle paddle) (wall wall))
(with-slots (direction) paddle
(setf direction (opposite-direction direction))
(move paddle (direction-heading direction) (* *paddle-speed* 2))))
;; The "english" is the directional force applied to the ball because
;; of the player's moving the paddle to the left or right at the
;; moment of collision.
(defmethod english ((paddle paddle))
(with-slots (direction) paddle
(case direction
(:left (direction-heading :upleft))
(:right (direction-heading :upright))
(otherwise (+ (slot-value (ball) 'heading)
(radian-angle 90))))))
;; In the BALL,PADDLE collision method, the english is applied and the
;; ball is bounced away.
(defmethod collide ((ball ball) (paddle paddle))
(with-slots (heading speed) ball
(setf heading (english paddle))
(move ball heading speed)))
;; Now that we have all the pieces of our game world, it's time to put
;; them all together in a buffer. First we have a function to make a
;; wall of a specified height, width, and position.
(defun make-wall (x y width height)
(let ((wall (make-instance 'wall)))
(resize wall width height)
(move-to wall x y)
wall))
;; This function MAKE-BORDER returns a buffer with four walls.
(defun make-border (x y width height)
(let ((left x)
(top y)
(right (+ x width))
(bottom (+ y height)))
(with-new-buffer
;; top wall
(insert (make-wall left top (- right left) (units 1)))
;; bottom wall
(insert (make-wall left bottom (- right left (units -1)) (units 1)))
;; left wall
(insert (make-wall left top (units 1) (- bottom top)))
;; right wall
(insert (make-wall right top (units 1) (- bottom top (units -1))))
;; send it all back
(current-buffer))))
;; Now it's time for pretty rows of colored bricks.
(defparameter *row-colors*
'("dark orchid" "medium orchid" "orchid" "dark orange" "orange" "gold"))
(defun row-color (row)
(nth (mod row (length *row-colors*))
*row-colors*))
(defun make-puzzle ()
(with-new-buffer
(dotimes (row 6)
(dotimes (column 17)
(add-node (current-buffer)
(make-instance 'brick :color (row-color row))
(+ 50 (* column *brick-width*))
(+ 50 (* row *brick-height*)))))))
;; You can see that MAKE-PUZZLE also returns a new buffer. We'll put
;; together these component buffers into the final game board below
;; with a function called PASTE.
;; But first, we need a Buffer subclass for the game board.
(defclass plong (buffer)
((paddle :initform (make-instance 'paddle))
(ball :initform (make-instance 'ball))
(background-color :initform "black")
(width :initform *width*)
(height :initform *height*)))
;; After initializing a new Plong buffer, we set things up so that
;; pressing Control-R causes the game to reset.
(defmethod initialize :after ((plong plong) &key)
(bind-event plong '(:r :control) 'start-game))
;; The START-GAME function builds the game board by inserting the
;; ball and paddle objects, then pasting in the bricks and border.
(defmethod start-game ((plong plong))
(with-slots (ball paddle) plong
(with-buffer plong
(insert ball)
(insert paddle)
(move-to ball 80 280)
(move-to paddle 110 400)
(paste plong (make-border 0 0 (- *width* (units 1)) (- *height* (units 1))))
(paste plong (make-puzzle)))))
;; Now we define the main entry point for the game, the function
;; PLONG. We set up our variables and then invoke WITH-SESSION to
;; start Xelf going.
(defun plong ()
;; Configure the screen dimensions
(setf *screen-height* *height*)
(setf *screen-width* *width*)
;; Allow resizing of window and scaling
(setf *resizable* t)
(setf *scale-output-to-window* t)
(with-session
(open-project :plong)
;; this indexes everything defined with DEFRESOURCE
(index-pending-resources)
(let ((plong (make-instance 'plong)))
;; start the buffer running
(switch-to-buffer plong)
(start-game plong))))
;;; plong.lisp ends here