Esempio n. 1
0
    def __init__(self, ss, WorldSize, font, rand_seed):
        self.size = WorldSize
        #How big the map is
        self.TileSize = 32
        self.ssize = (self.size[0] / self.TileSize,
                      self.size[1] / self.TileSize)
        self.seed = rand_seed
        seed(self.seed)

        self.ss = ss
        self.w, self.h = self.size
        #Certain functions entities need this.
        self.center = vector2.Vector2(self.w / 2, self.h / 2)

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.background_over = pygame.Surface((1600, 900), pygame.HWSURFACE)
        self.background_over.set_alpha(128)
        self.background_over.fill((0, 0, 20, 128))

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock = pygame.time.Clock()

        print self.size
        self.background = pygame.Surface((self.size[0], self.size[1]),
                                         pygame.HWSURFACE)
        self.background.fill((255, 255, 255))

        self.font = font
        self.font_size = self.font.size("A")[1]

        self.text = self.font.render(str(self.wood), True, (0, 0, 0))
        #World entity also stores global font for rendering to the screen.

        self.clock_degree = 0
        #Used for the clock

        self.convert_all()
        self.new_world()
        self.cliper = Clips(self, self.ss)

        self.BuildingQueue = []
        self.buildqueue = 0
Esempio n. 2
0
    def __init__(self, ss, WorldSize, font, rand_seed):
        self.size = WorldSize
        #How big the map is
        self.TileSize = 32
        self.ssize = (
            self.size[0] / self.TileSize, self.size[1] / self.TileSize)
        self.seed = rand_seed
        seed(self.seed)

        self.ss = ss
        self.w, self.h = self.size
        #Certain functions entities need this.
        self.center = vector2.Vector2(self.w / 2, self.h / 2)

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.background_over = pygame.Surface((1600, 900),
            pygame.HWSURFACE)
        self.background_over.set_alpha(128)
        self.background_over.fill((0, 0, 20, 128))

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock = pygame.time.Clock()

        print self.size
        self.background = pygame.Surface(
            (self.size[0], self.size[1]), pygame.HWSURFACE)
        self.background.fill((255, 255, 255))

        self.font = font
        self.font_size = self.font.size("A")[1]

        self.text = self.font.render(str(self.wood), True, (0, 0, 0))
        #World entity also stores global font for rendering to the screen.

        self.clock_degree = 0
        #Used for the clock

        self.convert_all()
        self.new_world()
        self.cliper = Clips(self, self.ss)

        self.BuildingQueue = []
        self.buildqueue = 0
Esempio n. 3
0
def run():
    """Simply runs the game."""

    font = pygame.font.SysFont("Terminal", 20)
    bool_full = False
    screen_size = (1280, 720)

    screen_width, screen_height = screen_size

    side_size = screen_width / 5.0

    if bool_full:
        screen = pygame.display.set_mode(
            (1600, 900), pygame.FULLSCREEN | pygame.HWSURFACE, 32)
    else:
        screen = pygame.display.set_mode(screen_size, 0, 32)

    # For the selection
    draw = False
    held = False

    s_size = 128
    size = (s_size, s_size)

    seed = None

    world = World(size, font, seed, screen_size)

    # These are all loaded here to be used in the main file.
    # TODO: Move these somewhere else
    placing_lumberyard_img = pygame.image.load(
        "Images/Buildings/Dark_LumberYard.png").convert()
    placing_lumberyard_img.set_colorkey((255, 0, 255))

    placing_house_img = pygame.image.load(
        "Images/Buildings/Dark_House.png").convert()
    placing_house_img.set_colorkey((255, 0, 255))

    placing_dock_img = pygame.image.load(
        "Images/Buildings/Dark_Dock.png").convert()
    placing_dock_img.set_colorkey((255, 0, 255))

    placing_manor_img = pygame.image.load(
        "Images/Buildings/Dark_Manor.png").convert()
    placing_manor_img.set_colorkey((255, 0, 255))

    bad_lumberyard_img = pygame.image.load(
        "Images/Buildings/Red_LumberYard.png").convert()
    bad_lumberyard_img.set_colorkey((255, 0, 255))

    world.clipper = Clips(world, (screen_width, screen_height))
    selected_building = "LumberYard"
    selected_img = pygame.image.load(
        "Images/Buildings/Dark_LumberYard.png").convert()
    selected_img.set_colorkey((255, 0, 255))

    world.clock.tick()
    done = False
    while not done:

        time_passed_seconds = world.clock.tick_busy_loop(60) / 1000.
        pos = Vector2(*pygame.mouse.get_pos())

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.MOUSEBUTTONDOWN:
                if (pos.x > world.clipper.minimap_rect.x
                        and pos.y > world.clipper.minimap_rect.y):
                    pass
                else:
                    if event.button == 1 and selected_building is None:
                        """This determines what icon you clicked on in the building selector"""
                        held = True
                        start = Vector2(*pygame.mouse.get_pos())
                        draw = True
                        if (pos.x < world.clipper.side.w) and (
                                pos.y < world.clipper.side.top_rect.h):
                            for tile_list in world.clipper.side.tiles:
                                for tile in tile_list:
                                    if tile is None:
                                        continue

                                    if tile.rect.collidepoint((pos.x, pos.y)):
                                        if tile.selected:
                                            tile.selected = False
                                        else:
                                            tile.selected = True

                                        selected_building = tile.rep
                                        world.clipper.side.update(tile)

                        else:
                            world.clipper.side.update()

                    elif event.button == 1 and selected_building is not None:
                        if pos.x > world.clipper.side.w:
                            world.add_building(selected_building, pos)
                            if world.test_buildable(selected_building, 0, pos):
                                selected_building = None
                                world.clipper.side.update()

                    if event.button == 3:
                        selected_building = None

            if event.type == pygame.MOUSEBUTTONUP:
                draw = False
                held = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_F2 or event.key == pygame.K_F3 or \
                        event.key == pygame.K_F4:
                    str1 = str(datetime.now())
                    str1 = str1.split(".")
                    str2 = str1[0] + str1[1]
                    str2 = str2.split(":")
                    str1 = ""
                    for i in str2:
                        str1 += i

                    if event.key == pygame.K_F2:
                        pygame.image.save(
                            screen,
                            "Images/Screenshots/SCREENSHOT%s.png" % str1)

                    elif event.key == pygame.K_F3:
                        pygame.image.save(
                            world.clipper.minimap,
                            "Images/Screenshots/MinimapSCREENSHOT%s.png" %
                            str1)

                    elif event.key == pygame.K_F4:
                        pygame.image.save(
                            world.full_surface,
                            "Images/Screenshots/FULL_MAP_RENDER%s.png" % str1)

                if event.key == pygame.K_n:
                    world.new_world()

                if event.key == pygame.K_ESCAPE:
                    done = True

        # ------------------Keys Below--------------------------------------
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[pygame.K_ESCAPE]:  # quits the game
            done = True

        if pressed_keys[pygame.K_d]:  # Fast-forward function
            world.clock_degree += 5

        # Test to see what the first entity's current state is
        if pressed_keys[pygame.K_l]:
            print world.entities[0].brain.active_state

        # --------------Keys Above----------------------------------------
        # --------------Mouse Below---------------------------------------

        if int(pos.x) <= 15:
            if not bool_full:
                pygame.mouse.set_pos((15, pos.y))
            world.background_pos.x += 500 * time_passed_seconds
            if world.background_pos.x > side_size:
                world.background_pos.x = side_size

        elif int(pos.x) >= screen_width - 16:
            if not bool_full:
                pygame.mouse.set_pos((screen_width - 16, pos.y))
            world.background_pos.x -= 500 * time_passed_seconds

            if world.background_pos.x < -1 * (world.w - screen_width):
                world.background_pos.x = -1 * (world.w - screen_width)

            # print world.background_pos.x

        if int(pos.y) <= 15:
            if not bool_full:
                pygame.mouse.set_pos((pos.x, 15))
            world.background_pos.y += 500 * time_passed_seconds

            if world.background_pos.y > 0:
                world.background_pos.y = 0

        elif int(pos.y) >= screen_height - 16:

            if not bool_full:
                pygame.mouse.set_pos((pos.x, screen_height - 16))

            world.background_pos.y -= 500 * time_passed_seconds

            if world.background_pos.y < -1 * (world.h - screen_height):
                world.background_pos.y = -1 * (world.h - screen_height)

        if pygame.mouse.get_pressed()[0]:
            if pos.x > world.clipper.minimap_rect.x and pos.y > world.clipper.minimap_rect.y:
                """ If the player clicked on the mini map,
                    go to that location with the view centered on the event position"""
                draw = False
                if not held:
                    world.background_pos.x = (
                        -1 * (pos.x - world.clipper.minimap_rect.x) *
                        world.clipper.a) + (world.clipper.rect_view_w *
                                            world.clipper.a) / 2

                    world.background_pos.y = (
                        -1 * (pos.y - world.clipper.minimap_rect.y) *
                        world.clipper.b) + (world.clipper.rect_view_h *
                                            world.clipper.b) / 2

        # --------------Mouse Above---------------------------------------
        # --------------Process below-------------------------------------

        world.process(time_passed_seconds)

        if selected_building == "House":
            selected_img = placing_house_img
        elif selected_building == "LumberYard":
            selected_img = placing_lumberyard_img
        elif selected_building == "Dock":
            selected_img = placing_dock_img
        elif selected_building == "Manor":
            selected_img = placing_manor_img

        # --------------Process above-------------------------------------
        # --------------Render Below------------------------

        screen.fill((0, 0, 0))
        world.render_all(screen, time_passed_seconds, pos)

        #world.grow_trees(world.baby_tree_locations)

        if selected_building is not None:
            if (pos.x > world.clipper.minimap_rect.x
                    and pos.y > world.clipper.minimap_rect.y) or (
                        pos.x < world.clipper.side.w + 32):
                pass
            else:
                if not world.test_buildable(selected_building, 0, pos):
                    selected_img = bad_lumberyard_img
                blit_pos = world.get_tile_pos(pos - world.background_pos) * 32
                screen.blit(selected_img,
                            ((blit_pos.x - (selected_img.get_width() - 32)) +
                             world.background_pos.x,
                             (blit_pos.y - (selected_img.get_height() - 32)) +
                             world.background_pos.y))

        # ------------Render above----------------------------
        # This is for selecting-------------
        if draw and selected_building is None:
            current_mouse_pos = Vector2(*pygame.mouse.get_pos())

            lst = world.get_tile_array(start,
                                       ((current_mouse_pos.x - start.x) / 32,
                                        (current_mouse_pos.x - start.x) / 32))
            for i in lst:
                for j in i:
                    j.selected = 1

            select_surface = pygame.Surface(
                (abs(current_mouse_pos.x - start.x),
                 abs(current_mouse_pos.y - start.y)))
            select_surface.set_alpha(25)
            select_surface.fill((255, 255, 255))

            if current_mouse_pos.x - \
                    start.x <= 0 and current_mouse_pos.y < start.y and current_mouse_pos.x > start.x:
                newa = (current_mouse_pos.x - (current_mouse_pos.x - start.x),
                        current_mouse_pos.y)
                screen.blit(select_surface, (newa))
            if current_mouse_pos.x - \
                    start.x <= 0 and current_mouse_pos.y > start.y and current_mouse_pos.x < start.x:
                newa = (current_mouse_pos.x,
                        current_mouse_pos.y - (current_mouse_pos.y - start.y))
                screen.blit(select_surface, (newa))
            if current_mouse_pos.x - \
                    start.x > 0 and current_mouse_pos.y - start.y > 0:
                screen.blit(select_surface, (start))
            if current_mouse_pos.x - \
                    start.x < 0 and current_mouse_pos.y - start.y < 0:
                screen.blit(select_surface, (current_mouse_pos))
            pygame.draw.rect(screen, (255, 255, 255),
                             (start, (current_mouse_pos.x - start.x,
                                      current_mouse_pos.y - start.y)), 1)
        # Selecting Above------------------

        # --------------Render Above------------------------

        pygame.display.flip()
        pygame.display.set_caption("VillagerSim")

    pygame.quit()
def run():
    pygame.display.set_caption("Villager Simulation")

    sizes = pygame.display.list_modes()
    SCREEN_SIZE = sizes[0]
    Owidth, Oheight = SCREEN_SIZE

    side_size = Owidth / 5.0

    mapGenerator = mapGen()

    if FULL_ON:
        screen = pygame.display.set_mode(SCREEN_SIZE,
                                         pygame.FULLSCREEN | pygame.HWSURFACE,
                                         32)
    else:
        screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)

    fade = CrossFade(screen)
    all_sprites = pygame.sprite.Group(fade)

    draw = False
    held = False

    #Load the image the world will be based on,
    #then set the world size proportionate to it

    #world_img_str, mini_img_str = random_map("SCREENSHOT", "*")
    #world_img = pygame.image.load(world_img_str).convert()

    #mini_img_str = None
    #world_img = mapGenerator.whole_new(25, (256, 256))

    size = (256, 256)
    w_size = size[0] * TILE_SIZE, size[1] * TILE_SIZE

    seed = randint(0, 100)
    #print seed

    world = World.World(SCREEN_SIZE, w_size, font, seed)
    #pygame.image.save(world.minimap_img, "Images/UBER-COOL-small.png")

    Villager_image = pygame.image.load(
        "Images/Entities/Villager.PNG").convert()
    Farmer_image = pygame.image.load("Images/Entities/Farmer.png").convert()
    Lumberjack_image = pygame.image.load(
        "Images/Entities/Lumberjack.png").convert()
    Builder_image = pygame.image.load("Images/Entities/Builder.png").convert()

    placing_lumberyard_img = pygame.image.load(
        "Images/Buildings/Dark_Lumberyard.png").convert()
    placing_lumberyard_img.set_colorkey((255, 0, 255))
    placing_house_img = pygame.image.load(
        "Images/Buildings/Dark_House.png").convert()
    placing_house_img.set_colorkey((255, 0, 255))
    placing_dock_img = pygame.image.load(
        "Images/Buildings/Dark_Dock.png").convert()
    placing_dock_img.set_colorkey((255, 0, 255))
    placing_manor_img = pygame.image.load(
        "Images/Buildings/Dark_Manor.png").convert()
    placing_manor_img.set_colorkey((255, 0, 255))

    bad_lumberyard_img = pygame.image.load(
        "Images/Buildings/Red_Lumberyard.png").convert()
    bad_lumberyard_img.set_colorkey((255, 0, 255))

    #pygame.image.save(me_double_size(pygame.image.load(
    #    "Images/Buildings/LumberYard_Icon.png").convert()), "LARGERLumb.png")
    #pygame.image.save(me_double_size(pygame.image.load(
    #    "Images/Buildings/Manor_Icon.png").convert()), "LARGERMan.png")

    world.clipper = Clips(world, (Owidth, Oheight))
    #pygame.image.save(world.background, "Images/VeryLargeShowingOff.png")
    #pygame.image.save(world.minimap_img, "Images/SmallAndVariety2.png")

    selected_building = "LumberYard"
    selected_img = pygame.image.load(
        "Images/Buildings/Dark_Lumberyard.png").convert()
    selected_img.set_colorkey((255, 0, 255))

    world.clock.tick()
    while True:

        time_passed = world.clock.tick(60)
        time_passed_seconds = time_passed / 1000.
        pos = vector2.Vector2(*pygame.mouse.get_pos())

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if (pos.x > world.clipper.minimap_rect.x
                        and pos.y > world.clipper.minimap_rect.y):
                    pass
                else:
                    if event.button == 1:
                        held = True
                        start = vector2.Vector2(*pygame.mouse.get_pos())
                        draw = True
                        if (pos.x < world.clipper.side.w) and (
                                pos.y < world.clipper.side.top_rect.h):
                            for L in world.clipper.side.tiles:
                                for T in L:
                                    if T is None:
                                        continue

                                    if T.rect.collidepoint((pos.x, pos.y)):
                                        if T.selected:
                                            T.selected = False
                                        else:
                                            T.selected = True

                                        selected_building = T.rep
                                        world.clipper.side.update(T)

                        else:
                            selected_building = None
                            world.clipper.side.update()

                    if event.button == 3 and selected_building is not None:
                        world.add_building(selected_building, pos)
                        if world.test_buildable(selected_building, 0, pos):
                            selected_building = None
                            world.clipper.side.update()

            if event.type == pygame.MOUSEBUTTONUP:
                draw = False
                held = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_F2:
                    str1 = str(datetime.now())
                    str1 = str1.split(".")
                    str2 = str1[0] + str1[1]
                    str2 = str2.split(":")
                    str1 = ""
                    for i in str2:
                        str1 += i
                    pygame.image.save(
                        screen, "Images/Screenshots/SCREENSHOT%s.png" % str1)
                if event.key == pygame.K_n:
                    world.new_world()

            if event.type == pygame.VIDEORESIZE:
                Owidth, Oheight = event.size

        #------------------Keys Below------------------------------------------
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[pygame.K_ESCAPE]:
            #quits the game
            pygame.quit()
            exit()

        if pressed_keys[pygame.K_SPACE]:
            #Resets wood
            world.wood = 0

        if pressed_keys[pygame.K_d]:
            #Fast-forward-esk functionability
            world.clock_degree += 5

        if pressed_keys[pygame.K_l]:
            #Test to see what the first entity's state is
            print world.entities[1].brain.active_state

        #--------------Keys Above----------------------------------------
        #--------------Mouse Below---------------------------------------

        if int(pos.x) <= 15:
            if not FULL_ON:
                pygame.mouse.set_pos((15, pos.y))
            world.background_pos.x += 500 * time_passed_seconds
            #print world.background_pos.x
            if world.background_pos.x > side_size:

                world.background_pos.x = side_size

        elif int(pos.x) >= Owidth - 16:
            if not FULL_ON:
                pygame.mouse.set_pos((Owidth - 16, pos.y))
            world.background_pos.x -= 500 * time_passed_seconds

            if world.background_pos.x < -1 * (world.w - Owidth):
                world.background_pos.x = -1 * (world.w - Owidth)
            #print world.background_pos.x

        if int(pos.y) <= 15:
            if not FULL_ON:
                pygame.mouse.set_pos((pos.x, 15))
            world.background_pos.y += 500 * time_passed_seconds

            if world.background_pos.y > 0:
                world.background_pos.y = 0

        elif int(pos.y) >= Oheight - 16:

            if not FULL_ON:
                pygame.mouse.set_pos((pos.x, Oheight - 16))

            world.background_pos.y -= 500 * time_passed_seconds

            if world.background_pos.y < -1 * (world.h - Oheight):
                world.background_pos.y = -1 * (world.h - Oheight)

        if pygame.mouse.get_pressed()[0]:
            if (pos.x > world.clipper.minimap_rect.x
                    and pos.y > world.clipper.minimap_rect.y):
                draw = False
                if held is not True:
                    world.background_pos.x = (
                        -1 * (pos.x - world.clipper.minimap_rect.x) *
                        world.clipper.a) + (world.clipper.rect_view_w *
                                            world.clipper.a) / 2
                    world.background_pos.y = (
                        -1 * (pos.y - world.clipper.minimap_rect.y) *
                        world.clipper.b) + (world.clipper.rect_view_h *
                                            world.clipper.b) / 2

        #--------------Mouse Above---------------------------------------
        #--------------Process below-------------------------------------

        world.process(time_passed_seconds)

        if selected_building == "House":
            selected_img = placing_house_img
        elif selected_building == "LumberYard":
            selected_img = placing_lumberyard_img
        elif selected_building == "Dock":
            selected_img = placing_dock_img
        elif selected_building == "Manor":
            selected_img = placing_manor_img

        #--------------Process above-------------------------------------
        #--------------Render Below------------------------

        screen.fill((0, 0, 0))
        world.render_all(screen, time_passed_seconds, pos)

        all_sprites.clear(screen, world.background)
        all_sprites.update()
        all_sprites.draw(screen)

        world.grow_trees(world.Baby_TreeLocations)

        if fade.trans_value == 0:
            all_sprites.remove(fade)

        if selected_building is not None:
            if (pos.x > world.clipper.minimap_rect.x
                    and pos.y > world.clipper.minimap_rect.y) or (
                        pos.x < world.clipper.side.w + 32):
                pass
            else:
                if not world.test_buildable(selected_building, 0, pos):
                    selected_img = bad_lumberyard_img
                blit_pos = world.get_tile_pos(pos - world.background_pos) * 32
                screen.blit(selected_img,
                            ((blit_pos.x - (selected_img.get_width() - 32)) +
                             world.background_pos.x,
                             (blit_pos.y - (selected_img.get_height() - 32)) +
                             world.background_pos.y))

        #This is for selecting-------------
        if draw is True and selected_building is None:
            a = vector2.Vector2(*pygame.mouse.get_pos())
            lst = world.get_tile_array(start, ((a.x - start.x) / 32,
                                               (a.x - start.x) / 32))
            for i in lst:
                for j in i:
                    j.selected = 1
            s = pygame.Surface((abs(a.x - start.x), abs(a.y - start.y)))
            s.set_alpha(25)
            s.fill((255, 255, 255))
            if a.x - start.x <= 0 and a.y < start.y and a.x > start.x:
                newa = (a.x - (a.x - start.x), a.y)
                screen.blit(s, (newa))
            if a.x - start.x <= 0 and a.y > start.y and a.x < start.x:
                newa = (a.x, a.y - (a.y - start.y))
                screen.blit(s, (newa))
            if a.x - start.x > 0 and a.y - start.y > 0:
                screen.blit(s, (start))
            if a.x - start.x < 0 and a.y - start.y < 0:
                screen.blit(s, (a))
            pygame.draw.rect(screen, (255, 255, 255),
                             (start, (a.x - start.x, a.y - start.y)), 1)
        #Selecting Above------------------

        #--------------Render Above------------------------

        pygame.display.flip()
Esempio n. 5
0
class World(object):  # Class that stores basically EVERYTHING

    def __init__(self, ss, WorldSize, font, rand_seed, surface):
        self.DrawSurface = surface
        # print self.DrawSurface
        self.size = WorldSize  # How big the map is
        self.TileSize = 32
        self.ssize = (
            self.size[0] /
            self.TileSize,
            self.size[1] /
            self.TileSize)
        self.seed = rand_seed
        if self.seed is not None:
            seed(self.seed)

        self.ss = ss
        self.w, self.h = self.size  # Certain functions entities need this.
        self.center = Vector2(self.w / 2, self.h / 2)

        self.building = {}
        self.entities = {}  # Stores all entities the game processes
        # Each entity is given a unique id so the program can find it
        self.entity_id = 0
        self.wood = 0  # Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = self.center.copy()

        self.mapGenerator = mapGen()

        self.background_over = pygame.Surface((1600, 900), HWSURFACE)
        self.background_over.set_alpha(128)
        self.background_over.fill((0, 0, 20, 128))
        self.full_map_shadow = False #Controls if the full map will have hard shadows

        self.full_surface = pygame.Surface(self.size, HWSURFACE)

        self.clock = pygame.time.Clock()

        self.shadowDown = 3.0 / ((self.size[0] / self.TileSize) / 128.0)
        print "Shadow Down", self.shadowDown

        self.background = pygame.Surface(
            (self.size[0], self.size[1]), HWSURFACE)
        self.background.fill((255, 255, 255))

        self.font = font
        self.font_size = self.font.size("A")[1]

        self.text = self.font.render(str(self.wood), True,
                                     (0, 0, 0))  # World entity also stores global font for rendering to the screen.

        self.clock_degree = 0  # Used for the clock

        self.convert_all()
        self.new_world()
        self.cliper = Clips(self, self.ss)

        self.BuildingQueue = []
        self.buildqueue = 0

    def new_world(self):
        del self.full_surface
        # seed(self.seed)
        vorMap = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(
                self.ssize,
                num_p=23))

        self.map_width = self.map_height = len(vorMap)

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [
            [0 for i in xrange(self.map_width)] for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.baby_tree_locations = {}

        self.buildings = {"LumberYard": {},
                          "Dock": {},
                          "House": {},
                          "Manor": {},
                          "UC": {}}

        self.building = {}
        self.entities = {}  # Stores all entities the game processes
        # Each entity is given a unique id so the program can find it
        self.entity_id = 0
        self.wood = 0  # Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, HWSURFACE)

        self.clock_degree = 0  # Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = vorMap[i][a]
                color = f_color
                to_rotate = 1

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0
                    to_rotate = False

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.grass_img)
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160

                    tile = Tile.TreePlantedTile_w(self, self.tree_img)
                    tile.location = Vector2(i << 5, a << 5)
                    tile.rect.topleft = tile.location
                    tile.id = self.TreeID

                    self.TreeLocations[str(self.TreeID)] = tile.location
                    self.TreeID += 1

                    to_rotate = False

                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.grass_img)
                    last_color = 170

                elif color >= 190 and color < 220:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    to_rotate = False
                    last_color = 190

                else:
                    colorb = 220
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_color = 220

                # Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - self.shadowDown:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= self.shadowDown

                else:
                    self.current_height -= self.shadowDown
                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(128)
                # -----------

                tile.location = Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(
                        tile.img,
                        randint(
                            0,
                            4) *
                        90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 220 - color
                if color >= 190 and color < 220:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                if self.full_map_shadow:
                    self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)

                # self.minimap_img.blit(combined_img.subsurface((0,0,1,1)), (i,a))
                self.minimap_img.blit(
                    tile.img.subsurface(
                        (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(
                    dark_surface.subsurface(
                        (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(
                    dark_surface2.subsurface(
                        (0, 0, 1, 1)), (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)

    def populate(self):
        FARMER_COUNT = 5
        VILLAGER_COUNT = 5
        BUILDER_COUNT = 1

        # adds all the people and make sure they don't go all ape shit
        lumber1 = LumberYard(self, self.lumberyard_img)
        lumber1.location = Vector2(self.w / 2, self.h / 2)
        lumber1.tile_x, lumber1.tile_y = 4, 4
        self.add_entity(lumber1)

        for Villager_no in xrange(VILLAGER_COUNT):  # Adds all Wood Cutters
            villager = Lumberjack(self, self.lumberjack_img)
            villager.location = lumber1.location.copy()
            villager.LastLumberYard = lumber1
            villager.brain.set_state("Searching")
            self.add_entity(villager)
            self.population += 1

        for Building_no in xrange(BUILDER_COUNT):
            builder = Builder(self, self.builder_img, lumber1)
            builder.location = lumber1.location.copy()
            builder.brain.set_state("Idle")
            self.add_entity(builder)
            self.population += 1

        for FARMER in xrange(FARMER_COUNT):  # Adds all the farmers
            farmer = Farmer(self, self.farmer_img)
            farmer.location = lumber1.location.copy()
            farmer.brain.set_state("Planting")
            self.add_entity(farmer)
            self.population += 1

    def add_building(self, building, pos):

        buildable = self.test_buildable(building, 0, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = self.get_tile_pos(pos - self.background_pos) * 32
            print "LOC: ", Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            self.BuildingQueue.append(Build)
            return 1

    def add_built(self, building, pos):

        buildable = self.test_buildable(building, 1, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = pos
            print "LOC2: ", Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            return 1

    def test_buildable(self, building, built, pos):

        if building == "LumberYard":
            if built:
                Build = LumberYard(self, self.lumberyard_img)
            else:
                Build = UnderConstruction(self, self.uc_img, "LumberYard")

        elif building == "House":
            if built:
                Build = House(self, self.house_img)
            else:
                Build = UnderConstruction(self, self.uc_house_img, "House")

        elif building == "Dock":
            if built:
                Build = Dock(self, self.dock_img)
            else:
                Build = UnderConstruction(self, self.ucw_img, "Dock")

        elif building == "Manor":
            if built:
                Build = Manor(self, self.manor_img)
            else:
                Build = UnderConstruction(self, self.uc_img, "Manor")

        buildable = 1
        land = 0
        water = 0

        Twidth, Theight = Build.image.get_size()
        for i in range(Twidth >> 5):
            for j in range(Theight >> 5):
                try:
                    if built:
                        test_tile = self.get_tile(
                            Vector2((pos.x - 32) + (i << 5), (pos.y - 32) + (j << 5)))
                        # print "A", test_tile, test_tile.location
                    else:
                        test_tile = self.get_tile(Vector2(((pos.x - 32) - self.background_pos.x) + (i << 5),
                                                          ((pos.y - 32) - self.background_pos.y) + (j >> 5)))
                        # print "B", test_tile, test_tile.location

                    if test_tile.buildable != 1 and building != "Dock":
                        buildable = 0
                        return 0
                    elif building == "Dock":
                        if test_tile.buildable_w:
                            water += 1
                        else:
                            land += 1
                except IndexError:
                    return 0

        if building == "Dock":
            if water >= 1 and land <= 2 and land > 0:
                buildable = 1
                return 1, Build
            else:
                buildable = 0
                return 0

        return 1, Build

    def convert_all(self):
        self.lush_grass_img = lush_grass_img.convert()
        self.grass_img = grass_img.convert()
        self.water_img = water_img.convert()
        self.sand_img = sand_img.convert()
        self.cobble_img = cobble_img.convert()
        self.SStone_img = SStone_img.convert()
        self.deepwater_img = deepwater_img.convert()
        self.snow_img = snow_img.convert()
        self.tree_img = tree_img.convert()

        self.lumberyard_img = lumber_yard_img.convert()
        self.lumberyard_img.set_colorkey((255, 0, 255))

        self.house_img = house_img.convert()
        self.house_img.set_colorkey((255, 0, 255))

        self.dock_img = dock_img.convert()
        self.dock_img.set_colorkey((255, 0, 255))

        self.manor_img = manor_img.convert()
        self.manor_img.set_colorkey((255, 0, 255))

        self.uc_img = uc_img.convert_alpha()
        self.ucw_img = ucw_img.convert()
        self.ucw_img.set_colorkey((255, 0, 255))
        self.uc_house_img = uc_house_img.convert()
        self.uc_house_img.set_colorkey((255, 0, 255))

        self.lumberjack_img = lumberjack_img.convert()
        self.farmer_img = farmer_img.convert()
        self.builder_img = builder_img.convert()

    def grow_trees(self, trees):
        for i in range(len(trees)):
            ran = randint(0, 100)
            if ran == 1 and len(trees) > 0:
                try:
                    a = trees.keys()
                    old_tile = self.get_tile(trees[a[i]])
                    darkness = pygame.Surface((32, 32))
                    darkness.set_alpha(old_tile.darkness)

                    new_tile = TreePlantedTile_w(self, tree_img)

                    new_tile.darkness = old_tile.darkness

                    new_tile.location = old_tile.location
                    new_tile.rect.topleft = new_tile.location
                    new_tile.color = old_tile.color

                    new_tile.id = self.TreeID
                    self.TreeID += 1

                    self.TileArray[
                        int(new_tile.location.y / 32)][int(new_tile.location.x / 32)] = new_tile
                    self.background.blit(new_tile.img, new_tile.location)
                    self.background.blit(darkness, new_tile.location)
                    # print self.baby_tree_locations
                    del self.baby_tree_locations[str(a[i])]
                except IndexError:
                    pass

    def add_entity(self, entity):  # Used to add entities to the world

        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1

    def remove_entity(self, entity):  # function for removing an entity
        del self.entities[entity.id]

    def remove_tree(self, tree_id):
        # print len(self.TreeLocations)
        try:
            del self.TreeLocations[str(tree_id)]
            return 0
        except KeyError:
            return None

    def get(self, entity_id):  # Return an entity

        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None

    def process(self, time_passed):  # Run the world through 1 cycle

        for entity in self.entities.values():
            entity.process(time_passed)

        self.wood_text = self.font.render("Wood: %d/%d" % (self.wood, self.MAXwood), True, (255, 255, 255))
        self.food_text = self.font.render("Food: %d/%d" % (self.food, self.MAXfood), True, (255, 255, 255))
        self.pop_text = self.font.render("Population: %d/%d" % (self.population, self.MAXpopulation), True, (255, 255, 255))
        self.frame_text = self.font.render("FPS: %.2f" % (self.clock.get_fps()), True, (255, 255, 255))

        semi_angle = abs(self.clock_degree - 180.0)
        self.background_alpha = min((255 - (255 * (abs(semi_angle / 180)))), 220.0)
        self.background_over.set_alpha(self.background_alpha)

    def render(self, surface):
        surface.blit(self.background, self.background_pos)

        for entity in self.entities.itervalues():
            entity.render(surface)

        surface.blit(self.background_over, (0, 0))

    def render_all(self, surface, tp, mouse_pos):
        self.cliper.render(surface, tp, mouse_pos)

    def get_close_entity(self, name, location, range=100.):

        location = Vector2(*location)

        for entity in self.entities.itervalues():
            if entity.name == name:
                distance = location.get_distance_to(entity.location)
                if range == -1:
                    return entity
                if distance < range:
                    return entity

        return None

    def get_tile(self, location):
        tile = self.get_tile_pos(location)

        return self.TileArray[int(tile.y)][int(tile.x)]

    def get_tile_pos(self, location):
        return Vector2(int(location.x) >> 5, int(location.y) >> 5)

    def get_tile_array(self, start_pos, dimensions):
        dimensions = (int(dimensions[0]), int(dimensions[1]))

        start_tile = self.get_tile_pos(start_pos)

        array = [[None for i in xrange((dimensions[0] * 2) + 1)]
                 for a in xrange((dimensions[1] * 2) + 1)]

        for i in xrange((dimensions[0] * 2) + 1):
            for a in xrange((dimensions[1] * 2) + 1):
                if start_tile.x + i < 0 or start_tile.y + a < 0:
                    continue

                else:
                    try:
                        array[a][i] = self.TileArray[int((start_tile.y + a) - 1)][int((start_tile.x + i) - 1)]
                    except IndexError:
                        print a, i, start_tile
                        raise IndexError
        return array

    def get_vnn_array(self, location, r):
        """ Stands for Von Neumann Neighborhood.
            Simply returns a neighborhood based
            on the initial location and range r"""
        return_array = []
        for row_number in range((2 * r) - 1):
            if row_number >= r:
                num_in_row = (2 * row_number) - (4 * (row_number - r + 1) - 1)
            else:
                num_in_row = (2 * row_number) + 1

            for cell in range(num_in_row):

                new_location = (location.x + (cell - math.floor(num_in_row / 2.0)), location.y + (row_number - (r - 1)))
                return_array.append(Vector2(*new_location))

        return return_array
Esempio n. 6
0
    def new_world(self):
        del self.full_surface
        # seed(self.seed)
        vorMap = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(
                self.ssize,
                num_p=23))

        self.map_width = self.map_height = len(vorMap)

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [
            [0 for i in xrange(self.map_width)] for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.baby_tree_locations = {}

        self.buildings = {"LumberYard": {},
                          "Dock": {},
                          "House": {},
                          "Manor": {},
                          "UC": {}}

        self.building = {}
        self.entities = {}  # Stores all entities the game processes
        # Each entity is given a unique id so the program can find it
        self.entity_id = 0
        self.wood = 0  # Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, HWSURFACE)

        self.clock_degree = 0  # Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = vorMap[i][a]
                color = f_color
                to_rotate = 1

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0
                    to_rotate = False

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.grass_img)
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160

                    tile = Tile.TreePlantedTile_w(self, self.tree_img)
                    tile.location = Vector2(i << 5, a << 5)
                    tile.rect.topleft = tile.location
                    tile.id = self.TreeID

                    self.TreeLocations[str(self.TreeID)] = tile.location
                    self.TreeID += 1

                    to_rotate = False

                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.grass_img)
                    last_color = 170

                elif color >= 190 and color < 220:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    to_rotate = False
                    last_color = 190

                else:
                    colorb = 220
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_color = 220

                # Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - self.shadowDown:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= self.shadowDown

                else:
                    self.current_height -= self.shadowDown
                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(128)
                # -----------

                tile.location = Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(
                        tile.img,
                        randint(
                            0,
                            4) *
                        90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 220 - color
                if color >= 190 and color < 220:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                if self.full_map_shadow:
                    self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)

                # self.minimap_img.blit(combined_img.subsurface((0,0,1,1)), (i,a))
                self.minimap_img.blit(
                    tile.img.subsurface(
                        (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(
                    dark_surface.subsurface(
                        (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(
                    dark_surface2.subsurface(
                        (0, 0, 1, 1)), (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)
Esempio n. 7
0
class World(object):
    """Class that stores basically EVERYTHING
    """

    def __init__(self, ss, WorldSize, font, rand_seed):
        self.size = WorldSize
        #How big the map is
        self.TileSize = 32
        self.ssize = (
            self.size[0] / self.TileSize, self.size[1] / self.TileSize)
        self.seed = rand_seed
        seed(self.seed)

        self.ss = ss
        self.w, self.h = self.size
        #Certain functions entities need this.
        self.center = vector2.Vector2(self.w / 2, self.h / 2)

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.background_over = pygame.Surface((1600, 900),
            pygame.HWSURFACE)
        self.background_over.set_alpha(128)
        self.background_over.fill((0, 0, 20, 128))

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock = pygame.time.Clock()

        print self.size
        self.background = pygame.Surface(
            (self.size[0], self.size[1]), pygame.HWSURFACE)
        self.background.fill((255, 255, 255))

        self.font = font
        self.font_size = self.font.size("A")[1]

        self.text = self.font.render(str(self.wood), True, (0, 0, 0))
        #World entity also stores global font for rendering to the screen.

        self.clock_degree = 0
        #Used for the clock

        self.convert_all()
        self.new_world()
        self.cliper = Clips(self, self.ss)

        self.BuildingQueue = []
        self.buildqueue = 0

    def new_world(self):
        del self.full_surface
        #seed(self.seed)
        img = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(self.ssize, num_p=23))
        #img = self.mapGenerator.whole_new(25, self.ssize, 1, -1)
        self.map_width, self.map_height = img.get_size()

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [[0 for i in xrange(
            self.map_width)] for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.Baby_TreeLocations = {}

        self.buildings = {"LumberYard": {},
                          "Dock": {},
                          "House": {},
                          "Manor": {},
                          "UC": {}}

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock_degree = 0
        #Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = img.get_at((i, a))
                color = f_color[0]

                to_rotate = 1

                color2 = (255, 0, 220)

#                 if color < 95:
#                     colorb = 0
#                     tile = DeepWaterTile(self, self.deepwater_img)

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_image = self.sand_img
                    #to_rotate = 0
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_image = self.sand_img
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.grass_img
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160
                    if color2[2] == 220:

                        tile = Tile.TreePlantedTile_w(self, self.WithTree_img)
                        tile.location = vector2.Vector2(i << 5, a << 5)
                        tile.rect.topleft = tile.location
                        tile.id = self.TreeID

                        self.TreeLocations[str(self.TreeID)] = tile.location
                        self.TreeID += 1

                        to_rotate = 0
                    else:
                        tile = Tile.TreePlantedTile(self, self.tree_img)

                    last_image = self.tree_img
                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.WithTree_img
                    last_color = 170

                elif color >= 190 and color < 236:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    last_image = self.tree_img
                    to_rotate = 0
                    last_color = 190

                else:
                    colorb = 236
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_image = self.SStone_img
                    last_color = 236

                #Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - 3:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= 3

                else:
                    self.current_height -= 3
                    dark_surface = pygame.Surface((1, 1))
                    dark_surface.set_alpha(128)
                #-----------

                #Used for determening start position
                if color2[1] == 255:
                    WORLD_START_POS = (i, a)
                #-----

                tile.location = vector2.Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(
                        tile.img, randint(0, 4) * 90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 235 - color
                if color >= 190 and color < 236:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)
                """
                try:
                    percentage = (color-last_color) / float(
                        colorb - last_color)

                except ZeroDivisionError:
                    percentage = 0.0
                combined_img = self.mapGenerator.lerp_two_images(tile.img,
                 last_image, percentage)
                """

                #self.minimap_img.blit(combined_img.subsurface(
                #   (0,0,1,1)), (i,a))
                self.minimap_img.blit(tile.img.subsurface(
                    (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(dark_surface.subsurface(
                    (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(dark_surface2.subsurface(
                    (0, 0, 1, 1)), (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)

    def populate(self):
        """adds all the people and make sure they don't go all ape shit
        """
        FARMER_COUNT = 4
        VILLAGER_COUNT = 5
        BUILDER_COUNT = 1

        lumber1 = Building.LumberYard(self, self.lumberyard_img)
        lumber1.location = vector2.Vector2(self.w / 2, self.h / 2)
        lumber1.tile_x, lumber1.tile_y = 4, 4
        self.add_entity(lumber1)

        for Villager_no in xrange(VILLAGER_COUNT):
            """Adds all Wood Cutters
            """
            villager = Lumberjack.Lumberjack(self, self.lumberjack_img)
            villager.location = lumber1.location.copy()
            villager.LastLumberYard = lumber1
            villager.brain.set_state("Searching")
            self.add_entity(villager)
            self.population += 1

        for Building_no in xrange(BUILDER_COUNT):
            builder = Builder.Builder(self, self.builder_img, lumber1)
            builder.location = lumber1.location.copy()
            builder.brain.set_state("Idle")
            self.add_entity(builder)
            self.population += 1

        for FARMER in xrange(FARMER_COUNT):
            """Adds all the farmers
            """
            farmer = Farmer.Farmer(self, self.farmer_img)
            farmer.location = vector2.Vector2(20, 20)
            farmer.brain.set_state("Planting")
            self.add_entity(farmer)
            self.population += 1

    def add_building(self, building, pos):

        buildable = self.test_buildable(building, 0, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = self.get_tile_pos(pos - self.background_pos) * 32
            print "add_building LOC: ", building, Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            self.BuildingQueue.append(Build)
            return 1

    def add_built(self, building, pos):

        buildable = self.test_buildable(building, 1, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = pos
            print "buildable LOC2: ", building, Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            return 1

    def test_buildable(self, building, built, pos):

        if building == "LumberYard":
            if built:
                Build = Building.LumberYard(self, self.lumberyard_img)
            else:
                Build = Building.UnderConstruction(self, self.uc_img,
                    "LumberYard")

        elif building == "House":
            if built:
                Build = Building.House(self, self.house_img)
            else:
                Build = Building.UnderConstruction(self,
                    self.uc_house_img, "House")

        elif building == "Dock":
            if built:
                Build = Building.Dock(self, self.dock_img)
            else:
                Build = Building.UnderConstruction(self, self.ucw_img, "Dock")

        elif building == "Manor":
            if built:
                Build = Building.Manor(self, self.manor_img)
            else:
                Build = Building.UnderConstruction(self, self.uc_img, "Manor")

        buildable = 1
        land = 0
        water = 0

        Twidth, Theight = Build.image.get_size()
        for i in range(Twidth >> 5):
            for j in range(Theight >> 5):
                try:
                    if built:
                        test_tile = self.get_tile(vector2.Vector2(
                            (pos.x - 32) + (i << 5), (pos.y - 32) + (j << 5)))
                        #print "A", test_tile, test_tile.location
                    else:
                        test_tile = self.get_tile(vector2.Vector2(
                           ((pos.x - 32) - self.background_pos.x) + (i << 5), (
                           (pos.y - 32) - self.background_pos.y) + (j >> 5)))
                        #print "B", test_tile, test_tile.location

                    if test_tile.buildable != 1 and building != "Dock":
                        buildable = 0
                        return 0
                    elif building == "Dock":
                        if test_tile.buildable_w:
                            water += 1
                        elif test_tile.buildable:
                            land += 1
                except IndexError:
                    print 'IndexError'
                    return 0

        if building == "Dock" and not built:
            if water >= 1 and land <= 2 and land > 0:
                #print ('buildable', water, land)
                buildable = 1
                return 1, Build
            else:
                #print ('not buildable', water, land)
                buildable = 0
                return 0

        return 1, Build

    def convert_all(self):
        self.grass_img = grass_img.convert()
        self.tree_img = tree_img.convert()
        self.water_img = water_img.convert()
        self.sand_img = sand_img.convert()
        self.cobble_img = cobble_img.convert()
        self.SStone_img = SStone_img.convert()
        self.deepwater_img = deepwater_img.convert()
        self.snow_img = snow_img.convert()
        self.WithTree_img = WithTree_img.convert()

        self.lumberyard_img = lumber_yard_img.convert()
        self.lumberyard_img.set_colorkey((255, 0, 255))

        self.house_img = house_img.convert()
        self.house_img.set_colorkey((255, 0, 255))

        self.dock_img = dock_img.convert()
        self.dock_img.set_colorkey((255, 0, 255))

        self.manor_img = manor_img.convert()
        self.manor_img.set_colorkey((255, 0, 255))

        self.uc_img = uc_img.convert_alpha()
        self.ucw_img = ucw_img.convert()
        self.ucw_img.set_colorkey((255, 0, 255))
        self.uc_house_img = uc_house_img.convert()
        self.uc_house_img.set_colorkey((255, 0, 255))

        self.lumberjack_img = lumberjack_img.convert()
        self.farmer_img = farmer_img.convert()
        self.builder_img = builder_img.convert()

    def grow_trees(self, trees):
        for i in range(len(trees)):
            ran = randint(0, 100)
            if ran == 1 and len(trees) > 0:
                try:
                    a = trees.keys()
                    old_tile = self.get_tile(trees[a[i]])
                    darkness = pygame.Surface((32, 32))
                    darkness.set_alpha(old_tile.darkness)

                    new_tile = Tile.TreePlantedTile_w(self, WithTree_img)

                    new_tile.darkness = old_tile.darkness

                    new_tile.location = old_tile.location
                    new_tile.rect.topleft = new_tile.location
                    new_tile.color = old_tile.color

                    new_tile.id = self.TreeID
                    self.TreeID += 1

                    self.TileArray[int(new_tile.location.y / 32)][int(
                        new_tile.location.x / 32)] = new_tile
                    self.background.blit(new_tile.img, new_tile.location)
                    self.background.blit(darkness, new_tile.location)
                    #print self.Baby_TreeLocations
                    del self.Baby_TreeLocations[str(a[i])]
                except IndexError:
                    pass

    def add_entity(self, entity):
        """Used to add entities to the world
        """
        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1

    def remove_entity(self, entity):
        """function for removing an entity
        """
        try:
            del self.entities[entity.id]
        except KeyError:
            pass

    def remove_tree(self, tree_id):
        #print len(self.TreeLocations)
        try:
            del self.TreeLocations[str(tree_id)]
            return 0
        except KeyError:
            return None

    def get(self, entity_id):
        #Return an entity

        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None

    def process(self, time_passed):
        #Run the world through 1 cycle

        for entity in self.entities.values():
            entity.process(time_passed)

        self.wood_text = self.font.render("Wood: %d/%d" % (
            self.wood, self.MAXwood), True, (255, 255, 255))
        self.food_text = self.font.render("Food: %d/%d" % (
            self.food, self.MAXfood), True, (255, 255, 255))
        self.pop_text = self.font.render("Population: %d/%d" % (
           self.population, self.MAXpopulation), True, (255, 255, 255))
        self.frame_text = self.font.render("FPS: %.2f" % (
           self.clock.get_fps()), True, (255, 255, 255))

        semi_angle = abs(self.clock_degree - 180.0)
        self.background_alpha = min((255 - (255 * (
                                    abs(semi_angle / 180)))), 220.0)
        self.background_over.set_alpha(self.background_alpha)

    def render(self, surface):
        surface.blit(self.background, self.background_pos)

        #for i in self.tilearray:
         #   for tile in i:
          #      tile.render(surface)

        for entity in self.entities.itervalues():
            entity.render(surface)

        surface.blit(self.background_over, (0, 0))

        #for point in self.pond_points:
        #    pygame.draw.circle(surface, (255,0,0), (
        #        int(point[0]), int(point[1])), 5)

#         surface.set_clip(0,0,self.w,self.font_size+4)
#         surface.blit(self.wood_text, (40,2))
#         surface.blit(self.food_text, (200,2))
#         surface.blit(self.pop_text, (360,2))
#         surface.set_clip(None)

    def render_all(self, surface, tp, mouse_pos):
        self.cliper.render(surface, tp, mouse_pos)

    def get_close_entity(self, name, location, range=100.):

        location = vector2.Vector2(*location)

        for entity in self.entities.itervalues():
            if entity.name == name:
                distance = location.get_distance_to(entity.location)
                if range == -1:
                    return entity
                if distance < range:
                    return entity

        return None

    def get_tile(self, location):
        tile = self.get_tile_pos(location)

        return self.TileArray[int(tile.y)][int(tile.x)]

    def get_tile_pos(self, location):
        return vector2.Vector2(int(location.x) >> 5, int(location.y) >> 5)

    def get_tile_array(self, start_pos, dimensions):
        dimensions = (int(dimensions[0]), int(dimensions[1]))

        start_tile = self.get_tile_pos(start_pos)

        array = [[None for i in xrange(
           (dimensions[0] * 2) + 1)] for a in xrange((dimensions[1] * 2) + 1)]

        for i in xrange((dimensions[0] * 2) + 1):
            for a in xrange((dimensions[1] * 2) + 1):
                if start_tile.x + i < 0 or start_tile.y + a < 0:
                    continue
                else:
                    array[a][i] = self.TileArray[int(
                       (start_tile.y + a) - 1)][int((start_tile.x + i) - 1)]

        return array
Esempio n. 8
0
    def new_world(self):
        del self.full_surface
        #seed(self.seed)
        img = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(self.ssize, num_p=23))
        #img = self.mapGenerator.whole_new(25, self.ssize, 1, -1)
        self.map_width, self.map_height = img.get_size()

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [[0 for i in xrange(
            self.map_width)] for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.Baby_TreeLocations = {}

        self.buildings = {"LumberYard": {},
                          "Dock": {},
                          "House": {},
                          "Manor": {},
                          "UC": {}}

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock_degree = 0
        #Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = img.get_at((i, a))
                color = f_color[0]

                to_rotate = 1

                color2 = (255, 0, 220)

#                 if color < 95:
#                     colorb = 0
#                     tile = DeepWaterTile(self, self.deepwater_img)

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_image = self.sand_img
                    #to_rotate = 0
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_image = self.sand_img
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.grass_img
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160
                    if color2[2] == 220:

                        tile = Tile.TreePlantedTile_w(self, self.WithTree_img)
                        tile.location = vector2.Vector2(i << 5, a << 5)
                        tile.rect.topleft = tile.location
                        tile.id = self.TreeID

                        self.TreeLocations[str(self.TreeID)] = tile.location
                        self.TreeID += 1

                        to_rotate = 0
                    else:
                        tile = Tile.TreePlantedTile(self, self.tree_img)

                    last_image = self.tree_img
                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.WithTree_img
                    last_color = 170

                elif color >= 190 and color < 236:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    last_image = self.tree_img
                    to_rotate = 0
                    last_color = 190

                else:
                    colorb = 236
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_image = self.SStone_img
                    last_color = 236

                #Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - 3:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= 3

                else:
                    self.current_height -= 3
                    dark_surface = pygame.Surface((1, 1))
                    dark_surface.set_alpha(128)
                #-----------

                #Used for determening start position
                if color2[1] == 255:
                    WORLD_START_POS = (i, a)
                #-----

                tile.location = vector2.Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(
                        tile.img, randint(0, 4) * 90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 235 - color
                if color >= 190 and color < 236:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)
                """
                try:
                    percentage = (color-last_color) / float(
                        colorb - last_color)

                except ZeroDivisionError:
                    percentage = 0.0
                combined_img = self.mapGenerator.lerp_two_images(tile.img,
                 last_image, percentage)
                """

                #self.minimap_img.blit(combined_img.subsurface(
                #   (0,0,1,1)), (i,a))
                self.minimap_img.blit(tile.img.subsurface(
                    (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(dark_surface.subsurface(
                    (0, 0, 1, 1)), (i, a))
                self.minimap_img.blit(dark_surface2.subsurface(
                    (0, 0, 1, 1)), (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)
Esempio n. 9
0
    def new_world(self):
        del self.full_surface
        #seed(self.seed)
        img = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(self.ssize, num_p=23))
        #img = self.mapGenerator.whole_new(25, self.ssize, 1, -1)
        self.map_width, self.map_height = img.get_size()

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [[0 for i in xrange(self.map_width)]
                          for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.Baby_TreeLocations = {}

        self.buildings = {
            "LumberYard": {},
            "Dock": {},
            "House": {},
            "Manor": {},
            "UC": {}
        }

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock_degree = 0
        #Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = img.get_at((i, a))
                color = f_color[0]

                to_rotate = 1

                color2 = (255, 0, 220)

                #                 if color < 95:
                #                     colorb = 0
                #                     tile = DeepWaterTile(self, self.deepwater_img)

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_image = self.sand_img
                    #to_rotate = 0
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_image = self.sand_img
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.grass_img
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160
                    if color2[2] == 220:

                        tile = Tile.TreePlantedTile_w(self, self.WithTree_img)
                        tile.location = vector2.Vector2(i << 5, a << 5)
                        tile.rect.topleft = tile.location
                        tile.id = self.TreeID

                        self.TreeLocations[str(self.TreeID)] = tile.location
                        self.TreeID += 1

                        to_rotate = 0
                    else:
                        tile = Tile.TreePlantedTile(self, self.tree_img)

                    last_image = self.tree_img
                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.WithTree_img
                    last_color = 170

                elif color >= 190 and color < 236:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    last_image = self.tree_img
                    to_rotate = 0
                    last_color = 190

                else:
                    colorb = 236
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_image = self.SStone_img
                    last_color = 236

                #Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - 3:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= 3

                else:
                    self.current_height -= 3
                    dark_surface = pygame.Surface((1, 1))
                    dark_surface.set_alpha(128)
                #-----------

                #Used for determening start position
                if color2[1] == 255:
                    WORLD_START_POS = (i, a)
                #-----

                tile.location = vector2.Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(tile.img,
                                                       randint(0, 4) * 90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 235 - color
                if color >= 190 and color < 236:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)
                """
                try:
                    percentage = (color-last_color) / float(
                        colorb - last_color)

                except ZeroDivisionError:
                    percentage = 0.0
                combined_img = self.mapGenerator.lerp_two_images(tile.img,
                 last_image, percentage)
                """

                #self.minimap_img.blit(combined_img.subsurface(
                #   (0,0,1,1)), (i,a))
                self.minimap_img.blit(tile.img.subsurface((0, 0, 1, 1)),
                                      (i, a))
                self.minimap_img.blit(dark_surface.subsurface((0, 0, 1, 1)),
                                      (i, a))
                self.minimap_img.blit(dark_surface2.subsurface((0, 0, 1, 1)),
                                      (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)
Esempio n. 10
0
class World(object):
    """Class that stores basically EVERYTHING
    """
    def __init__(self, ss, WorldSize, font, rand_seed):
        self.size = WorldSize
        #How big the map is
        self.TileSize = 32
        self.ssize = (self.size[0] / self.TileSize,
                      self.size[1] / self.TileSize)
        self.seed = rand_seed
        seed(self.seed)

        self.ss = ss
        self.w, self.h = self.size
        #Certain functions entities need this.
        self.center = vector2.Vector2(self.w / 2, self.h / 2)

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.background_over = pygame.Surface((1600, 900), pygame.HWSURFACE)
        self.background_over.set_alpha(128)
        self.background_over.fill((0, 0, 20, 128))

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock = pygame.time.Clock()

        print self.size
        self.background = pygame.Surface((self.size[0], self.size[1]),
                                         pygame.HWSURFACE)
        self.background.fill((255, 255, 255))

        self.font = font
        self.font_size = self.font.size("A")[1]

        self.text = self.font.render(str(self.wood), True, (0, 0, 0))
        #World entity also stores global font for rendering to the screen.

        self.clock_degree = 0
        #Used for the clock

        self.convert_all()
        self.new_world()
        self.cliper = Clips(self, self.ss)

        self.BuildingQueue = []
        self.buildqueue = 0

    def new_world(self):
        del self.full_surface
        #seed(self.seed)
        img = self.mapGenerator.negative(
            self.mapGenerator.reallyCoolFull(self.ssize, num_p=23))
        #img = self.mapGenerator.whole_new(25, self.ssize, 1, -1)
        self.map_width, self.map_height = img.get_size()

        self.minimap_img = pygame.Surface((self.map_width, self.map_height))

        self.TileArray = [[0 for i in xrange(self.map_width)]
                          for a in xrange(self.map_height)]

        self.TreeID = 0
        self.TreeLocations = {}
        self.Baby_TreeID = 0
        self.Baby_TreeLocations = {}

        self.buildings = {
            "LumberYard": {},
            "Dock": {},
            "House": {},
            "Manor": {},
            "UC": {}
        }

        self.building = {}
        self.entities = {}
        #Stores all entities the game processes
        self.entity_id = 0
        #Each entity is given a unique id so the program can find it
        self.wood = 0
        #Probably will add other resources
        self.MAXwood = 50
        self.food = 0
        self.MAXfood = 0
        self.population = 0
        self.MAXpopulation = 15
        self.background_pos = vector2.Vector2(self.ss[0] / 5.0, 0)
        self.mapGenerator = mapGen()

        self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)

        self.clock_degree = 0
        #Used for the clock

        self.BuildingQueue = []
        self.buildqueue = 0

        for i in xrange(self.map_width):
            self.current_height = 0
            w_last = False
            for a in xrange(self.map_height):

                f_color = img.get_at((i, a))
                color = f_color[0]

                to_rotate = 1

                color2 = (255, 0, 220)

                #                 if color < 95:
                #                     colorb = 0
                #                     tile = DeepWaterTile(self, self.deepwater_img)

                if color < 110:
                    colorb = 0
                    tile = Tile.WaterTile(self, self.water_img)

                    last_image = self.sand_img
                    last_color = 0

                elif color >= 110 and color < 120:
                    colorb = 110
                    tile = Tile.BeachTile(self, self.sand_img)
                    last_image = self.sand_img
                    #to_rotate = 0
                    last_color = 110

                elif color >= 120 and color < 140:
                    colorb = 120
                    tile = Tile.GrassTile(self, self.grass_img)
                    last_image = self.sand_img
                    last_color = 120

                elif color >= 140 and color < 160:
                    colorb = 140
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.grass_img
                    last_color = 140

                elif color >= 160 and color < 170:
                    colorb = 160
                    if color2[2] == 220:

                        tile = Tile.TreePlantedTile_w(self, self.WithTree_img)
                        tile.location = vector2.Vector2(i << 5, a << 5)
                        tile.rect.topleft = tile.location
                        tile.id = self.TreeID

                        self.TreeLocations[str(self.TreeID)] = tile.location
                        self.TreeID += 1

                        to_rotate = 0
                    else:
                        tile = Tile.TreePlantedTile(self, self.tree_img)

                    last_image = self.tree_img
                    last_color = 160

                elif color >= 170 and color < 190:
                    colorb = 170
                    tile = Tile.TreePlantedTile(self, self.tree_img)
                    last_image = self.WithTree_img
                    last_color = 170

                elif color >= 190 and color < 236:
                    colorb = 190
                    tile = Tile.SmoothStoneTile(self, self.SStone_img)
                    last_image = self.tree_img
                    to_rotate = 0
                    last_color = 190

                else:
                    colorb = 236
                    tile = Tile.SnowTile(self, self.snow_img)
                    last_image = self.SStone_img
                    last_color = 236

                #Shadows----
                fake_color = color
                if color < 110:
                    fake_color = 110
                if fake_color > self.current_height - 3:

                    dark_surface = pygame.Surface((32, 32))
                    dark_surface.set_alpha(0)
                    if color >= 110:
                        self.current_height = fake_color

                    else:
                        self.current_height -= 3

                else:
                    self.current_height -= 3
                    dark_surface = pygame.Surface((1, 1))
                    dark_surface.set_alpha(128)
                #-----------

                #Used for determening start position
                if color2[1] == 255:
                    WORLD_START_POS = (i, a)
                #-----

                tile.location = vector2.Vector2(i << 5, a << 5)
                tile.rect.topleft = tile.location
                tile.color = color

                if to_rotate:
                    tile.img = pygame.transform.rotate(tile.img,
                                                       randint(0, 4) * 90)

                self.background.blit(tile.img, tile.location)

                dark_surface2 = pygame.Surface((32, 32))

                alph = 235 - color
                if color >= 190 and color < 236:
                    alph = 330 - color

                dark_surface2.set_alpha(alph)

                tile.darkness = alph

                self.background.blit(dark_surface, tile.location)
                self.background.blit(dark_surface2, tile.location)
                """
                try:
                    percentage = (color-last_color) / float(
                        colorb - last_color)

                except ZeroDivisionError:
                    percentage = 0.0
                combined_img = self.mapGenerator.lerp_two_images(tile.img,
                 last_image, percentage)
                """

                #self.minimap_img.blit(combined_img.subsurface(
                #   (0,0,1,1)), (i,a))
                self.minimap_img.blit(tile.img.subsurface((0, 0, 1, 1)),
                                      (i, a))
                self.minimap_img.blit(dark_surface.subsurface((0, 0, 1, 1)),
                                      (i, a))
                self.minimap_img.blit(dark_surface2.subsurface((0, 0, 1, 1)),
                                      (i, a))

                self.TileArray[a][i] = tile

        self.populate()
        self.cliper = Clips(self, self.ss)

    def populate(self):
        """adds all the people and make sure they don't go all ape shit
        """
        FARMER_COUNT = 4
        VILLAGER_COUNT = 5
        BUILDER_COUNT = 1

        lumber1 = Building.LumberYard(self, self.lumberyard_img)
        lumber1.location = vector2.Vector2(self.w / 2, self.h / 2)
        lumber1.tile_x, lumber1.tile_y = 4, 4
        self.add_entity(lumber1)

        for Villager_no in xrange(VILLAGER_COUNT):
            """Adds all Wood Cutters
            """
            villager = Lumberjack.Lumberjack(self, self.lumberjack_img)
            villager.location = lumber1.location.copy()
            villager.LastLumberYard = lumber1
            villager.brain.set_state("Searching")
            self.add_entity(villager)
            self.population += 1

        for Building_no in xrange(BUILDER_COUNT):
            builder = Builder.Builder(self, self.builder_img, lumber1)
            builder.location = lumber1.location.copy()
            builder.brain.set_state("Idle")
            self.add_entity(builder)
            self.population += 1

        for FARMER in xrange(FARMER_COUNT):
            """Adds all the farmers
            """
            farmer = Farmer.Farmer(self, self.farmer_img)
            farmer.location = vector2.Vector2(20, 20)
            farmer.brain.set_state("Planting")
            self.add_entity(farmer)
            self.population += 1

    def add_building(self, building, pos):

        buildable = self.test_buildable(building, 0, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = self.get_tile_pos(pos - self.background_pos) * 32
            print "add_building LOC: ", building, Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            self.BuildingQueue.append(Build)
            return 1

    def add_built(self, building, pos):

        buildable = self.test_buildable(building, 1, pos)
        print pos

        if buildable:
            Build = buildable[1]
            Build.location = pos
            print "buildable LOC2: ", building, Build.location
            self.add_entity(Build)
            self.buildings[building] = Build
            return 1

    def test_buildable(self, building, built, pos):

        if building == "LumberYard":
            if built:
                Build = Building.LumberYard(self, self.lumberyard_img)
            else:
                Build = Building.UnderConstruction(self, self.uc_img,
                                                   "LumberYard")

        elif building == "House":
            if built:
                Build = Building.House(self, self.house_img)
            else:
                Build = Building.UnderConstruction(self, self.uc_house_img,
                                                   "House")

        elif building == "Dock":
            if built:
                Build = Building.Dock(self, self.dock_img)
            else:
                Build = Building.UnderConstruction(self, self.ucw_img, "Dock")

        elif building == "Manor":
            if built:
                Build = Building.Manor(self, self.manor_img)
            else:
                Build = Building.UnderConstruction(self, self.uc_img, "Manor")

        buildable = 1
        land = 0
        water = 0

        Twidth, Theight = Build.image.get_size()
        for i in range(Twidth >> 5):
            for j in range(Theight >> 5):
                try:
                    if built:
                        test_tile = self.get_tile(
                            vector2.Vector2((pos.x - 32) + (i << 5),
                                            (pos.y - 32) + (j << 5)))
                        #print "A", test_tile, test_tile.location
                    else:
                        test_tile = self.get_tile(
                            vector2.Vector2(
                                ((pos.x - 32) - self.background_pos.x) +
                                (i << 5),
                                ((pos.y - 32) - self.background_pos.y) +
                                (j >> 5)))
                        #print "B", test_tile, test_tile.location

                    if test_tile.buildable != 1 and building != "Dock":
                        buildable = 0
                        return 0
                    elif building == "Dock":
                        if test_tile.buildable_w:
                            water += 1
                        elif test_tile.buildable:
                            land += 1
                except IndexError:
                    print 'IndexError'
                    return 0

        if building == "Dock" and not built:
            if water >= 1 and land <= 2 and land > 0:
                #print ('buildable', water, land)
                buildable = 1
                return 1, Build
            else:
                #print ('not buildable', water, land)
                buildable = 0
                return 0

        return 1, Build

    def convert_all(self):
        self.grass_img = grass_img.convert()
        self.tree_img = tree_img.convert()
        self.water_img = water_img.convert()
        self.sand_img = sand_img.convert()
        self.cobble_img = cobble_img.convert()
        self.SStone_img = SStone_img.convert()
        self.deepwater_img = deepwater_img.convert()
        self.snow_img = snow_img.convert()
        self.WithTree_img = WithTree_img.convert()

        self.lumberyard_img = lumber_yard_img.convert()
        self.lumberyard_img.set_colorkey((255, 0, 255))

        self.house_img = house_img.convert()
        self.house_img.set_colorkey((255, 0, 255))

        self.dock_img = dock_img.convert()
        self.dock_img.set_colorkey((255, 0, 255))

        self.manor_img = manor_img.convert()
        self.manor_img.set_colorkey((255, 0, 255))

        self.uc_img = uc_img.convert_alpha()
        self.ucw_img = ucw_img.convert()
        self.ucw_img.set_colorkey((255, 0, 255))
        self.uc_house_img = uc_house_img.convert()
        self.uc_house_img.set_colorkey((255, 0, 255))

        self.lumberjack_img = lumberjack_img.convert()
        self.farmer_img = farmer_img.convert()
        self.builder_img = builder_img.convert()

    def grow_trees(self, trees):
        for i in range(len(trees)):
            ran = randint(0, 100)
            if ran == 1 and len(trees) > 0:
                try:
                    a = trees.keys()
                    old_tile = self.get_tile(trees[a[i]])
                    darkness = pygame.Surface((32, 32))
                    darkness.set_alpha(old_tile.darkness)

                    new_tile = Tile.TreePlantedTile_w(self, WithTree_img)

                    new_tile.darkness = old_tile.darkness

                    new_tile.location = old_tile.location
                    new_tile.rect.topleft = new_tile.location
                    new_tile.color = old_tile.color

                    new_tile.id = self.TreeID
                    self.TreeID += 1

                    self.TileArray[int(new_tile.location.y / 32)][int(
                        new_tile.location.x / 32)] = new_tile
                    self.background.blit(new_tile.img, new_tile.location)
                    self.background.blit(darkness, new_tile.location)
                    #print self.Baby_TreeLocations
                    del self.Baby_TreeLocations[str(a[i])]
                except IndexError:
                    pass

    def add_entity(self, entity):
        """Used to add entities to the world
        """
        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1

    def remove_entity(self, entity):
        """function for removing an entity
        """
        try:
            del self.entities[entity.id]
        except KeyError:
            pass

    def remove_tree(self, tree_id):
        #print len(self.TreeLocations)
        try:
            del self.TreeLocations[str(tree_id)]
            return 0
        except KeyError:
            return None

    def get(self, entity_id):
        #Return an entity

        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None

    def process(self, time_passed):
        #Run the world through 1 cycle

        for entity in self.entities.values():
            entity.process(time_passed)

        self.wood_text = self.font.render(
            "Wood: %d/%d" % (self.wood, self.MAXwood), True, (255, 255, 255))
        self.food_text = self.font.render(
            "Food: %d/%d" % (self.food, self.MAXfood), True, (255, 255, 255))
        self.pop_text = self.font.render(
            "Population: %d/%d" % (self.population, self.MAXpopulation), True,
            (255, 255, 255))
        self.frame_text = self.font.render(
            "FPS: %.2f" % (self.clock.get_fps()), True, (255, 255, 255))

        semi_angle = abs(self.clock_degree - 180.0)
        self.background_alpha = min((255 - (255 * (abs(semi_angle / 180)))),
                                    220.0)
        self.background_over.set_alpha(self.background_alpha)

    def render(self, surface):
        surface.blit(self.background, self.background_pos)

        #for i in self.tilearray:
        #   for tile in i:
        #      tile.render(surface)

        for entity in self.entities.itervalues():
            entity.render(surface)

        surface.blit(self.background_over, (0, 0))

        #for point in self.pond_points:
        #    pygame.draw.circle(surface, (255,0,0), (
        #        int(point[0]), int(point[1])), 5)

#         surface.set_clip(0,0,self.w,self.font_size+4)
#         surface.blit(self.wood_text, (40,2))
#         surface.blit(self.food_text, (200,2))
#         surface.blit(self.pop_text, (360,2))
#         surface.set_clip(None)

    def render_all(self, surface, tp, mouse_pos):
        self.cliper.render(surface, tp, mouse_pos)

    def get_close_entity(self, name, location, range=100.):

        location = vector2.Vector2(*location)

        for entity in self.entities.itervalues():
            if entity.name == name:
                distance = location.get_distance_to(entity.location)
                if range == -1:
                    return entity
                if distance < range:
                    return entity

        return None

    def get_tile(self, location):
        tile = self.get_tile_pos(location)

        return self.TileArray[int(tile.y)][int(tile.x)]

    def get_tile_pos(self, location):
        return vector2.Vector2(int(location.x) >> 5, int(location.y) >> 5)

    def get_tile_array(self, start_pos, dimensions):
        dimensions = (int(dimensions[0]), int(dimensions[1]))

        start_tile = self.get_tile_pos(start_pos)

        array = [[None for i in xrange((dimensions[0] * 2) + 1)]
                 for a in xrange((dimensions[1] * 2) + 1)]

        for i in xrange((dimensions[0] * 2) + 1):
            for a in xrange((dimensions[1] * 2) + 1):
                if start_tile.x + i < 0 or start_tile.y + a < 0:
                    continue
                else:
                    array[a][i] = self.TileArray[int((start_tile.y + a) -
                                                     1)][int((start_tile.x +
                                                              i) - 1)]

        return array