Пример #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
Пример #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
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()
Пример #4
0
import pygame
from pygame.locals import *
from VoronoiMapGen import mapGen
from random import randint

pygame.init()

screen_size = (512, 512)
w, h = screen_size

screen = pygame.display.set_mode(screen_size)

generator = mapGen()


def new_cave(width, height, p=35, ds=None):
    p = int(max(width, height) / min(width, height) * 2.5 + 25)
    map1 = generator.reallyCoolFull((width, height), num_p=p, ds=ds)
    blank_img = pygame.Surface((width, height))
    blank_img.fill((111, 72, 34))
    barWidth = 0
    for y in xrange(height):
        for x in xrange(width):
            barWidth += 1
            clr = map1.get_at((x, y))[0]
            if 150 >= clr >= 128:
                blank_img.set_at((x, y), (156, 102, 31))
            elif (height / 2 < y < height / 2 + 40) and randint(1, 8) == 1:
                blank_img.set_at((x, y), (20, 20, 20))  # Coal
            if (clr == 120 or clr == 121) and randint(1, 4) == 1:
                blank_img.set_at((x, y), (255, 0, 0))
Пример #5
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)
Пример #6
0
import pygame
from pygame.locals import *
from VoronoiMapGen import mapGen
from random import randint

pygame.init()

screen_size = (512, 512)
w, h = screen_size

screen = pygame.display.set_mode(screen_size)

generator = mapGen()


def new_cave(width, height, p=35, ds=None):
    p = int(max(width, height)/min(width, height)*2.5+25)
    map1 = generator.reallyCoolFull((width, height), num_p=p, ds=ds)
    blank_img = pygame.Surface((width, height))
    blank_img.fill((111, 72, 34))
    barWidth = 0
    for y in xrange(height):
        for x in xrange(width):
            barWidth += 1
            clr = map1.get_at((x, y))[0]
            if 150 >= clr >= 128:
                blank_img.set_at((x, y), (156, 102, 31))
            elif (height/2 < y < height/2+40) and randint(1, 8) == 1:
                blank_img.set_at((x, y), (20, 20, 20))  # Coal
            if (clr == 120 or clr == 121) and randint(1, 4) == 1:
                blank_img.set_at((x, y), (255, 0, 0))
Пример #7
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 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()
Пример #9
0
    def new_world(self):
        del self.full_surface
        # seed(self.seed)
        vorMap = self.mapGenerator.negativeArray(
            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

                #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)