forked from ramr/pacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.coffee
executable file
·305 lines (274 loc) · 6.57 KB
/
game.coffee
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
socket = io.connect(document.domain)
socket.on 'connect', ->
console.log 'socket connected'
canvas = document.getElementById 'board'
ctx = canvas.getContext '2d'
blinky = new Image()
pacman = new Image()
pinky = new Image()
clyde = new Image()
clyde.src = 'clyde.png'
clyde.X = -100
clyde.Y = -100
clyde.name = 'clyde'
inky = new Image()
maze = new Image()
maze.src = 'board.png'
pacman.src = 'pacman.png'
pacman.X = -100
pacman.Y = -200
inky.src = 'inky.png'
inky.X = -100
inky.Y = -100
inky.name = 'inky'
pinky.src = 'pinky.png'
pinky.X = -100
pinky.Y = -100
pinky.name = 'pinky'
console.log pinky.X
blinky.src = 'blinky.gif'
blinky.X = -100
blinky.Y = -100
blinky.name = 'blinky'
direction = 'up'
ghost = null
controllingPacman = false
pacLocInterval = false
randDirInterval = false
movePacInterval = false
lastmsgts = new Date().getTime()
showMessage = (msg)->
banner = $('#message')
banner.html(msg).show()
setTimeout (->banner.hide()), 5000
unschedule = (intrvl)->
if intrvl != false
intrvl = window.clearInterval(intrvl)
return intrvl
schedule = (intrvl, code, freq)->
unschedule(intrvl)
return intrvl = setInterval(code, freq)
randomlyMovePacman = ->
showMessage("Will randomly move pacman around!")
controllingPacman = true
resetPac()
pacLocInterval = schedule(pacLocInterval, pacLoc, 20)
randDirInterval = schedule(randDirInterval, randDir, 500)
return movePacInterval = schedule(movePacInterval, movePac, 20)
losePacman = ->
controllingPacman = true
pacLocInterval = unschedule(pacLocInterval)
randDirInterval = unschedule(randDirInterval)
movePacInterval = unschedule(movePacInterval)
showMessage("Inactive - no longer randomly moving pacman!")
reset = (ghost)->
switch ghost.name
when 'blinky'
ghost.X = 430
when 'pinky'
ghost.X = 400
when 'inky'
ghost.X = 460
when 'clyde'
ghost.X = 490
ghost.Y = 220
# console.log "sending location %s: %d,%d", ghost.name, ghost.X, ghost.Y
socket.emit 'pacman-message',
type: 'location'
ghost: ghost.name
x: ghost.X
y: ghost.Y
lastmsgts = new Date().getTime()
showMessage("Use the arrow keys to move your ghost " + ghost.name +
" around")
socket.on 'pacman-message', (message)->
#console.log message
switch message.type
when 'lose-pacman'
losePacman()
when 'pacman'
randomlyMovePacman()
when 'location'
sprite = eval message.sprite
sprite.X = message.x
sprite.Y = message.y
when 'ghost'
ghost = eval(message.name)
console.log ghost
reset(ghost)
when 'win'
reset(ghost)
if controllingPacman
resetPac()
showMessage(message.ghost + ' wins!')
when 'full'
full = $('#full')
full.html('Sorry, all ghosts are in use<br>Enjoy the show').show()
when 'newghost'
lastmsgts = new Date().getTime()
socket.emit 'pacman-message',
type: 'location'
ghost: ghost.name
x: ghost.X
y: ghost.Y
resetPac = ->
pacman.X = 500
pacman.Y = 300
newpac = ->
console.log 'newpac!'
foo = null
blah = null
while foo != 1
width = Math.floor(Math.random()*1099)+1
height = Math.floor(Math.random()*373)+1
#console.log 'width: ' + width
#console.log 'height: ' + height
imgd = ctx.getImageData width, height, 30, 30
pix = imgd.data
i = 0
for val in pix
if pix[i] == 0
foo = 1
i += 4
pacman.X = width
pacman.Y = height
return [width, height]
newGhost = (ghost)->
foo = null
blah = null
while foo != 1
width = Math.floor(Math.random()*1099)+1
height = Math.floor(Math.random()*373)+1
imgd = ctx.getImageData width, height, 30, 30
pix = imgd.data
i = 0
for val in pix
if pix[i] == 0
foo = 1
i += 4
ghost.X = width
ghost.Y = height
return [width, height]
sendGhostCoords = ->
if ! ghost
return ghost
lastmsgts = new Date().getTime()
socket.emit 'pacman-message',
type: 'location'
ghost: ghost.name
x: ghost.X
y: ghost.Y
mover = (event, ghost)->
#event.preventDefault()
if ! ghost
return 0
switch event.keyCode
when 39
if checkCollision('right', ghost) != true
ghost.X += 10
when 38
if checkCollision('up', ghost) != true
ghost.Y -= 10
when 37
if checkCollision('left', ghost) != true
ghost.X -= 10
when 40
if checkCollision('down', ghost) != true
ghost.Y += 10
when 87
ghost.Y -= 10
when 83
ghost.Y += 10
when 68
ghost.X += 10
when 65
ghost.X -= 10
#console.log [ghost.X, ghost.Y]
sendGhostCoords()
randDir = ->
switch Math.floor(Math.random()*4)
when 0
direction = 'left'
when 1
direction = 'right'
when 2
direction = 'up'
when 3
direction = 'down'
movePac = ->
if checkCollision(direction, pacman) == true
randDir()
else
switch direction
when 'left'
pacman.X -= 10
when 'right'
pacman.X += 10
when 'up'
pacman.Y -= 10
when 'down'
pacman.Y += 10
pacLoc = ->
#console.log socket
#console.log pacman.X
lastmsgts = new Date().getTime()
socket.emit 'pacman-message',
type: 'location'
ghost: 'pacman'
x: pacman.X
y: pacman.Y
ghosts = [blinky,pinky,inky,clyde]
checkWin = ->
if Math.abs(ghost.X-pacman.X) <35 && Math.abs(pacman.Y - ghost.Y)< 35
lastmsgts = new Date().getTime()
socket.emit 'pacman-message',
type: 'win'
clear = ->
canvas.width = canvas.width
imagesLoaded = ->
((maze.width > 0) && (pacman.width > 0) && (blinky.width > 0) && \
(pinky.width > 0) && (clyde.width > 0) && (inky.width > 0))
draw = ->
clear()
if ! imagesLoaded()
return
checkWin() if ghost?
ctx.drawImage maze, 0, 0
ctx.drawImage pacman, pacman.X, pacman.Y, 30, 30
ctx.drawImage blinky, blinky.X, blinky.Y, 30, 30
ctx.drawImage pinky, pinky.X, pinky.Y, 30, 30
ctx.drawImage inky, inky.X, inky.Y, 30, 30
ctx.drawImage clyde, clyde.X, clyde.Y, 30, 30
checkCollision = (direction, sprite)->
switch direction
when 'up'
x = sprite.X
y = sprite.Y - 10
when 'down'
x = sprite.X
y = sprite.Y + 30
when 'left'
x = sprite.X - 10
y = sprite.Y
when 'right'
x = sprite.X + 30
y = sprite.Y
imgd = ctx.getImageData x, y, 10, 10
pix = imgd.data
i = 0
for val in pix
if pix[i] == 0
return true
i += 4
return false
ensureChannel = ->
if (new Date().getTime() - lastmsgts) < 3000
return 0
# Force client to randomly move and send pacman info.
if controllingPacman
return randomlyMovePacman()
return sendGhostCoordinates()
setInterval draw, 20
setInterval ensureChannel, 5000
i = Math.floor(Math.random()*2)
window.addEventListener('keydown', ((event)->mover(event,ghost)), false)