getting right starter files
This commit is contained in:
+202
-143
@@ -4,7 +4,7 @@
|
||||
# educational purposes provided that (1) you do not distribute or publish
|
||||
# solutions, (2) you retain this notice, and (3) you provide clear
|
||||
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
|
||||
#
|
||||
#
|
||||
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
|
||||
# The core projects and autograders were primarily created by John DeNero
|
||||
# ([email protected]) and Dan Klein ([email protected]).
|
||||
@@ -13,7 +13,8 @@
|
||||
|
||||
|
||||
from graphicsUtils import *
|
||||
import math, time
|
||||
import math
|
||||
import time
|
||||
from game import Directions
|
||||
|
||||
###########################
|
||||
@@ -25,60 +26,61 @@ from game import Directions
|
||||
|
||||
DEFAULT_GRID_SIZE = 30.0
|
||||
INFO_PANE_HEIGHT = 35
|
||||
BACKGROUND_COLOR = formatColor(0,0,0)
|
||||
BACKGROUND_COLOR = formatColor(0, 0, 0)
|
||||
WALL_COLOR = formatColor(0.0/255.0, 51.0/255.0, 255.0/255.0)
|
||||
INFO_PANE_COLOR = formatColor(.4,.4,0)
|
||||
INFO_PANE_COLOR = formatColor(.4, .4, 0)
|
||||
SCORE_COLOR = formatColor(.9, .9, .9)
|
||||
PACMAN_OUTLINE_WIDTH = 2
|
||||
PACMAN_CAPTURE_OUTLINE_WIDTH = 4
|
||||
|
||||
GHOST_COLORS = []
|
||||
GHOST_COLORS.append(formatColor(.9,0,0)) # Red
|
||||
GHOST_COLORS.append(formatColor(0,.3,.9)) # Blue
|
||||
GHOST_COLORS.append(formatColor(.98,.41,.07)) # Orange
|
||||
GHOST_COLORS.append(formatColor(.1,.75,.7)) # Green
|
||||
GHOST_COLORS.append(formatColor(1.0,0.6,0.0)) # Yellow
|
||||
GHOST_COLORS.append(formatColor(.4,0.13,0.91)) # Purple
|
||||
GHOST_COLORS.append(formatColor(.9, 0, 0)) # Red
|
||||
GHOST_COLORS.append(formatColor(0, .3, .9)) # Blue
|
||||
GHOST_COLORS.append(formatColor(.98, .41, .07)) # Orange
|
||||
GHOST_COLORS.append(formatColor(.1, .75, .7)) # Green
|
||||
GHOST_COLORS.append(formatColor(1.0, 0.6, 0.0)) # Yellow
|
||||
GHOST_COLORS.append(formatColor(.4, 0.13, 0.91)) # Purple
|
||||
|
||||
TEAM_COLORS = GHOST_COLORS[:2]
|
||||
|
||||
GHOST_SHAPE = [
|
||||
( 0, 0.3 ),
|
||||
( 0.25, 0.75 ),
|
||||
( 0.5, 0.3 ),
|
||||
( 0.75, 0.75 ),
|
||||
( 0.75, -0.5 ),
|
||||
( 0.5, -0.75 ),
|
||||
(-0.5, -0.75 ),
|
||||
(-0.75, -0.5 ),
|
||||
(-0.75, 0.75 ),
|
||||
(-0.5, 0.3 ),
|
||||
(-0.25, 0.75 )
|
||||
]
|
||||
(0, 0.3),
|
||||
(0.25, 0.75),
|
||||
(0.5, 0.3),
|
||||
(0.75, 0.75),
|
||||
(0.75, -0.5),
|
||||
(0.5, -0.75),
|
||||
(-0.5, -0.75),
|
||||
(-0.75, -0.5),
|
||||
(-0.75, 0.75),
|
||||
(-0.5, 0.3),
|
||||
(-0.25, 0.75)
|
||||
]
|
||||
GHOST_SIZE = 0.65
|
||||
SCARED_COLOR = formatColor(1,1,1)
|
||||
SCARED_COLOR = formatColor(1, 1, 1)
|
||||
|
||||
GHOST_VEC_COLORS = [colorToVector(c) for c in GHOST_COLORS]
|
||||
GHOST_VEC_COLORS = list(map(colorToVector, GHOST_COLORS))
|
||||
|
||||
PACMAN_COLOR = formatColor(255.0/255.0,255.0/255.0,61.0/255)
|
||||
PACMAN_COLOR = formatColor(255.0/255.0, 255.0/255.0, 61.0/255)
|
||||
PACMAN_SCALE = 0.5
|
||||
#pacman_speed = 0.25
|
||||
|
||||
# Food
|
||||
FOOD_COLOR = formatColor(1,1,1)
|
||||
FOOD_COLOR = formatColor(1, 1, 1)
|
||||
FOOD_SIZE = 0.1
|
||||
|
||||
# Laser
|
||||
LASER_COLOR = formatColor(1,0,0)
|
||||
LASER_COLOR = formatColor(1, 0, 0)
|
||||
LASER_SIZE = 0.02
|
||||
|
||||
# Capsule graphics
|
||||
CAPSULE_COLOR = formatColor(1,1,1)
|
||||
CAPSULE_COLOR = formatColor(1, 1, 1)
|
||||
CAPSULE_SIZE = 0.25
|
||||
|
||||
# Drawing walls
|
||||
WALL_RADIUS = 0.15
|
||||
|
||||
|
||||
class InfoPane:
|
||||
def __init__(self, layout, gridSize):
|
||||
self.gridSize = gridSize
|
||||
@@ -89,21 +91,22 @@ class InfoPane:
|
||||
self.textColor = PACMAN_COLOR
|
||||
self.drawPane()
|
||||
|
||||
def toScreen(self, pos, y = None):
|
||||
def toScreen(self, pos, y=None):
|
||||
"""
|
||||
Translates a point relative from the bottom left of the info pane.
|
||||
"""
|
||||
if y == None:
|
||||
x,y = pos
|
||||
x, y = pos
|
||||
else:
|
||||
x = pos
|
||||
|
||||
x = self.gridSize + x # Margin
|
||||
x = self.gridSize + x # Margin
|
||||
y = self.base + y
|
||||
return x,y
|
||||
return x, y
|
||||
|
||||
def drawPane(self):
|
||||
self.scoreText = text( self.toScreen(0, 0 ), self.textColor, "SCORE: 0", "Times", self.fontSize, "bold")
|
||||
self.scoreText = text(self.toScreen(
|
||||
0, 0), self.textColor, "SCORE: 0", "Times", self.fontSize, "bold")
|
||||
|
||||
def initializeGhostDistances(self, distances):
|
||||
self.ghostDistanceText = []
|
||||
@@ -115,7 +118,8 @@ class InfoPane:
|
||||
size = 10
|
||||
|
||||
for i, d in enumerate(distances):
|
||||
t = text( self.toScreen(self.width//2 + self.width//8 * i, 0), GHOST_COLORS[i+1], d, "Times", size, "bold")
|
||||
t = text(self.toScreen(self.width/2 + self.width/8 * i, 0),
|
||||
GHOST_COLORS[i+1], d, "Times", size, "bold")
|
||||
self.ghostDistanceText.append(t)
|
||||
|
||||
def updateScore(self, score):
|
||||
@@ -123,12 +127,16 @@ class InfoPane:
|
||||
|
||||
def setTeam(self, isBlue):
|
||||
text = "RED TEAM"
|
||||
if isBlue: text = "BLUE TEAM"
|
||||
self.teamText = text( self.toScreen(300, 0 ), self.textColor, text, "Times", self.fontSize, "bold")
|
||||
if isBlue:
|
||||
text = "BLUE TEAM"
|
||||
self.teamText = text(self.toScreen(
|
||||
300, 0), self.textColor, text, "Times", self.fontSize, "bold")
|
||||
|
||||
def updateGhostDistances(self, distances):
|
||||
if len(distances) == 0: return
|
||||
if 'ghostDistanceText' not in dir(self): self.initializeGhostDistances(distances)
|
||||
if len(distances) == 0:
|
||||
return
|
||||
if 'ghostDistanceText' not in dir(self):
|
||||
self.initializeGhostDistances(distances)
|
||||
else:
|
||||
for i, d in enumerate(distances):
|
||||
changeText(self.ghostDistanceText[i], d)
|
||||
@@ -165,7 +173,7 @@ class PacmanGraphics:
|
||||
def checkNullDisplay(self):
|
||||
return False
|
||||
|
||||
def initialize(self, state, isBlue = False):
|
||||
def initialize(self, state, isBlue=False):
|
||||
self.isBlue = isBlue
|
||||
self.startGraphics(state)
|
||||
|
||||
@@ -193,11 +201,11 @@ class PacmanGraphics:
|
||||
distx = []
|
||||
dist.append(distx)
|
||||
for y in range(walls.height):
|
||||
( screen_x, screen_y ) = self.to_screen( (x, y) )
|
||||
block = square( (screen_x, screen_y),
|
||||
0.5 * self.gridSize,
|
||||
color = BACKGROUND_COLOR,
|
||||
filled = 1, behind=2)
|
||||
(screen_x, screen_y) = self.to_screen((x, y))
|
||||
block = square((screen_x, screen_y),
|
||||
0.5 * self.gridSize,
|
||||
color=BACKGROUND_COLOR,
|
||||
filled=1, behind=2)
|
||||
distx.append(block)
|
||||
self.distributionImages = dist
|
||||
|
||||
@@ -209,14 +217,14 @@ class PacmanGraphics:
|
||||
refresh()
|
||||
|
||||
def drawAgentObjects(self, state):
|
||||
self.agentImages = [] # (agentState, image)
|
||||
self.agentImages = [] # (agentState, image)
|
||||
for index, agent in enumerate(state.agentStates):
|
||||
if agent.isPacman:
|
||||
image = self.drawPacman(agent, index)
|
||||
self.agentImages.append( (agent, image) )
|
||||
self.agentImages.append((agent, image))
|
||||
else:
|
||||
image = self.drawGhost(agent, index)
|
||||
self.agentImages.append( (agent, image) )
|
||||
self.agentImages.append((agent, image))
|
||||
refresh()
|
||||
|
||||
def swapImages(self, agentIndex, newState):
|
||||
@@ -224,20 +232,22 @@ class PacmanGraphics:
|
||||
Changes an image from a ghost to a pacman or vis versa (for capture)
|
||||
"""
|
||||
prevState, prevImage = self.agentImages[agentIndex]
|
||||
for item in prevImage: remove_from_screen(item)
|
||||
for item in prevImage:
|
||||
remove_from_screen(item)
|
||||
if newState.isPacman:
|
||||
image = self.drawPacman(newState, agentIndex)
|
||||
self.agentImages[agentIndex] = (newState, image )
|
||||
self.agentImages[agentIndex] = (newState, image)
|
||||
else:
|
||||
image = self.drawGhost(newState, agentIndex)
|
||||
self.agentImages[agentIndex] = (newState, image )
|
||||
self.agentImages[agentIndex] = (newState, image)
|
||||
refresh()
|
||||
|
||||
def update(self, newState):
|
||||
agentIndex = newState._agentMoved
|
||||
agentState = newState.agentStates[agentIndex]
|
||||
|
||||
if self.agentImages[agentIndex][0].isPacman != agentState.isPacman: self.swapImages(agentIndex, agentState)
|
||||
if self.agentImages[agentIndex][0].isPacman != agentState.isPacman:
|
||||
self.swapImages(agentIndex, agentState)
|
||||
prevState, prevImage = self.agentImages[agentIndex]
|
||||
if agentState.isPacman:
|
||||
self.animatePacman(agentState, prevState, prevImage)
|
||||
@@ -279,14 +289,14 @@ class PacmanGraphics:
|
||||
width = PACMAN_CAPTURE_OUTLINE_WIDTH
|
||||
|
||||
return [circle(screen_point, PACMAN_SCALE * self.gridSize,
|
||||
fillColor = fillColor, outlineColor = outlineColor,
|
||||
endpoints = endpoints,
|
||||
width = width)]
|
||||
fillColor=fillColor, outlineColor=outlineColor,
|
||||
endpoints=endpoints,
|
||||
width=width)]
|
||||
|
||||
def getEndpoints(self, direction, position=(0,0)):
|
||||
def getEndpoints(self, direction, position=(0, 0)):
|
||||
x, y = position
|
||||
pos = x - int(x) + y - int(y)
|
||||
width = 30 + 80 * math.sin(math.pi* pos)
|
||||
width = 30 + 80 * math.sin(math.pi * pos)
|
||||
|
||||
delta = width / 2
|
||||
if (direction == 'West'):
|
||||
@@ -301,7 +311,7 @@ class PacmanGraphics:
|
||||
|
||||
def movePacman(self, position, direction, image):
|
||||
screenPosition = self.to_screen(position)
|
||||
endpoints = self.getEndpoints( direction, position )
|
||||
endpoints = self.getEndpoints(direction, position)
|
||||
r = PACMAN_SCALE * self.gridSize
|
||||
moveCircle(image[0], screenPosition, r, endpoints)
|
||||
refresh()
|
||||
@@ -317,13 +327,15 @@ class PacmanGraphics:
|
||||
fx, fy = self.getPosition(prevPacman)
|
||||
px, py = self.getPosition(pacman)
|
||||
frames = 4.0
|
||||
for i in range(1,int(frames) + 1):
|
||||
pos = px*i/frames + fx*(frames-i)/frames, py*i/frames + fy*(frames-i)/frames
|
||||
for i in range(1, int(frames) + 1):
|
||||
pos = px*i/frames + fx * \
|
||||
(frames-i)/frames, py*i/frames + fy*(frames-i)/frames
|
||||
self.movePacman(pos, self.getDirection(pacman), image)
|
||||
refresh()
|
||||
sleep(abs(self.frameTime) / frames)
|
||||
else:
|
||||
self.movePacman(self.getPosition(pacman), self.getDirection(pacman), image)
|
||||
self.movePacman(self.getPosition(pacman),
|
||||
self.getDirection(pacman), image)
|
||||
refresh()
|
||||
|
||||
def getGhostColor(self, ghost, ghostIndex):
|
||||
@@ -335,13 +347,14 @@ class PacmanGraphics:
|
||||
def drawGhost(self, ghost, agentIndex):
|
||||
pos = self.getPosition(ghost)
|
||||
dir = self.getDirection(ghost)
|
||||
(screen_x, screen_y) = (self.to_screen(pos) )
|
||||
(screen_x, screen_y) = (self.to_screen(pos))
|
||||
coords = []
|
||||
for (x, y) in GHOST_SHAPE:
|
||||
coords.append((x*self.gridSize*GHOST_SIZE + screen_x, y*self.gridSize*GHOST_SIZE + screen_y))
|
||||
coords.append((x*self.gridSize*GHOST_SIZE + screen_x,
|
||||
y*self.gridSize*GHOST_SIZE + screen_y))
|
||||
|
||||
colour = self.getGhostColor(ghost, agentIndex)
|
||||
body = polygon(coords, colour, filled = 1)
|
||||
body = polygon(coords, colour, filled=1)
|
||||
WHITE = formatColor(1.0, 1.0, 1.0)
|
||||
BLACK = formatColor(0.0, 0.0, 0.0)
|
||||
|
||||
@@ -355,10 +368,14 @@ class PacmanGraphics:
|
||||
dx = 0.2
|
||||
if dir == 'West':
|
||||
dx = -0.2
|
||||
leftEye = circle((screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx/1.5), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2, WHITE, WHITE)
|
||||
rightEye = circle((screen_x+self.gridSize*GHOST_SIZE*(0.3+dx/1.5), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2, WHITE, WHITE)
|
||||
leftPupil = circle((screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08, BLACK, BLACK)
|
||||
rightPupil = circle((screen_x+self.gridSize*GHOST_SIZE*(0.3+dx), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08, BLACK, BLACK)
|
||||
leftEye = circle((screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx/1.5), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2, WHITE, WHITE)
|
||||
rightEye = circle((screen_x+self.gridSize*GHOST_SIZE*(0.3+dx/1.5), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2, WHITE, WHITE)
|
||||
leftPupil = circle((screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08, BLACK, BLACK)
|
||||
rightPupil = circle((screen_x+self.gridSize*GHOST_SIZE*(0.3+dx), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08, BLACK, BLACK)
|
||||
ghostImageParts = []
|
||||
ghostImageParts.append(body)
|
||||
ghostImageParts.append(leftEye)
|
||||
@@ -369,7 +386,7 @@ class PacmanGraphics:
|
||||
return ghostImageParts
|
||||
|
||||
def moveEyes(self, pos, dir, eyes):
|
||||
(screen_x, screen_y) = (self.to_screen(pos) )
|
||||
(screen_x, screen_y) = (self.to_screen(pos))
|
||||
dx = 0
|
||||
dy = 0
|
||||
if dir == 'North':
|
||||
@@ -380,10 +397,14 @@ class PacmanGraphics:
|
||||
dx = 0.2
|
||||
if dir == 'West':
|
||||
dx = -0.2
|
||||
moveCircle(eyes[0],(screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx/1.5), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2)
|
||||
moveCircle(eyes[1],(screen_x+self.gridSize*GHOST_SIZE*(0.3+dx/1.5), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2)
|
||||
moveCircle(eyes[2],(screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08)
|
||||
moveCircle(eyes[3],(screen_x+self.gridSize*GHOST_SIZE*(0.3+dx), screen_y-self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08)
|
||||
moveCircle(eyes[0], (screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx/1.5), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2)
|
||||
moveCircle(eyes[1], (screen_x+self.gridSize*GHOST_SIZE*(0.3+dx/1.5), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy/1.5)), self.gridSize*GHOST_SIZE*0.2)
|
||||
moveCircle(eyes[2], (screen_x+self.gridSize*GHOST_SIZE*(-0.3+dx), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08)
|
||||
moveCircle(eyes[3], (screen_x+self.gridSize*GHOST_SIZE*(0.3+dx), screen_y -
|
||||
self.gridSize*GHOST_SIZE*(0.3-dy)), self.gridSize*GHOST_SIZE*0.08)
|
||||
|
||||
def moveGhost(self, ghost, ghostIndex, prevGhost, ghostImageParts):
|
||||
old_x, old_y = self.to_screen(self.getPosition(prevGhost))
|
||||
@@ -399,43 +420,48 @@ class PacmanGraphics:
|
||||
else:
|
||||
color = GHOST_COLORS[ghostIndex]
|
||||
edit(ghostImageParts[0], ('fill', color), ('outline', color))
|
||||
self.moveEyes(self.getPosition(ghost), self.getDirection(ghost), ghostImageParts[-4:])
|
||||
self.moveEyes(self.getPosition(ghost),
|
||||
self.getDirection(ghost), ghostImageParts[-4:])
|
||||
refresh()
|
||||
|
||||
def getPosition(self, agentState):
|
||||
if agentState.configuration == None: return (-1000, -1000)
|
||||
if agentState.configuration == None:
|
||||
return (-1000, -1000)
|
||||
return agentState.getPosition()
|
||||
|
||||
def getDirection(self, agentState):
|
||||
if agentState.configuration == None: return Directions.STOP
|
||||
if agentState.configuration == None:
|
||||
return Directions.STOP
|
||||
return agentState.configuration.getDirection()
|
||||
|
||||
def finish(self):
|
||||
end_graphics()
|
||||
|
||||
def to_screen(self, point):
|
||||
( x, y ) = point
|
||||
(x, y) = point
|
||||
#y = self.height - y
|
||||
x = (x + 1)*self.gridSize
|
||||
y = (self.height - y)*self.gridSize
|
||||
return ( x, y )
|
||||
y = (self.height - y)*self.gridSize
|
||||
return (x, y)
|
||||
|
||||
# Fixes some TK issue with off-center circles
|
||||
def to_screen2(self, point):
|
||||
( x, y ) = point
|
||||
(x, y) = point
|
||||
#y = self.height - y
|
||||
x = (x + 1)*self.gridSize
|
||||
y = (self.height - y)*self.gridSize
|
||||
return ( x, y )
|
||||
y = (self.height - y)*self.gridSize
|
||||
return (x, y)
|
||||
|
||||
def drawWalls(self, wallMatrix):
|
||||
wallColor = WALL_COLOR
|
||||
for xNum, x in enumerate(wallMatrix):
|
||||
if self.capture and (xNum * 2) < wallMatrix.width: wallColor = TEAM_COLORS[0]
|
||||
if self.capture and (xNum * 2) >= wallMatrix.width: wallColor = TEAM_COLORS[1]
|
||||
if self.capture and (xNum * 2) < wallMatrix.width:
|
||||
wallColor = TEAM_COLORS[0]
|
||||
if self.capture and (xNum * 2) >= wallMatrix.width:
|
||||
wallColor = TEAM_COLORS[1]
|
||||
|
||||
for yNum, cell in enumerate(x):
|
||||
if cell: # There's a wall here
|
||||
if cell: # There's a wall here
|
||||
pos = (xNum, yNum)
|
||||
screen = self.to_screen(pos)
|
||||
screen2 = self.to_screen2(pos)
|
||||
@@ -453,66 +479,90 @@ class PacmanGraphics:
|
||||
# NE quadrant
|
||||
if (not nIsWall) and (not eIsWall):
|
||||
# inner circle
|
||||
circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor, (0,91), 'arc')
|
||||
circle(screen2, WALL_RADIUS * self.gridSize,
|
||||
wallColor, wallColor, (0, 91), 'arc')
|
||||
if (nIsWall) and (not eIsWall):
|
||||
# vertical line
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, 0)), add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(-0.5)-1)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, 0)), add(screen,
|
||||
(self.gridSize*WALL_RADIUS, self.gridSize*(-0.5)-1)), wallColor)
|
||||
if (not nIsWall) and (eIsWall):
|
||||
# horizontal line
|
||||
line(add(screen, (0, self.gridSize*(-1)*WALL_RADIUS)), add(screen, (self.gridSize*0.5+1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (0, self.gridSize*(-1)*WALL_RADIUS)), add(screen,
|
||||
(self.gridSize*0.5+1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
if (nIsWall) and (eIsWall) and (not neIsWall):
|
||||
# outer circle
|
||||
circle(add(screen2, (self.gridSize*2*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS)), WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (180,271), 'arc')
|
||||
line(add(screen, (self.gridSize*2*WALL_RADIUS-1, self.gridSize*(-1)*WALL_RADIUS)), add(screen, (self.gridSize*0.5+1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS+1)), add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(-0.5))), wallColor)
|
||||
circle(add(screen2, (self.gridSize*2*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS)),
|
||||
WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (180, 271), 'arc')
|
||||
line(add(screen, (self.gridSize*2*WALL_RADIUS-1, self.gridSize*(-1)*WALL_RADIUS)),
|
||||
add(screen, (self.gridSize*0.5+1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS+1)),
|
||||
add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(-0.5))), wallColor)
|
||||
|
||||
# NW quadrant
|
||||
if (not nIsWall) and (not wIsWall):
|
||||
# inner circle
|
||||
circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor, (90,181), 'arc')
|
||||
circle(screen2, WALL_RADIUS * self.gridSize,
|
||||
wallColor, wallColor, (90, 181), 'arc')
|
||||
if (nIsWall) and (not wIsWall):
|
||||
# vertical line
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, 0)), add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-0.5)-1)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, 0)), add(screen,
|
||||
(self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-0.5)-1)), wallColor)
|
||||
if (not nIsWall) and (wIsWall):
|
||||
# horizontal line
|
||||
line(add(screen, (0, self.gridSize*(-1)*WALL_RADIUS)), add(screen, (self.gridSize*(-0.5)-1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (0, self.gridSize*(-1)*WALL_RADIUS)), add(screen,
|
||||
(self.gridSize*(-0.5)-1, self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
if (nIsWall) and (wIsWall) and (not nwIsWall):
|
||||
# outer circle
|
||||
circle(add(screen2, (self.gridSize*(-2)*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS)), WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (270,361), 'arc')
|
||||
line(add(screen, (self.gridSize*(-2)*WALL_RADIUS+1, self.gridSize*(-1)*WALL_RADIUS)), add(screen, (self.gridSize*(-0.5), self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS+1)), add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-0.5))), wallColor)
|
||||
circle(add(screen2, (self.gridSize*(-2)*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS)),
|
||||
WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (270, 361), 'arc')
|
||||
line(add(screen, (self.gridSize*(-2)*WALL_RADIUS+1, self.gridSize*(-1)*WALL_RADIUS)),
|
||||
add(screen, (self.gridSize*(-0.5), self.gridSize*(-1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-2)*WALL_RADIUS+1)),
|
||||
add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(-0.5))), wallColor)
|
||||
|
||||
# SE quadrant
|
||||
if (not sIsWall) and (not eIsWall):
|
||||
# inner circle
|
||||
circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor, (270,361), 'arc')
|
||||
circle(screen2, WALL_RADIUS * self.gridSize,
|
||||
wallColor, wallColor, (270, 361), 'arc')
|
||||
if (sIsWall) and (not eIsWall):
|
||||
# vertical line
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, 0)), add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(0.5)+1)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, 0)), add(screen,
|
||||
(self.gridSize*WALL_RADIUS, self.gridSize*(0.5)+1)), wallColor)
|
||||
if (not sIsWall) and (eIsWall):
|
||||
# horizontal line
|
||||
line(add(screen, (0, self.gridSize*(1)*WALL_RADIUS)), add(screen, (self.gridSize*0.5+1, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (0, self.gridSize*(1)*WALL_RADIUS)), add(screen,
|
||||
(self.gridSize*0.5+1, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
if (sIsWall) and (eIsWall) and (not seIsWall):
|
||||
# outer circle
|
||||
circle(add(screen2, (self.gridSize*2*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS)), WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (90,181), 'arc')
|
||||
line(add(screen, (self.gridSize*2*WALL_RADIUS-1, self.gridSize*(1)*WALL_RADIUS)), add(screen, (self.gridSize*0.5, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS-1)), add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(0.5))), wallColor)
|
||||
circle(add(screen2, (self.gridSize*2*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS)),
|
||||
WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (90, 181), 'arc')
|
||||
line(add(screen, (self.gridSize*2*WALL_RADIUS-1, self.gridSize*(1)*WALL_RADIUS)),
|
||||
add(screen, (self.gridSize*0.5, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS-1)),
|
||||
add(screen, (self.gridSize*WALL_RADIUS, self.gridSize*(0.5))), wallColor)
|
||||
|
||||
# SW quadrant
|
||||
if (not sIsWall) and (not wIsWall):
|
||||
# inner circle
|
||||
circle(screen2, WALL_RADIUS * self.gridSize, wallColor, wallColor, (180,271), 'arc')
|
||||
circle(screen2, WALL_RADIUS * self.gridSize,
|
||||
wallColor, wallColor, (180, 271), 'arc')
|
||||
if (sIsWall) and (not wIsWall):
|
||||
# vertical line
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, 0)), add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(0.5)+1)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, 0)), add(screen,
|
||||
(self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(0.5)+1)), wallColor)
|
||||
if (not sIsWall) and (wIsWall):
|
||||
# horizontal line
|
||||
line(add(screen, (0, self.gridSize*(1)*WALL_RADIUS)), add(screen, (self.gridSize*(-0.5)-1, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (0, self.gridSize*(1)*WALL_RADIUS)), add(screen,
|
||||
(self.gridSize*(-0.5)-1, self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
if (sIsWall) and (wIsWall) and (not swIsWall):
|
||||
# outer circle
|
||||
circle(add(screen2, (self.gridSize*(-2)*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS)), WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (0,91), 'arc')
|
||||
line(add(screen, (self.gridSize*(-2)*WALL_RADIUS+1, self.gridSize*(1)*WALL_RADIUS)), add(screen, (self.gridSize*(-0.5), self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS-1)), add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(0.5))), wallColor)
|
||||
circle(add(screen2, (self.gridSize*(-2)*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS)),
|
||||
WALL_RADIUS * self.gridSize-1, wallColor, wallColor, (0, 91), 'arc')
|
||||
line(add(screen, (self.gridSize*(-2)*WALL_RADIUS+1, self.gridSize*(1)*WALL_RADIUS)),
|
||||
add(screen, (self.gridSize*(-0.5), self.gridSize*(1)*WALL_RADIUS)), wallColor)
|
||||
line(add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(2)*WALL_RADIUS-1)),
|
||||
add(screen, (self.gridSize*(-1)*WALL_RADIUS, self.gridSize*(0.5))), wallColor)
|
||||
|
||||
def isWall(self, x, y, walls):
|
||||
if x < 0 or y < 0:
|
||||
@@ -521,43 +571,45 @@ class PacmanGraphics:
|
||||
return False
|
||||
return walls[x][y]
|
||||
|
||||
def drawFood(self, foodMatrix ):
|
||||
def drawFood(self, foodMatrix):
|
||||
foodImages = []
|
||||
color = FOOD_COLOR
|
||||
for xNum, x in enumerate(foodMatrix):
|
||||
if self.capture and (xNum * 2) <= foodMatrix.width: color = TEAM_COLORS[0]
|
||||
if self.capture and (xNum * 2) > foodMatrix.width: color = TEAM_COLORS[1]
|
||||
if self.capture and (xNum * 2) <= foodMatrix.width:
|
||||
color = TEAM_COLORS[0]
|
||||
if self.capture and (xNum * 2) > foodMatrix.width:
|
||||
color = TEAM_COLORS[1]
|
||||
imageRow = []
|
||||
foodImages.append(imageRow)
|
||||
for yNum, cell in enumerate(x):
|
||||
if cell: # There's food here
|
||||
screen = self.to_screen((xNum, yNum ))
|
||||
dot = circle( screen,
|
||||
FOOD_SIZE * self.gridSize,
|
||||
outlineColor = color, fillColor = color,
|
||||
width = 1)
|
||||
if cell: # There's food here
|
||||
screen = self.to_screen((xNum, yNum))
|
||||
dot = circle(screen,
|
||||
FOOD_SIZE * self.gridSize,
|
||||
outlineColor=color, fillColor=color,
|
||||
width=1)
|
||||
imageRow.append(dot)
|
||||
else:
|
||||
imageRow.append(None)
|
||||
return foodImages
|
||||
|
||||
def drawCapsules(self, capsules ):
|
||||
def drawCapsules(self, capsules):
|
||||
capsuleImages = {}
|
||||
for capsule in capsules:
|
||||
( screen_x, screen_y ) = self.to_screen(capsule)
|
||||
dot = circle( (screen_x, screen_y),
|
||||
CAPSULE_SIZE * self.gridSize,
|
||||
outlineColor = CAPSULE_COLOR,
|
||||
fillColor = CAPSULE_COLOR,
|
||||
width = 1)
|
||||
(screen_x, screen_y) = self.to_screen(capsule)
|
||||
dot = circle((screen_x, screen_y),
|
||||
CAPSULE_SIZE * self.gridSize,
|
||||
outlineColor=CAPSULE_COLOR,
|
||||
fillColor=CAPSULE_COLOR,
|
||||
width=1)
|
||||
capsuleImages[capsule] = dot
|
||||
return capsuleImages
|
||||
|
||||
def removeFood(self, cell, foodImages ):
|
||||
def removeFood(self, cell, foodImages):
|
||||
x, y = cell
|
||||
remove_from_screen(foodImages[x][y])
|
||||
|
||||
def removeCapsule(self, cell, capsuleImages ):
|
||||
def removeCapsule(self, cell, capsuleImages):
|
||||
x, y = cell
|
||||
remove_from_screen(capsuleImages[(x, y)])
|
||||
|
||||
@@ -570,12 +622,13 @@ class PacmanGraphics:
|
||||
self.clearExpandedCells()
|
||||
self.expandedCells = []
|
||||
for k, cell in enumerate(cells):
|
||||
screenPos = self.to_screen( cell)
|
||||
cellColor = formatColor(*[(n-k) * c * .5 / n + .25 for c in baseColor])
|
||||
screenPos = self.to_screen(cell)
|
||||
cellColor = formatColor(
|
||||
*[(n-k) * c * .5 / n + .25 for c in baseColor])
|
||||
block = square(screenPos,
|
||||
0.5 * self.gridSize,
|
||||
color = cellColor,
|
||||
filled = 1, behind=2)
|
||||
0.5 * self.gridSize,
|
||||
color=cellColor,
|
||||
filled=1, behind=2)
|
||||
self.expandedCells.append(block)
|
||||
if self.frameTime < 0:
|
||||
refresh()
|
||||
@@ -585,36 +638,38 @@ class PacmanGraphics:
|
||||
for cell in self.expandedCells:
|
||||
remove_from_screen(cell)
|
||||
|
||||
|
||||
def updateDistributions(self, distributions):
|
||||
"Draws an agent's belief distributions"
|
||||
# copy all distributions so we don't change their state
|
||||
distributions = map(lambda x: x.copy(), distributions)
|
||||
distributions = [x.copy() for x in distributions]
|
||||
if self.distributionImages == None:
|
||||
self.drawDistributions(self.previousState)
|
||||
for x in range(len(self.distributionImages)):
|
||||
for y in range(len(self.distributionImages[0])):
|
||||
image = self.distributionImages[x][y]
|
||||
weights = [dist[ (x,y) ] for dist in distributions]
|
||||
weights = [dist[(x, y)] for dist in distributions]
|
||||
|
||||
if sum(weights) != 0:
|
||||
pass
|
||||
# Fog of war
|
||||
color = [0.0,0.0,0.0]
|
||||
colors = GHOST_VEC_COLORS[1:] # With Pacman
|
||||
if self.capture: colors = GHOST_VEC_COLORS
|
||||
color = [0.0, 0.0, 0.0]
|
||||
colors = GHOST_VEC_COLORS[1:] # With Pacman
|
||||
if self.capture:
|
||||
colors = GHOST_VEC_COLORS
|
||||
for weight, gcolor in zip(weights, colors):
|
||||
color = [min(1.0, c + 0.95 * g * weight ** .3) for c,g in zip(color, gcolor)]
|
||||
color = [min(1.0, c + 0.95 * g * weight ** .3)
|
||||
for c, g in zip(color, gcolor)]
|
||||
changeColor(image, formatColor(*color))
|
||||
refresh()
|
||||
|
||||
|
||||
class FirstPersonPacmanGraphics(PacmanGraphics):
|
||||
def __init__(self, zoom = 1.0, showGhosts = True, capture = False, frameTime=0):
|
||||
def __init__(self, zoom=1.0, showGhosts=True, capture=False, frameTime=0):
|
||||
PacmanGraphics.__init__(self, zoom, frameTime=frameTime)
|
||||
self.showGhosts = showGhosts
|
||||
self.capture = capture
|
||||
|
||||
def initialize(self, state, isBlue = False):
|
||||
def initialize(self, state, isBlue=False):
|
||||
|
||||
self.isBlue = isBlue
|
||||
PacmanGraphics.startGraphics(self, state)
|
||||
@@ -654,6 +709,7 @@ class FirstPersonPacmanGraphics(PacmanGraphics):
|
||||
else:
|
||||
return PacmanGraphics.getPosition(self, ghostState)
|
||||
|
||||
|
||||
def add(x, y):
|
||||
return (x[0] + y[0], x[1] + y[1])
|
||||
|
||||
@@ -669,11 +725,14 @@ POSTSCRIPT_OUTPUT_DIR = 'frames'
|
||||
FRAME_NUMBER = 0
|
||||
import os
|
||||
|
||||
|
||||
def saveFrame():
|
||||
"Saves the current graphical output as a postscript file"
|
||||
global SAVE_POSTSCRIPT, FRAME_NUMBER, POSTSCRIPT_OUTPUT_DIR
|
||||
if not SAVE_POSTSCRIPT: return
|
||||
if not os.path.exists(POSTSCRIPT_OUTPUT_DIR): os.mkdir(POSTSCRIPT_OUTPUT_DIR)
|
||||
if not SAVE_POSTSCRIPT:
|
||||
return
|
||||
if not os.path.exists(POSTSCRIPT_OUTPUT_DIR):
|
||||
os.mkdir(POSTSCRIPT_OUTPUT_DIR)
|
||||
name = os.path.join(POSTSCRIPT_OUTPUT_DIR, 'frame_%08d.ps' % FRAME_NUMBER)
|
||||
FRAME_NUMBER += 1
|
||||
writePostscript(name) # writes the current canvas
|
||||
writePostscript(name) # writes the current canvas
|
||||
|
||||
Reference in New Issue
Block a user