def loadReferenceMap(self, filename): with open(filename, 'r') as file: lines = file.readlines()[1:-1] y = 1 for line in lines: x = 1 line = line[1:-2] for char in line: if char == 'X': SETTINGS.ObstacleTiles.append( Tile( vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]))) if char == 'S': self.start = vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]) if char == 'G': self.end = vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]) x += 1 y += 1 for col in range(len(SETTINGS.Graph)): for row in range(len(SETTINGS.Graph[col])): node = SETTINGS.Graph[col][row] node.addNeighbours() node.validate()
def renderLine(self, start: vec2, end: vec2, color=(255, 255, 255), width=1): pygame.draw.line(self.surface, color, vec2(start).tuple, vec2(end).tuple, width)
def renderGrid(self): tWidth = SETTINGS.TILE_SCALE[0] tHeight = SETTINGS.TILE_SCALE[1] sWidth = (SETTINGS.SCREEN_WIDTH - tWidth) sHeight = (SETTINGS.SCREEN_HEIGHT - tHeight) for x in fori(tWidth, sWidth, tWidth): self.renderLine(vec2(x, tHeight), vec2(x, sHeight)) for y in fori(tHeight, sHeight, tHeight): self.renderLine(vec2(tWidth, y), vec2(sWidth, y))
def __init__(self, filename, reference=None): self.tmx = pytmx.load_pygame(filename, pixelalpha=True) mapWidth = self.tmx.width * self.tmx.tilewidth mapHeight = self.tmx.height * self.tmx.tileheight SETTINGS.configure(mapWidth, mapHeight) self.start = vec2(0, 0) self.end = vec2(0, 0) self.loadPath() if reference: self.loadReferenceMap(reference)
def followTarget(cls, target: Entity): centerx = (-target.position.X + cls.width // 2) - cls.center.X centery = (-target.position.Y + cls.height // 2) - cls.center.Y cls.center += vec2(centerx, centery) * GameTime.fixedDeltaTime # Make sure we're within map boundaries xMin = min(0, cls.center.X) yMin = min(0, cls.center.Y) xMax = max(-(cls.width - SETTINGS.SCREEN_WIDTH), xMin) yMax = max(-(cls.height - SETTINGS.SCREEN_HEIGHT), yMin) cls.center = vec2(xMax, yMax) cls.rect = pygame.Rect(cls.center.X, cls.center.Y, cls.width, cls.height)
def loadMap(self, index): SETTINGS.CURRENT_LEVEL = index self.agents = [] self.buildings = [] if index == 4: #SETTINGS.SCREEN_WIDTH = SETTINGS.SCREEN_HEIGHT = 832 #SETTINGS.SCREEN_WIDTH = 1024 #SETTINGS.SCREEN_HEIGHT = 768 self.map = Map(self.getRealFilePath(SETTINGS.MAP_OLD)) self.scaleAssets() self.buildings = (getClub(), getDrink(), getResturant(), getStore(), getStackHQ(), getHotel(), getHangout(), getLTU()) self.agents = [ Entity("Alex", Collect(), Global(), self.entityImg, vec2(359, 307)) ] #Entity("Wendy", Collect(), Global(), self.entityImg, vec2(150, 610)), #Entity("John", Collect(), Global(), self.entityImg, vec2(700, 380)), #Entity("James", Collect(), Global(), self.entityImg, vec2(940, 400))] else: if index == 1: self.map = Map(self.getRealFilePath(SETTINGS.MAP_1), self.getRealFilePath(SETTINGS.MAP_REF1)) self.scaleAssets() elif index == 2: self.map = Map(self.getRealFilePath(SETTINGS.MAP_2), self.getRealFilePath(SETTINGS.MAP_REF2)) self.scaleAssets() elif index == 3: SETTINGS.SCREEN_WIDTH = SETTINGS.SCREEN_HEIGHT = 832 self.map = Map(self.getRealFilePath(SETTINGS.MAP_3), self.getRealFilePath(SETTINGS.MAP_REF3)) self.scaleAssets() self.agents = [ Entity("John", None, None, self.entityImg, self.map.start) ] self.setEnd(self.map.end) self.setStart(self.map.start) CameraInstance.init() self.surface = pygame.display.set_mode( (SETTINGS.SCREEN_WIDTH, SETTINGS.SCREEN_HEIGHT)) self.renderer = Renderer(self.surface) self.updatePaths()
def __init__(self, position: vec2, name: str, description: str = None): self.position = position self.name = name self.description = description threshold = 8 temp = vec2( self.position.X + random.randrange(-threshold, threshold), self.position.Y + random.randrange(-threshold, threshold / 2)) self.randomized = temp
def __init__(self, directory, folder): self.directory = directory self.folder = folder pygame.init() pygame.mixer.init() pygame.freetype.init() self.surface = pygame.display.set_mode( (SETTINGS.SCREEN_WIDTH, SETTINGS.SCREEN_HEIGHT)) self.renderer = Renderer(self.surface) logo = pygame.image.load(self.getRealFilePath(SETTINGS.ICON_PATH)) pygame.display.set_icon(logo) pygame.display.set_caption(SETTINGS.TITLE) self.clock = pygame.time.Clock() self.paused = False temp = pygame.mouse.get_pos() self.cursor = vec2(temp[0], temp[1]) self.cursorSize = 9 self.realCursorEnabled = False pygame.mouse.set_visible(self.realCursorEnabled) pygame.event.set_grab(not self.realCursorEnabled) self.startPos = vec2() self.endPos = vec2() self.agents = [] self.buildings = [] self.activePaths = [] self.activeChildren = [] self.loadMap(1)
def addNeighbours(self): self.neighbours.clear() adjacent = [ vec2(1, 0), vec2(-1, 0), vec2(0, 1), vec2(0, -1), # Vertical / Horizontal vec2(1, 1), vec2(-1, 1), vec2(1, -1), vec2(-1, -1) ] # Diagonal for direction in adjacent: neighbour = self.position + vec2( direction.X * SETTINGS.TILE_SCALE[0], direction.Y * SETTINGS.TILE_SCALE[1]) if neighbour not in self.neighbours: if 0 < neighbour.X < SETTINGS.SCREEN_WIDTH - SETTINGS.TILE_SCALE[ 0] and 0 < neighbour.Y < SETTINGS.SCREEN_HEIGHT - SETTINGS.TILE_SCALE[ 1]: self.neighbours.append(neighbour)
def update(self): CameraInstance.followTarget(self.agents[0]) #self.cursor.log() if not self.paused: pygame.display.set_caption(SETTINGS.TITLE + " | Speed: " + str(GameTime.timeScale) + " | FPS " + "{:.0f}".format(self.clock.get_fps()) + " | Date: " + GameTime.timeElapsed() + " | Algorithm: " + str(self.agents[0].getPathType())) if not self.realCursorEnabled: temp = pygame.mouse.get_pos() self.cursor = vec2(temp[0], temp[1]) for agent in self.agents: agent.update()
def centeredVec(cls, vec): if SETTINGS.CURRENT_LEVEL >= 4: camPos = vec2(CameraInstance.center.X, CameraInstance.center.Y) return (camPos + vec).tuple else: return vec[0], vec[1]
def init(cls): cls.center = vec2() print(str(SETTINGS.MAP_WIDTH)) cls.rect = pygame.Rect(0, 0, SETTINGS.MAP_WIDTH, SETTINGS.MAP_HEIGHT) cls.width = cls.rect[2] cls.height = cls.rect[3]
def loadPath(self): startTime = time.time() pathLayer = self.tmx.get_layer_by_name("Path") backgroundLayer = self.tmx.get_layer_by_name("Background") ti = self.tmx.get_tile_image_by_gid SETTINGS.TilesAll = [] SETTINGS.PathTiles = [] SETTINGS.ObstacleTiles = [] SETTINGS.BackgroundTIles = [] # This creates an 2D array, very quickly, through copying the same immutable object over and over again rows, cols = (SETTINGS.MAP_WIDTH, SETTINGS.MAP_HEIGHT) SETTINGS.Graph = [i[:] for i in [[0] * rows] * cols] for x, y, gid in backgroundLayer: tile = ti(gid) if tile: tileObj = Tile( vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]), gid) tileObj.addImage(tile) SETTINGS.BackgroundTIles.append(tileObj) for x, y, gid in pathLayer: tile = ti(gid) if tile: position = vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]) tileObj = Tile(position, gid) tileObj.addImage(tile) SETTINGS.PathTiles.append(tileObj) nodeObj = Node(position) SETTINGS.Graph[y][x] = nodeObj for layer in self.tmx.visible_layers: for x, y, gid in layer: tile = ti(gid) if tile: tileObj = Tile( vec2(x * SETTINGS.TILE_SCALE[0], y * SETTINGS.TILE_SCALE[1]), gid) tileObj.addImage(tile) SETTINGS.TilesAll.append(tileObj) temp = [] #print(str(type(SETTINGS.Graph))) for x in SETTINGS.Graph: row = [] for y in x: if str(y) != str(0): row.append(y) if len(row) > 0: temp.append(row) SETTINGS.Graph = temp for col in range(len(SETTINGS.Graph)): for row in range(len(SETTINGS.Graph[col])): node = SETTINGS.Graph[col][row] node.addNeighbours() node.validate() timeElapsed = time.time() - startTime print("Loaded map in: " + str(truncate(timeElapsed * 1000)) + "ms")
def draw(self): self.renderer.clear() for tile in SETTINGS.BackgroundTIles: self.renderer.renderTileImg(tile.image, tile.position) for tile in SETTINGS.PathTiles: self.renderer.renderTileImg(tile.image, tile.position) for tile in SETTINGS.TilesAll: self.renderer.renderTileImg(tile.image, tile.position) #self.renderer.renderGrid() if SETTINGS.CURRENT_LEVEL <= 4: for obstacle in SETTINGS.ObstacleTiles: self.renderer.renderTileImg(self.obstacleImg, obstacle.position) self.renderer.renderTileImg(self.startImg, self.startPos) self.renderer.renderTileImg(self.goalImg, self.endPos) if self.activePaths: # children for child in self.activeChildren: if child.position == self.endPos or child.position == self.startPos: continue self.renderer.renderRect( (SETTINGS.TILE_SCALE[0], SETTINGS.TILE_SCALE[1]), child.position.tuple, child.color) # path line for i in range(1, len(self.activePaths) - 1): waypoint1 = (self.activePaths[i].position + SETTINGS.TILE_SCALE[0] / 2).toInt.tuple waypoint2 = (self.activePaths[i + 1].position + SETTINGS.TILE_SCALE[1] / 2).toInt.tuple self.renderer.renderLine(waypoint1, waypoint2) pygame.draw.circle(self.surface, (152, 52, 23), waypoint1, 2) # agents path for agent in self.agents: waypoint1 = vec2( agent.position.X + SETTINGS.TILE_SCALE[0] / 2, agent.position.Y + SETTINGS.TILE_SCALE[1] / 2) waypoint2 = vec2( agent.nextNode.X + SETTINGS.TILE_SCALE[0] / 2, agent.nextNode.Y + SETTINGS.TILE_SCALE[1] / 2) self.renderer.renderLine(waypoint1, waypoint2, (152, 52, 152), 5) if not self.realCursorEnabled: intersection = self.selectedTile() if intersection: self.renderer.renderRect(SETTINGS.TILE_SCALE, intersection.position.tuple) self.renderer.renderRect((self.cursorSize, self.cursorSize), (self.cursor.X + self.cursorSize, self.cursor.Y + self.cursorSize), (37, 37, 38), 200) for agent in self.agents: self.renderer.renderTileImg(agent.image, agent.position) self.clock.tick(SETTINGS.MAX_FPS)