def __init__(self, world): self.world = world self.tile_size = 6 self.width = 192 self.height = 192 self.camera = pygame.Rect((0, 0), (self.width, self.height)).copy() self.map = Map() self.map.load_dungeon(world.map.dungeon, True, self.tile_size) self.zoom_in_arrow = ZoomArrow(0, (174, 10)) self.zoom_in_arrow.create() self.zoom_out_arrow = ZoomArrow(1, (174, 20)) self.zoom_out_arrow.create() Gui.__init__(self, 'minimap', (4, 4), pygame.Surface((self.width, self.height)), True) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit( self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) register_handler([TIME_PASSED, pygame.MOUSEBUTTONDOWN], self.handle_event)
def __init__(self, floor, up=None, down=None): self.entities = [] self.player = None self.items = [] self.up = up self.floor = floor self.down = down self.dungeon = Dungeon( (random.randint(33, 50), random.randint(33, 50)), "None", 100, (5, 5), (32, 32), (32, 32)) self.dungeon.generate_dungeon() self.map = Map() self.width = self.dungeon.width self.height = self.dungeon.height self.grid_size_x = self.dungeon.grid_size_x self.grid_size_y = self.dungeon.grid_size_y self.map.load_dungeon(self.dungeon) for y in range(0, len(self.dungeon.grid)): for x in range(0, len(self.dungeon.grid[0])): if self.dungeon.grid[y][x] == 8: self.up_stair = (x, y) elif self.dungeon.grid[y][x] == 9: self.down_stair = (x, y)
def __init__(self, floor, up=None, down=None): self.monsters = [] self.items = [] self.up = up self.floor = floor self.down = down self.dungeon = Dungeon((random.randint(33, 50), random.randint(33, 50)), "None", 100, (5, 5), (32, 32), (32, 32)) self.dungeon.generate_dungeon() self.explored_tiles = copy.deepcopy(self.dungeon.grid) self.map = Map() self.width = self.dungeon.width self.height = self.dungeon.height self.grid_size_x = self.dungeon.grid_size_x self.grid_size_y = self.dungeon.grid_size_y self.map.load_dungeon(self.dungeon) for y in range(0, len(self.dungeon.grid)): for x in range(0, len(self.dungeon.grid[0])): if self.dungeon.grid[y][x] == 8: self.up_stair = (x, y) elif self.dungeon.grid[y][x] == 9: self.down_stair = (x, y) self.explored_tiles[y][x] = -1
def __init__(self, world): self.world = world self.tile_size = 6 self.width = 192 self.height = 192 self.camera = pygame.Rect((0, 0), (self.width, self.height)).copy() self.map = Map() self.map.load_dungeon(world.map.dungeon, True, self.tile_size) self.zoom_in_arrow = ZoomArrow(0, (174, 10)) self.zoom_in_arrow.create() self.zoom_out_arrow = ZoomArrow(1, (174, 20)) self.zoom_out_arrow.create() Gui.__init__(self, 'minimap', (4, 4), pygame.Surface((self.width, self.height)), True) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit(self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) register_handler([TIME_PASSED, pygame.MOUSEBUTTONDOWN], self.handle_event)
class MiniMap(Gui): def __init__(self, world): self.world = world self.tile_size = 6 self.width = 192 self.height = 192 self.camera = pygame.Rect((0, 0), (self.width, self.height)).copy() self.map = Map() self.map.load_dungeon(world.map.dungeon, True, self.tile_size) self.zoom_in_arrow = ZoomArrow(0, (174, 10)) self.zoom_in_arrow.create() self.zoom_out_arrow = ZoomArrow(1, (174, 20)) self.zoom_out_arrow.create() Gui.__init__(self, 'minimap', (4, 4), pygame.Surface((self.width, self.height)), True) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit( self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) register_handler([TIME_PASSED, pygame.MOUSEBUTTONDOWN], self.handle_event) def redraw(self): self.image.fill((0, 0, 0)) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.load_dungeon(self.world.map.dungeon, True, self.tile_size) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.zoom_in_arrow.create() self.zoom_out_arrow.create() self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit( self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) def handle_event(self, event): etype = get_event_type(event) if etype == TIME_PASSED: self.redraw() elif etype == pygame.MOUSEBUTTONDOWN and event.button in [4, 5]: if event.button == 4: self.zoom_in() elif event.button == 5: self.zoom_out() def mouse(self, mouse, event): mouse.x -= self.x mouse.y -= self.y if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if mouse.colliderect(self.zoom_in_arrow.rect): self.zoom_in() elif mouse.colliderect(self.zoom_out_arrow.rect): self.zoom_out() def zoom_in(self): if self.zoom_in_arrow.zoom_level < self.zoom_in_arrow.max_zoom_level: self.zoom_in_arrow.zoom_level += 1 self.zoom_out_arrow.zoom_level -= 1 self.tile_size += 2 self.redraw() def zoom_out(self): if self.zoom_out_arrow.zoom_level < self.zoom_out_arrow.max_zoom_level: self.zoom_out_arrow.zoom_level += 1 self.zoom_in_arrow.zoom_level -= 1 self.tile_size -= 2 self.redraw() def draw(self, screen): screen.blit(self.image, (self.x, self.y))
class DungeonLevel(object): def __init__(self, floor, up=None, down=None): self.entities = [] self.player = None self.items = [] self.up = up self.floor = floor self.down = down self.dungeon = Dungeon( (random.randint(33, 50), random.randint(33, 50)), "None", 100, (5, 5), (32, 32), (32, 32)) self.dungeon.generate_dungeon() self.map = Map() self.width = self.dungeon.width self.height = self.dungeon.height self.grid_size_x = self.dungeon.grid_size_x self.grid_size_y = self.dungeon.grid_size_y self.map.load_dungeon(self.dungeon) for y in range(0, len(self.dungeon.grid)): for x in range(0, len(self.dungeon.grid[0])): if self.dungeon.grid[y][x] == 8: self.up_stair = (x, y) elif self.dungeon.grid[y][x] == 9: self.down_stair = (x, y) def setup(self): # Prepare first level if not self.up: self.dungeon.grid[self.up_stair[1]][self.up_stair[0]] = 1 self.spawn = self.up_stair self.map.load_dungeon(self.dungeon) def fog_of_war(self, x0, y0, radius): for y in range(len(self.map.tiles)): for x in range(len(self.map.tiles[y])): if self.map.tiles[y][x].explored == 2: self.map.tiles[y][x].explored = 1 fov = FOVCalc() fov.NOT_VISIBLE_BLOCKS_VISION = True fov.RESTRICTIVENESS = 0 fov.VISIBLE_ON_EQUAL = False cells = fov.calc_visible_cells_from(x0, y0, radius, self.is_unobstructed) for coords in cells: if len(coords) == 2: self.map.tiles[coords[1]][coords[0]].explored = 2 def is_unobstructed(self, x, y): try: if self.map.tiles[y][x].id in [0, 2, 3, 4, 5, 6]: return False else: return True except IndexError: return False def handle_event(self, event): etype = get_event_type(event) if etype == PLAYER_DROP_ITEM: self.items.append(event.target) elif etype == PLAYER_PICKUP_ITEM: self.items.remove(event.target) def spawn_objects(self): for row in range(0, len(self.map.tiles)): for tile in range(0, len(self.map.tiles[row])): if self.map.tiles[row][tile].id == 10: self.map.tiles[row][tile].id = 1 self.map.tiles[row][tile].load_image() self.items.append(Chest((tile, row), self)) if self.map.tiles[row][tile].id == 7: self.map.tiles[row][tile].id = 1 self.entities.append(Monster((tile, row), self)) if self.map.tiles[row][tile].id == 11: self.map.tiles[row][tile].id = 1 self.dungeon.grid[row][tile] = 1 self.map.tiles[row][tile].load_image() def get_item(self, x, y): items = [] for item in self.items: if item: if item.x == x and item.y == y: items.append(item) if items: return items def get_line(self, x1, y1, x2, y2): points = [] issteep = abs(y2 - y1) > abs(x2 - x1) if issteep: x1, y1 = y1, x1 x2, y2 = y2, x2 rev = False if x1 > x2: x1, x2 = x2, x1 y1, y2 = y2, y1 rev = True deltax = x2 - x1 deltay = abs(y2 - y1) error = int(deltax / 2) y = y1 ystep = None if y1 < y2: ystep = 1 else: ystep = -1 for x in range(x1, x2 + 1): if issteep: points.append((y, x)) else: points.append((x, y)) error -= deltay if error < 0: y += ystep error += deltax # Reverse the list if the coordinates were reversed if rev: points.reverse() return points
class DungeonLevel(object): def __init__(self, floor, up=None, down=None): self.monsters = [] self.items = [] self.up = up self.floor = floor self.down = down self.dungeon = Dungeon( (random.randint(33, 50), random.randint(33, 50)), "None", 100, (5, 5), (32, 32), (32, 32)) self.dungeon.generate_dungeon() self.explored_tiles = copy.deepcopy(self.dungeon.grid) self.map = Map() self.width = self.dungeon.width self.height = self.dungeon.height self.grid_size_x = self.dungeon.grid_size_x self.grid_size_y = self.dungeon.grid_size_y self.map.load_dungeon(self.dungeon) for y in range(0, len(self.dungeon.grid)): for x in range(0, len(self.dungeon.grid[0])): if self.dungeon.grid[y][x] == 8: self.up_stair = (x, y) elif self.dungeon.grid[y][x] == 9: self.down_stair = (x, y) self.explored_tiles[y][x] = -1 def setup(self): # Prepare first level if not self.up: self.dungeon.grid[self.up_stair[1]][self.up_stair[0]] = 1 self.spawn = self.up_stair self.map.load_dungeon(self.dungeon) def fog_of_war(self, x0, y0, radius): for y in range(len(self.explored_tiles)): for x in range(len(self.explored_tiles[y])): if self.explored_tiles[y][x] == 1: self.explored_tiles[y][x] = 0 end_points = [] lines = [] f = 1 - radius ddf_x = 1 ddf_y = -2 * radius x = 0 y = radius end_points.append((x0, y0 + radius)) end_points.append((x0, y0 - radius)) end_points.append((x0 + radius, y0)) end_points.append((x0 - radius, y0)) end_points.append((x0 - 7, y0 + 3)) end_points.append((x0 + 7, y0 + 3)) end_points.append((x0 - 7, y0 - 3)) end_points.append((x0 + 7, y0 - 3)) end_points.append((x0 - 3, y0 - 7)) end_points.append((x0 + 3, y0 - 7)) end_points.append((x0 - 7, y0 - 6)) end_points.append((x0 - 7, y0 + 6)) end_points.append((x0 + 7, y0 - 6)) end_points.append((x0 + 7, y0 + 6)) end_points.append((x0 - 6, y0 - 7)) end_points.append((x0 + 6, y0 - 7)) end_points.append((x0 - 6, y0 + 7)) end_points.append((x0 + 6, y0 + 7)) while x < y: if f >= 0: y -= 1 ddf_y += 1 f += ddf_y x += 1 ddf_x += 1 f += ddf_x end_points.append((x0 + x, y0 + y)) end_points.append((x0 - x, y0 + y)) end_points.append((x0 + x, y0 - y)) end_points.append((x0 - x, y0 - y)) end_points.append((x0 + y, y0 + x)) end_points.append((x0 - y, y0 + x)) end_points.append((x0 + y, y0 - x)) end_points.append((x0 - y, y0 - x)) for i in range(len(end_points)): if end_points[i][0] < 0: end_points[i] = (0, end_points[i][1]) elif end_points[i][0] > len(self.explored_tiles[0]) - 1: end_points[i] = (len(self.explored_tiles[0]) - 1, end_points[i][1]) if end_points[i][1] < 0: end_points[i] = (end_points[i][0], 0) elif end_points[i][1] > len(self.explored_tiles) - 1: end_points[i] = (end_points[i][0], len(self.explored_tiles) - 1) line = self.get_line(x0, y0, end_points[i][0], end_points[i][1]) lines.append(line) for line in lines: for pos in line: x = pos[0] y = pos[1] if self.dungeon.grid[y][x] in [0, 2, 3, 4, 5, 6]: self.explored_tiles[y][x] = 1 break else: self.explored_tiles[pos[1]][pos[0]] = 1 def handle_event(self, event): etype = get_event_type(event) if etype == PLAYER_DROP_ITEM: self.items.append(event.target) elif etype == PLAYER_PICKUP_ITEM: self.items.remove(event.target) def spawn_objects(self): for row in range(0, len(self.map.tiles)): for tile in range(0, len(self.map.tiles[row])): if self.map.tiles[row][tile].id == 10: self.map.tiles[row][tile].id = 1 self.map.tiles[row][tile].load_image() self.items.append(Chest((tile, row), self)) elif self.map.tiles[row][tile].id == 7: self.map.tiles[row][tile].id = 1 self.map.tiles[row][tile].load_image() self.monsters.append( Monster('test', 'warrior', (tile, row), self)) def get_item(self, x, y): items = [] for item in self.items: if item: if item.x == x and item.y == y: items.append(item) if items: return items def get_line(self, x1, y1, x2, y2): points = [] issteep = abs(y2 - y1) > abs(x2 - x1) if issteep: x1, y1 = y1, x1 x2, y2 = y2, x2 rev = False if x1 > x2: x1, x2 = x2, x1 y1, y2 = y2, y1 rev = True deltax = x2 - x1 deltay = abs(y2 - y1) error = int(deltax / 2) y = y1 ystep = None if y1 < y2: ystep = 1 else: ystep = -1 for x in range(x1, x2 + 1): if issteep: points.append((y, x)) else: points.append((x, y)) error -= deltay if error < 0: y += ystep error += deltax # Reverse the list if the coordinates were reversed if rev: points.reverse() return points
class DungeonLevel(object): def __init__(self, floor, up=None, down=None): self.monsters = [] self.items = [] self.up = up self.floor = floor self.down = down self.dungeon = Dungeon((random.randint(33, 50), random.randint(33, 50)), "None", 100, (5, 5), (32, 32), (32, 32)) self.dungeon.generate_dungeon() self.explored_tiles = copy.deepcopy(self.dungeon.grid) self.map = Map() self.width = self.dungeon.width self.height = self.dungeon.height self.grid_size_x = self.dungeon.grid_size_x self.grid_size_y = self.dungeon.grid_size_y self.map.load_dungeon(self.dungeon) for y in range(0, len(self.dungeon.grid)): for x in range(0, len(self.dungeon.grid[0])): if self.dungeon.grid[y][x] == 8: self.up_stair = (x, y) elif self.dungeon.grid[y][x] == 9: self.down_stair = (x, y) self.explored_tiles[y][x] = -1 def setup(self): # Prepare first level if not self.up: self.dungeon.grid[self.up_stair[1]][self.up_stair[0]] = 1 self.spawn = self.up_stair self.map.load_dungeon(self.dungeon) def fog_of_war(self, x0, y0, radius): for y in range(len(self.explored_tiles)): for x in range(len(self.explored_tiles[y])): if self.explored_tiles[y][x] == 1: self.explored_tiles[y][x] = 0 end_points = [] lines = [] f = 1 - radius ddf_x = 1 ddf_y = -2 * radius x = 0 y = radius end_points.append((x0, y0 + radius)) end_points.append((x0, y0 - radius)) end_points.append((x0 + radius, y0)) end_points.append((x0 - radius, y0)) end_points.append((x0 - 7, y0 + 3)) end_points.append((x0 + 7, y0 + 3)) end_points.append((x0 - 7, y0 - 3)) end_points.append((x0 + 7, y0 - 3)) end_points.append((x0 - 3, y0 - 7)) end_points.append((x0 + 3, y0 - 7)) end_points.append((x0 - 7, y0 - 6)) end_points.append((x0 - 7, y0 + 6)) end_points.append((x0 + 7, y0 - 6)) end_points.append((x0 + 7, y0 + 6)) end_points.append((x0 - 6, y0 - 7)) end_points.append((x0 + 6, y0 - 7)) end_points.append((x0 - 6, y0 + 7)) end_points.append((x0 + 6, y0 + 7)) while x < y: if f >= 0: y -= 1 ddf_y += 1 f += ddf_y x += 1 ddf_x += 1 f += ddf_x end_points.append((x0 + x, y0 + y)) end_points.append((x0 - x, y0 + y)) end_points.append((x0 + x, y0 - y)) end_points.append((x0 - x, y0 - y)) end_points.append((x0 + y, y0 + x)) end_points.append((x0 - y, y0 + x)) end_points.append((x0 + y, y0 - x)) end_points.append((x0 - y, y0 - x)) for i in range(len(end_points)): if end_points[i][0] < 0: end_points[i] = (0, end_points[i][1]) elif end_points[i][0] > len(self.explored_tiles[0]) - 1: end_points[i] = (len(self.explored_tiles[0]) - 1, end_points[i][1]) if end_points[i][1] < 0: end_points[i] = (end_points[i][0], 0) elif end_points[i][1] > len(self.explored_tiles) - 1: end_points[i] = (end_points[i][0], len(self.explored_tiles) - 1) line = self.get_line(x0, y0, end_points[i][0], end_points[i][1]) lines.append(line) for line in lines: for pos in line: x = pos[0] y = pos[1] if self.dungeon.grid[y][x] in [0, 2, 3, 4, 5, 6]: self.explored_tiles[y][x] = 1 break else: self.explored_tiles[pos[1]][pos[0]] = 1 def handle_event(self, event): etype = get_event_type(event) if etype == PLAYER_DROP_ITEM: self.items.append(event.target) elif etype == PLAYER_PICKUP_ITEM: self.items.remove(event.target) def spawn_objects(self): for row in range(0, len(self.map.tiles)): for tile in range(0, len(self.map.tiles[row])): if self.map.tiles[row][tile].id == 10: self.map.tiles[row][tile].id = 1 self.map.tiles[row][tile].load_image() self.items.append(Chest((tile, row), self)) elif self.map.tiles[row][tile].id == 7: self.map.tiles[row][tile].id = 1 self.map.tiles[row][tile].load_image() self.monsters.append(Monster('test', 'warrior', (tile, row), self)) def get_item(self, x, y): items = [] for item in self.items: if item: if item.x == x and item.y == y: items.append(item) if items: return items def get_line(self, x1, y1, x2, y2): points = [] issteep = abs(y2 - y1) > abs(x2 - x1) if issteep: x1, y1 = y1, x1 x2, y2 = y2, x2 rev = False if x1 > x2: x1, x2 = x2, x1 y1, y2 = y2, y1 rev = True deltax = x2 - x1 deltay = abs(y2 - y1) error = int(deltax / 2) y = y1 ystep = None if y1 < y2: ystep = 1 else: ystep = -1 for x in range(x1, x2 + 1): if issteep: points.append((y, x)) else: points.append((x, y)) error -= deltay if error < 0: y += ystep error += deltax # Reverse the list if the coordinates were reversed if rev: points.reverse() return points
class MiniMap(Gui): def __init__(self, world): self.world = world self.tile_size = 6 self.width = 192 self.height = 192 self.camera = pygame.Rect((0, 0), (self.width, self.height)).copy() self.map = Map() self.map.load_dungeon(world.map.dungeon, True, self.tile_size) self.zoom_in_arrow = ZoomArrow(0, (174, 10)) self.zoom_in_arrow.create() self.zoom_out_arrow = ZoomArrow(1, (174, 20)) self.zoom_out_arrow.create() Gui.__init__(self, 'minimap', (4, 4), pygame.Surface((self.width, self.height)), True) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit(self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) register_handler([TIME_PASSED, pygame.MOUSEBUTTONDOWN], self.handle_event) def redraw(self): self.image.fill((0, 0, 0)) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.load_dungeon(self.world.map.dungeon, True, self.tile_size) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.zoom_in_arrow.create() self.zoom_out_arrow.create() self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit(self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) def handle_event(self, event): etype = get_event_type(event) if etype == TIME_PASSED: self.redraw() elif etype == pygame.MOUSEBUTTONDOWN and event.button in [4, 5]: if event.button == 4: self.zoom_in() elif event.button == 5: self.zoom_out() def mouse(self, mouse, event): mouse.x -= self.x mouse.y -= self.y if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if mouse.colliderect(self.zoom_in_arrow.rect): self.zoom_in() elif mouse.colliderect(self.zoom_out_arrow.rect): self.zoom_out() def zoom_in(self): if self.zoom_in_arrow.zoom_level < self.zoom_in_arrow.max_zoom_level: self.zoom_in_arrow.zoom_level += 1 self.zoom_out_arrow.zoom_level -= 1 self.tile_size += 2 self.redraw() def zoom_out(self): if self.zoom_out_arrow.zoom_level < self.zoom_out_arrow.max_zoom_level: self.zoom_out_arrow.zoom_level += 1 self.zoom_in_arrow.zoom_level -= 1 self.tile_size -= 2 self.redraw() def draw(self, screen): screen.blit(self.image, (self.x, self.y))