Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

graphicsDisplay.py 30 KB

You have to be logged in to leave a comment. Sign In
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
  1. # graphicsDisplay.py
  2. # ------------------
  3. # Licensing Information: Please do not distribute or publish solutions to this
  4. # project. You are free to use and extend these projects for educational
  5. # purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
  6. # John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
  7. # For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
  8. from utilities.graphicsUtils import *
  9. import math, time
  10. from utilities.game import Directions
  11. ###########################
  12. # GRAPHICS DISPLAY CODE #
  13. ###########################
  14. # Most code by Dan Klein and John Denero written or rewritten for cs188, UC Berkeley.
  15. # Some code from a Pacman implementation by LiveWires, and used / modified with permission.
  16. DEFAULT_GRID_SIZE = 30.0
  17. INFO_PANE_HEIGHT = 35
  18. BACKGROUND_COLOR = formatColor(0, 0, 0)
  19. WALL_COLOR = formatColor(0.0 / 255.0, 51.0 / 255.0, 255.0 / 255.0)
  20. INFO_PANE_COLOR = formatColor(.4, .4, 0)
  21. SCORE_COLOR = formatColor(.9, .9, .9)
  22. PACMAN_OUTLINE_WIDTH = 2
  23. PACMAN_CAPTURE_OUTLINE_WIDTH = 4
  24. GHOST_COLORS = []
  25. GHOST_COLORS.append(formatColor(.9, 0, 0)) # Red
  26. GHOST_COLORS.append(formatColor(0, .3, .9)) # Blue
  27. GHOST_COLORS.append(formatColor(.98, .41, .07)) # Orange
  28. GHOST_COLORS.append(formatColor(.1, .75, .7)) # Green
  29. GHOST_COLORS.append(formatColor(1.0, 0.6, 0.0)) # Yellow
  30. GHOST_COLORS.append(formatColor(.4, 0.13, 0.91)) # Purple
  31. TEAM_COLORS = GHOST_COLORS[:2]
  32. GHOST_SHAPE = [
  33. (0, 0.3),
  34. (0.25, 0.75),
  35. (0.5, 0.3),
  36. (0.75, 0.75),
  37. (0.75, -0.5),
  38. (0.5, -0.75),
  39. (-0.5, -0.75),
  40. (-0.75, -0.5),
  41. (-0.75, 0.75),
  42. (-0.5, 0.3),
  43. (-0.25, 0.75)
  44. ]
  45. GHOST_SIZE = 0.65
  46. SCARED_COLOR = formatColor(1, 1, 1)
  47. GHOST_VEC_COLORS = map(colorToVector, GHOST_COLORS)
  48. PACMAN_COLOR = formatColor(255.0 / 255.0, 255.0 / 255.0, 61.0 / 255)
  49. PACMAN_SCALE = 0.5
  50. # pacman_speed = 0.25
  51. # Food
  52. FOOD_COLOR = formatColor(1, 1, 1)
  53. FOOD_SIZE = 0.1
  54. # Laser
  55. LASER_COLOR = formatColor(1, 0, 0)
  56. LASER_SIZE = 0.02
  57. # Capsule graphics
  58. CAPSULE_COLOR = formatColor(1, 1, 1)
  59. CAPSULE_SIZE = 0.25
  60. # Drawing walls
  61. WALL_RADIUS = 0.15
  62. class InfoPane:
  63. def __init__(self, layout, gridSize):
  64. self.gridSize = gridSize
  65. self.width = (layout.width) * gridSize
  66. self.base = (layout.height + 1) * gridSize
  67. self.height = INFO_PANE_HEIGHT
  68. self.fontSize = 24
  69. self.textColor = PACMAN_COLOR
  70. self.drawPane()
  71. def toScreen(self, pos, y=None):
  72. """
  73. Translates a point relative from the bottom left of the info pane.
  74. """
  75. if y == None:
  76. x, y = pos
  77. else:
  78. x = pos
  79. x = self.gridSize + x # Margin
  80. y = self.base + y
  81. return x, y
  82. def drawPane(self):
  83. self.scoreText = text(self.toScreen(0, 0), self.textColor, "SCORE: 0", "Times",
  84. self.fontSize, "bold")
  85. def initializeGhostDistances(self, distances):
  86. self.ghostDistanceText = []
  87. size = 20
  88. if self.width < 240:
  89. size = 12
  90. if self.width < 160:
  91. size = 10
  92. for i, d in enumerate(distances):
  93. t = text(self.toScreen(self.width / 2 + self.width / 8 * i, 0), GHOST_COLORS[i + 1], d,
  94. "Times", size, "bold")
  95. self.ghostDistanceText.append(t)
  96. def updateScore(self, score):
  97. changeText(self.scoreText, "SCORE: % 4d" % score)
  98. def setTeam(self, isBlue):
  99. text = "RED TEAM"
  100. if isBlue: text = "BLUE TEAM"
  101. self.teamText = text(self.toScreen(300, 0), self.textColor, text, "Times", self.fontSize, "bold")
  102. def updateGhostDistances(self, distances):
  103. if len(distances) == 0: return
  104. if 'ghostDistanceText' not in dir(self):
  105. self.initializeGhostDistances(distances)
  106. else:
  107. for i, d in enumerate(distances):
  108. changeText(self.ghostDistanceText[i], d)
  109. def drawGhost(self):
  110. pass
  111. def drawPacman(self):
  112. pass
  113. def drawWarning(self):
  114. pass
  115. def clearIcon(self):
  116. pass
  117. def updateMessage(self, message):
  118. pass
  119. def clearMessage(self):
  120. pass
  121. class PacmanGraphics:
  122. def __init__(self, zoom=1.0, frameTime=0.0, capture=False):
  123. self.have_window = 0
  124. self.currentGhostImages = {}
  125. self.pacmanImage = None
  126. self.zoom = zoom
  127. self.gridSize = DEFAULT_GRID_SIZE * zoom
  128. self.capture = capture
  129. self.frameTime = frameTime
  130. def initialize(self, state, isBlue=False):
  131. self.isBlue = isBlue
  132. self.startGraphics(state)
  133. # self.drawDistributions(state)
  134. self.distributionImages = None # Initialized lazily
  135. self.drawStaticObjects(state)
  136. self.drawAgentObjects(state)
  137. # Information
  138. self.previousState = state
  139. def startGraphics(self, state):
  140. self.layout = state.layout
  141. layout = self.layout
  142. self.width = layout.width
  143. self.height = layout.height
  144. self.make_window(self.width, self.height)
  145. self.infoPane = InfoPane(layout, self.gridSize)
  146. self.currentState = layout
  147. def drawDistributions(self, state):
  148. walls = state.layout.walls
  149. dist = []
  150. for x in range(walls.width):
  151. distx = []
  152. dist.append(distx)
  153. for y in range(walls.height):
  154. (screen_x, screen_y) = self.to_screen((x, y))
  155. block = square((screen_x, screen_y),
  156. 0.5 * self.gridSize,
  157. color=BACKGROUND_COLOR,
  158. filled=1, behind=2)
  159. distx.append(block)
  160. self.distributionImages = dist
  161. def drawStaticObjects(self, state):
  162. layout = self.layout
  163. self.drawWalls(layout.walls)
  164. self.food = self.drawFood(layout.food)
  165. self.capsules = self.drawCapsules(layout.capsules)
  166. refresh()
  167. def drawAgentObjects(self, state):
  168. self.agentImages = [] # (agentState, image)
  169. for index, agent in enumerate(state.agentStates):
  170. if agent.isPacman:
  171. image = self.drawPacman(agent, index)
  172. self.agentImages.append((agent, image))
  173. else:
  174. image = self.drawGhost(agent, index)
  175. self.agentImages.append((agent, image))
  176. refresh()
  177. def swapImages(self, agentIndex, newState):
  178. """
  179. Changes an image from a ghost to a pacman or vis versa (for capture)
  180. """
  181. prevState, prevImage = self.agentImages[agentIndex]
  182. for item in prevImage: remove_from_screen(item)
  183. if newState.isPacman:
  184. image = self.drawPacman(newState, agentIndex)
  185. self.agentImages[agentIndex] = (newState, image)
  186. else:
  187. image = self.drawGhost(newState, agentIndex)
  188. self.agentImages[agentIndex] = (newState, image)
  189. refresh()
  190. def update(self, newState):
  191. agentIndex = newState._agentMoved
  192. agentState = newState.agentStates[agentIndex]
  193. if self.agentImages[agentIndex][0].isPacman != agentState.isPacman: self.swapImages(
  194. agentIndex, agentState)
  195. prevState, prevImage = self.agentImages[agentIndex]
  196. if agentState.isPacman:
  197. self.animatePacman(agentState, prevState, prevImage)
  198. else:
  199. self.moveGhost(agentState, agentIndex, prevState, prevImage)
  200. self.agentImages[agentIndex] = (agentState, prevImage)
  201. if newState._foodEaten != None:
  202. self.removeFood(newState._foodEaten, self.food)
  203. if newState._capsuleEaten != None:
  204. self.removeCapsule(newState._capsuleEaten, self.capsules)
  205. self.infoPane.updateScore(newState.score)
  206. if 'ghostDistances' in dir(newState):
  207. self.infoPane.updateGhostDistances(newState.ghostDistances)
  208. def make_window(self, width, height):
  209. grid_width = (width - 1) * self.gridSize
  210. grid_height = (height - 1) * self.gridSize
  211. screen_width = 2 * self.gridSize + grid_width
  212. screen_height = 2 * self.gridSize + grid_height + INFO_PANE_HEIGHT
  213. begin_graphics(screen_width,
  214. screen_height,
  215. BACKGROUND_COLOR,
  216. "67842 INTRODUCTION TO ARTIFICIAL INTELLIGENCE")
  217. def drawPacman(self, pacman, index):
  218. position = self.getPosition(pacman)
  219. screen_point = self.to_screen(position)
  220. endpoints = self.getEndpoints(self.getDirection(pacman))
  221. width = PACMAN_OUTLINE_WIDTH
  222. outlineColor = PACMAN_COLOR
  223. fillColor = PACMAN_COLOR
  224. if self.capture:
  225. outlineColor = TEAM_COLORS[index % 2]
  226. fillColor = GHOST_COLORS[index]
  227. width = PACMAN_CAPTURE_OUTLINE_WIDTH
  228. return [circle(screen_point, PACMAN_SCALE * self.gridSize,
  229. fillColor=fillColor, outlineColor=outlineColor,
  230. endpoints=endpoints,
  231. width=width)]
  232. def getEndpoints(self, direction, position=(0, 0)):
  233. x, y = position
  234. pos = x - int(x) + y - int(y)
  235. width = 30 + 80 * math.sin(math.pi * pos)
  236. delta = width / 2
  237. if (direction == 'West'):
  238. endpoints = (180 + delta, 180 - delta)
  239. elif (direction == 'North'):
  240. endpoints = (90 + delta, 90 - delta)
  241. elif (direction == 'South'):
  242. endpoints = (270 + delta, 270 - delta)
  243. else:
  244. endpoints = (0 + delta, 0 - delta)
  245. return endpoints
  246. def movePacman(self, position, direction, image):
  247. screenPosition = self.to_screen(position)
  248. endpoints = self.getEndpoints(direction, position)
  249. r = PACMAN_SCALE * self.gridSize
  250. moveCircle(image[0], screenPosition, r, endpoints)
  251. refresh()
  252. def animatePacman(self, pacman, prevPacman, image):
  253. if self.frameTime < 0:
  254. print('Press any key to step forward, "q" to play')
  255. keys = wait_for_keys()
  256. if 'q' in keys:
  257. self.frameTime = 0.1
  258. if self.frameTime > 0.01 or self.frameTime < 0:
  259. start = time.time()
  260. fx, fy = self.getPosition(prevPacman)
  261. px, py = self.getPosition(pacman)
  262. frames = 4.0
  263. for i in range(1, int(frames) + 1):
  264. pos = px * i / frames + fx * (frames - i) / frames, py * i / frames + fy * (
  265. frames - i) / frames
  266. self.movePacman(pos, self.getDirection(pacman), image)
  267. refresh()
  268. sleep(abs(self.frameTime) / frames)
  269. else:
  270. self.movePacman(self.getPosition(pacman), self.getDirection(pacman), image)
  271. refresh()
  272. def getGhostColor(self, ghost, ghostIndex):
  273. if ghost.scaredTimer > 0:
  274. return SCARED_COLOR
  275. else:
  276. return GHOST_COLORS[ghostIndex]
  277. def drawGhost(self, ghost, agentIndex):
  278. pos = self.getPosition(ghost)
  279. dir = self.getDirection(ghost)
  280. (screen_x, screen_y) = (self.to_screen(pos))
  281. coords = []
  282. for (x, y) in GHOST_SHAPE:
  283. coords.append((x * self.gridSize * GHOST_SIZE + screen_x,
  284. y * self.gridSize * GHOST_SIZE + screen_y))
  285. colour = self.getGhostColor(ghost, agentIndex)
  286. body = polygon(coords, colour, filled=1)
  287. WHITE = formatColor(1.0, 1.0, 1.0)
  288. BLACK = formatColor(0.0, 0.0, 0.0)
  289. dx = 0
  290. dy = 0
  291. if dir == 'North':
  292. dy = -0.2
  293. if dir == 'South':
  294. dy = 0.2
  295. if dir == 'East':
  296. dx = 0.2
  297. if dir == 'West':
  298. dx = -0.2
  299. leftEye = circle((screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx / 1.5),
  300. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
  301. self.gridSize * GHOST_SIZE * 0.2, WHITE, WHITE)
  302. rightEye = circle((screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx / 1.5),
  303. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
  304. self.gridSize * GHOST_SIZE * 0.2, WHITE, WHITE)
  305. leftPupil = circle((screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx),
  306. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
  307. self.gridSize * GHOST_SIZE * 0.08, BLACK, BLACK)
  308. rightPupil = circle((screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx),
  309. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
  310. self.gridSize * GHOST_SIZE * 0.08, BLACK, BLACK)
  311. ghostImageParts = []
  312. ghostImageParts.append(body)
  313. ghostImageParts.append(leftEye)
  314. ghostImageParts.append(rightEye)
  315. ghostImageParts.append(leftPupil)
  316. ghostImageParts.append(rightPupil)
  317. return ghostImageParts
  318. def moveEyes(self, pos, dir, eyes):
  319. (screen_x, screen_y) = (self.to_screen(pos))
  320. dx = 0
  321. dy = 0
  322. if dir == 'North':
  323. dy = -0.2
  324. if dir == 'South':
  325. dy = 0.2
  326. if dir == 'East':
  327. dx = 0.2
  328. if dir == 'West':
  329. dx = -0.2
  330. moveCircle(eyes[0], (screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx / 1.5),
  331. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
  332. self.gridSize * GHOST_SIZE * 0.2)
  333. moveCircle(eyes[1], (screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx / 1.5),
  334. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
  335. self.gridSize * GHOST_SIZE * 0.2)
  336. moveCircle(eyes[2], (screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx),
  337. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
  338. self.gridSize * GHOST_SIZE * 0.08)
  339. moveCircle(eyes[3], (screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx),
  340. screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
  341. self.gridSize * GHOST_SIZE * 0.08)
  342. def moveGhost(self, ghost, ghostIndex, prevGhost, ghostImageParts):
  343. old_x, old_y = self.to_screen(self.getPosition(prevGhost))
  344. new_x, new_y = self.to_screen(self.getPosition(ghost))
  345. delta = new_x - old_x, new_y - old_y
  346. for ghostImagePart in ghostImageParts:
  347. move_by(ghostImagePart, delta)
  348. refresh()
  349. if ghost.scaredTimer > 0:
  350. color = SCARED_COLOR
  351. else:
  352. color = GHOST_COLORS[ghostIndex]
  353. edit(ghostImageParts[0], ('fill', color), ('outline', color))
  354. self.moveEyes(self.getPosition(ghost), self.getDirection(ghost), ghostImageParts[-4:])
  355. refresh()
  356. def getPosition(self, agentState):
  357. if agentState.configuration == None: return (-1000, -1000)
  358. return agentState.getPosition()
  359. def getDirection(self, agentState):
  360. if agentState.configuration == None: return Directions.STOP
  361. return agentState.configuration.getDirection()
  362. def finish(self):
  363. end_graphics()
  364. def to_screen(self, point):
  365. (x, y) = point
  366. # y = self.height - y
  367. x = (x + 1) * self.gridSize
  368. y = (self.height - y) * self.gridSize
  369. return (x, y)
  370. # Fixes some TK issue with off-center circles
  371. def to_screen2(self, point):
  372. (x, y) = point
  373. # y = self.height - y
  374. x = (x + 1) * self.gridSize
  375. y = (self.height - y) * self.gridSize
  376. return (x, y)
  377. def drawWalls(self, wallMatrix):
  378. wallColor = WALL_COLOR
  379. for xNum, x in enumerate(wallMatrix):
  380. if self.capture and (xNum * 2) < wallMatrix.width: wallColor = TEAM_COLORS[0]
  381. if self.capture and (xNum * 2) >= wallMatrix.width: wallColor = TEAM_COLORS[1]
  382. for yNum, cell in enumerate(x):
  383. if cell: # There's a wall here
  384. pos = (xNum, yNum)
  385. screen = self.to_screen(pos)
  386. screen2 = self.to_screen2(pos)
  387. # draw each quadrant of the square based on adjacent walls
  388. wIsWall = self.isWall(xNum - 1, yNum, wallMatrix)
  389. eIsWall = self.isWall(xNum + 1, yNum, wallMatrix)
  390. nIsWall = self.isWall(xNum, yNum + 1, wallMatrix)
  391. sIsWall = self.isWall(xNum, yNum - 1, wallMatrix)
  392. nwIsWall = self.isWall(xNum - 1, yNum + 1, wallMatrix)
  393. swIsWall = self.isWall(xNum - 1, yNum - 1, wallMatrix)
  394. neIsWall = self.isWall(xNum + 1, yNum + 1, wallMatrix)
  395. seIsWall = self.isWall(xNum + 1, yNum - 1, wallMatrix)
  396. # NE quadrant
  397. if (not nIsWall) and (not eIsWall):
  398. # inner circle
  399. circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor, (0, 91),
  400. 'arc')
  401. if (nIsWall) and (not eIsWall):
  402. # vertical line
  403. line(add(screen, (self.gridSize * WALL_RADIUS, 0)),
  404. add(screen, (self.gridSize * WALL_RADIUS, self.gridSize * (-0.5) - 1)),
  405. wallColor)
  406. if (not nIsWall) and (eIsWall):
  407. # horizontal line
  408. line(add(screen, (0, self.gridSize * (-1) * WALL_RADIUS)), add(screen, (
  409. self.gridSize * 0.5 + 1, self.gridSize * (-1) * WALL_RADIUS)), wallColor)
  410. if (nIsWall) and (eIsWall) and (not neIsWall):
  411. # outer circle
  412. circle(add(screen2, (
  413. self.gridSize * 2 * WALL_RADIUS, self.gridSize * (-2) * WALL_RADIUS)),
  414. WALL_RADIUS * self.gridSize - 1, wallColor, wallColor, (180, 271),
  415. 'arc')
  416. line(add(screen, (
  417. self.gridSize * 2 * WALL_RADIUS - 1, self.gridSize * (-1) * WALL_RADIUS)),
  418. add(screen,
  419. (self.gridSize * 0.5 + 1, self.gridSize * (-1) * WALL_RADIUS)),
  420. wallColor)
  421. line(add(screen, (
  422. self.gridSize * WALL_RADIUS, self.gridSize * (-2) * WALL_RADIUS + 1)),
  423. add(screen, (self.gridSize * WALL_RADIUS, self.gridSize * (-0.5))),
  424. wallColor)
  425. # NW quadrant
  426. if (not nIsWall) and (not wIsWall):
  427. # inner circle
  428. circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor,
  429. (90, 181), 'arc')
  430. if (nIsWall) and (not wIsWall):
  431. # vertical line
  432. line(add(screen, (self.gridSize * (-1) * WALL_RADIUS, 0)), add(screen, (
  433. self.gridSize * (-1) * WALL_RADIUS, self.gridSize * (-0.5) - 1)), wallColor)
  434. if (not nIsWall) and (wIsWall):
  435. # horizontal line
  436. line(add(screen, (0, self.gridSize * (-1) * WALL_RADIUS)), add(screen, (
  437. self.gridSize * (-0.5) - 1, self.gridSize * (-1) * WALL_RADIUS)), wallColor)
  438. if (nIsWall) and (wIsWall) and (not nwIsWall):
  439. # outer circle
  440. circle(add(screen2, (
  441. self.gridSize * (-2) * WALL_RADIUS, self.gridSize * (-2) * WALL_RADIUS)),
  442. WALL_RADIUS * self.gridSize - 1, wallColor, wallColor, (270, 361),
  443. 'arc')
  444. line(add(screen, (self.gridSize * (-2) * WALL_RADIUS + 1,
  445. self.gridSize * (-1) * WALL_RADIUS)), add(screen, (
  446. self.gridSize * (-0.5), self.gridSize * (-1) * WALL_RADIUS)), wallColor)
  447. line(add(screen, (self.gridSize * (-1) * WALL_RADIUS,
  448. self.gridSize * (-2) * WALL_RADIUS + 1)), add(screen, (
  449. self.gridSize * (-1) * WALL_RADIUS, self.gridSize * (-0.5))), wallColor)
  450. # SE quadrant
  451. if (not sIsWall) and (not eIsWall):
  452. # inner circle
  453. circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor,
  454. (270, 361), 'arc')
  455. if (sIsWall) and (not eIsWall):
  456. # vertical line
  457. line(add(screen, (self.gridSize * WALL_RADIUS, 0)),
  458. add(screen, (self.gridSize * WALL_RADIUS, self.gridSize * (0.5) + 1)),
  459. wallColor)
  460. if (not sIsWall) and (eIsWall):
  461. # horizontal line
  462. line(add(screen, (0, self.gridSize * (1) * WALL_RADIUS)), add(screen, (
  463. self.gridSize * 0.5 + 1, self.gridSize * (1) * WALL_RADIUS)), wallColor)
  464. if (sIsWall) and (eIsWall) and (not seIsWall):
  465. # outer circle
  466. circle(add(screen2, (
  467. self.gridSize * 2 * WALL_RADIUS, self.gridSize * (2) * WALL_RADIUS)),
  468. WALL_RADIUS * self.gridSize - 1, wallColor, wallColor, (90, 181),
  469. 'arc')
  470. line(add(screen, (
  471. self.gridSize * 2 * WALL_RADIUS - 1, self.gridSize * (1) * WALL_RADIUS)),
  472. add(screen, (self.gridSize * 0.5, self.gridSize * (1) * WALL_RADIUS)),
  473. wallColor)
  474. line(add(screen, (
  475. self.gridSize * WALL_RADIUS, self.gridSize * (2) * WALL_RADIUS - 1)),
  476. add(screen, (self.gridSize * WALL_RADIUS, self.gridSize * (0.5))),
  477. wallColor)
  478. # SW quadrant
  479. if (not sIsWall) and (not wIsWall):
  480. # inner circle
  481. circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor,
  482. (180, 271), 'arc')
  483. if (sIsWall) and (not wIsWall):
  484. # vertical line
  485. line(add(screen, (self.gridSize * (-1) * WALL_RADIUS, 0)), add(screen, (
  486. self.gridSize * (-1) * WALL_RADIUS, self.gridSize * (0.5) + 1)), wallColor)
  487. if (not sIsWall) and (wIsWall):
  488. # horizontal line
  489. line(add(screen, (0, self.gridSize * (1) * WALL_RADIUS)), add(screen, (
  490. self.gridSize * (-0.5) - 1, self.gridSize * (1) * WALL_RADIUS)), wallColor)
  491. if (sIsWall) and (wIsWall) and (not swIsWall):
  492. # outer circle
  493. circle(add(screen2, (
  494. self.gridSize * (-2) * WALL_RADIUS, self.gridSize * (2) * WALL_RADIUS)),
  495. WALL_RADIUS * self.gridSize - 1, wallColor, wallColor, (0, 91),
  496. 'arc')
  497. line(add(screen, (
  498. self.gridSize * (-2) * WALL_RADIUS + 1, self.gridSize * (1) * WALL_RADIUS)),
  499. add(screen,
  500. (self.gridSize * (-0.5), self.gridSize * (1) * WALL_RADIUS)),
  501. wallColor)
  502. line(add(screen, (
  503. self.gridSize * (-1) * WALL_RADIUS, self.gridSize * (2) * WALL_RADIUS - 1)),
  504. add(screen,
  505. (self.gridSize * (-1) * WALL_RADIUS, self.gridSize * (0.5))),
  506. wallColor)
  507. def isWall(self, x, y, walls):
  508. if x < 0 or y < 0:
  509. return False
  510. if x >= walls.width or y >= walls.height:
  511. return False
  512. return walls[x][y]
  513. def drawFood(self, foodMatrix):
  514. foodImages = []
  515. color = FOOD_COLOR
  516. for xNum, x in enumerate(foodMatrix):
  517. if self.capture and (xNum * 2) <= foodMatrix.width: color = TEAM_COLORS[0]
  518. if self.capture and (xNum * 2) > foodMatrix.width: color = TEAM_COLORS[1]
  519. imageRow = []
  520. foodImages.append(imageRow)
  521. for yNum, cell in enumerate(x):
  522. if cell: # There's food here
  523. screen = self.to_screen((xNum, yNum))
  524. dot = circle(screen,
  525. FOOD_SIZE * self.gridSize,
  526. outlineColor=color, fillColor=color,
  527. width=1)
  528. imageRow.append(dot)
  529. else:
  530. imageRow.append(None)
  531. return foodImages
  532. def drawCapsules(self, capsules):
  533. capsuleImages = {}
  534. for capsule in capsules:
  535. (screen_x, screen_y) = self.to_screen(capsule)
  536. dot = circle((screen_x, screen_y),
  537. CAPSULE_SIZE * self.gridSize,
  538. outlineColor=CAPSULE_COLOR,
  539. fillColor=CAPSULE_COLOR,
  540. width=1)
  541. capsuleImages[capsule] = dot
  542. return capsuleImages
  543. def removeFood(self, cell, foodImages):
  544. x, y = cell
  545. remove_from_screen(foodImages[x][y])
  546. def removeCapsule(self, cell, capsuleImages):
  547. x, y = cell
  548. remove_from_screen(capsuleImages[(x, y)])
  549. def drawExpandedCells(self, cells):
  550. """
  551. Draws an overlay of expanded grid positions for search agents
  552. """
  553. n = float(len(cells))
  554. baseColor = [1.0, 0.0, 0.0]
  555. self.clearExpandedCells()
  556. self.expandedCells = []
  557. for k, cell in enumerate(cells):
  558. screenPos = self.to_screen(cell)
  559. cellColor = formatColor(*[(n - k) * c * .5 / n + .25 for c in baseColor])
  560. block = square(screenPos,
  561. 0.5 * self.gridSize,
  562. color=cellColor,
  563. filled=1, behind=2)
  564. self.expandedCells.append(block)
  565. if self.frameTime < 0:
  566. refresh()
  567. def clearExpandedCells(self):
  568. if 'expandedCells' in dir(self) and len(self.expandedCells) > 0:
  569. for cell in self.expandedCells:
  570. remove_from_screen(cell)
  571. def updateDistributions(self, distributions):
  572. """
  573. Draws an agent's belief distributions
  574. """
  575. if self.distributionImages == None:
  576. self.drawDistributions(self.previousState)
  577. for x in range(len(self.distributionImages)):
  578. for y in range(len(self.distributionImages[0])):
  579. image = self.distributionImages[x][y]
  580. weights = [dist[(x, y)] for dist in distributions]
  581. if sum(weights) != 0:
  582. pass
  583. # Fog of war
  584. color = [0.0, 0.0, 0.0]
  585. colors = GHOST_VEC_COLORS[1:] # With Pacman
  586. if self.capture: colors = GHOST_VEC_COLORS
  587. for weight, gcolor in zip(weights, colors):
  588. color = [min(1.0, c + 0.95 * g * weight ** .3) for c, g in zip(color, gcolor)]
  589. changeColor(image, formatColor(*color))
  590. refresh()
  591. class FirstPersonPacmanGraphics(PacmanGraphics):
  592. def __init__(self, zoom=1.0, showGhosts=True, capture=False, frameTime=0):
  593. PacmanGraphics.__init__(self, zoom, frameTime=frameTime)
  594. self.showGhosts = showGhosts
  595. self.capture = capture
  596. def initialize(self, state, isBlue=False):
  597. self.isBlue = isBlue
  598. PacmanGraphics.startGraphics(self, state)
  599. # Initialize distribution images
  600. walls = state.layout.walls
  601. dist = []
  602. self.layout = state.layout
  603. # Draw the rest
  604. self.distributionImages = None # initialize lazily
  605. self.drawStaticObjects(state)
  606. self.drawAgentObjects(state)
  607. # Information
  608. self.previousState = state
  609. def lookAhead(self, config, state):
  610. if config.getDirection() == 'Stop':
  611. return
  612. else:
  613. pass
  614. # Draw relevant ghosts
  615. allGhosts = state.getGhostStates()
  616. visibleGhosts = state.getVisibleGhosts()
  617. for i, ghost in enumerate(allGhosts):
  618. if ghost in visibleGhosts:
  619. self.drawGhost(ghost, i)
  620. else:
  621. self.currentGhostImages[i] = None
  622. def getGhostColor(self, ghost, ghostIndex):
  623. return GHOST_COLORS[ghostIndex]
  624. def getPosition(self, ghostState):
  625. if not self.showGhosts and not ghostState.isPacman and ghostState.getPosition()[1] > 1:
  626. return (-1000, -1000)
  627. else:
  628. return PacmanGraphics.getPosition(self, ghostState)
  629. def add(x, y):
  630. return (x[0] + y[0], x[1] + y[1])
  631. # Saving graphical output
  632. # -----------------------
  633. # Note: to make an animated gif from this postscript output, try the command:
  634. # convert -delay 7 -loop 1 -compress lzw -layers optimize frame* out.gif
  635. # convert is part of imagemagick (freeware)
  636. SAVE_POSTSCRIPT = False
  637. POSTSCRIPT_OUTPUT_DIR = 'frames'
  638. FRAME_NUMBER = 0
  639. import os
  640. def saveFrame():
  641. """
  642. Saves the current graphical output as a postscript file
  643. """
  644. global SAVE_POSTSCRIPT, FRAME_NUMBER, POSTSCRIPT_OUTPUT_DIR
  645. if not SAVE_POSTSCRIPT: return
  646. if not os.path.exists(POSTSCRIPT_OUTPUT_DIR): os.mkdir(POSTSCRIPT_OUTPUT_DIR)
  647. name = os.path.join(POSTSCRIPT_OUTPUT_DIR, 'frame_%08d.ps' % FRAME_NUMBER)
  648. FRAME_NUMBER += 1
  649. writePostscript(name) # writes the current canvas
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...