def random_dest(self, recurse=False): # Function for going to a random destination if recurse: self.farmer.orientation += 20 else: self.farmer.orientation += random.randint(-20, 20) angle = math.radians(self.farmer.orientation) distance = random.randint(50, 100) random_dest = (self.farmer.location.x + math.cos(angle) * distance, self.farmer.location.y + math.sin(angle) * distance) if self.farmer.world.get_tile(Vector2(*random_dest)).name == "WaterTile": try: self.random_dest(True) except RuntimeError: print "SOMEONE IS DROWNING!!" self.farmer.destination = Vector2(*random_dest)
def plant_seed(self): # Function for planting trees # Test to see if the tile the arborist is on is a tile that a tree can be planted on if TileFuncs.get_tile(self.arborist.world,self.arborist.location).plantable == 1: self.arborist.hit = 0 self.arborist.update() old_tile = TileFuncs.get_tile(self.arborist.world,Vector2(self.arborist.location)) darkness = pygame.Surface((32, 32)) darkness.set_alpha(old_tile.darkness) new_tile = Tile.Baby_Tree(self.arborist.world, "GrassWithCenterTree") new_tile.darkness = old_tile.darkness new_tile.location = TileFuncs.get_tile_pos(self.arborist.world,self.arborist.destination)*32 new_tile.rect.topleft = new_tile.location new_tile.color = old_tile.color self.arborist.world.tile_array[int(new_tile.location.y/32)][int(new_tile.location.x/32)] = new_tile self.arborist.world.world_surface.blit(new_tile.img, new_tile.location) self.arborist.world.world_surface.blit(darkness, new_tile.location) # Goes to a random destination no matter what self.arborist.hit = 0 BaseFunctions.random_dest(self.arborist)
def check_conditions(self): check = TileFuncs.get_tile(self.lumberjack.world, Vector2(self.lumberjack.location)) if self.lumberjack.location.get_distance_to( self.lumberjack.destination) < 15: self.lumberjack.destination = Vector2(self.lumberjack.location) if check.name != "GrassWithCenterTree": self.lumberjack.hit = 0 self.lumberjack.update() return "Searching" self.lumberjack.update() if self.lumberjack.hit >= 4: self.lumberjack.destination = Vector2(self.lumberjack.location) self.lumberjack.update() old_tile = TileFuncs.get_tile( self.lumberjack.world, Vector2(self.lumberjack.location)) darkness = pygame.Surface((32, 32)) darkness.set_alpha(old_tile.darkness) new_tile = Tile.TreePlantedTile(self.lumberjack.world, "MinecraftGrass") new_tile.darkness = old_tile.darkness new_tile.location = TileFuncs.get_tile_pos( self.lumberjack.world, self.lumberjack.destination) * 32 new_tile.rect.topleft = new_tile.location new_tile.color = old_tile.color self.lumberjack.world.tile_array[int( new_tile.location.y / 32)][int(new_tile.location.x / 32)] = new_tile self.lumberjack.world.world_surface.blit( new_tile.img, new_tile.location) self.lumberjack.world.world_surface.blit( darkness, new_tile.location) self.lumberjack.hit = 0 # del self.lumberjack.world.TreeLocations[str(self.lumberjack.tree_id)] return "Delivering"
def check_conditions(self): if self.angler.location.get_distance_to( self.angler.destination) <= self.angler.max_speed: self.angler.destination = Vector2(self.angler.location) self.angler.update() if self.angler.fish == 1: return "Delivering"
def __init__(self, world, img): self.world = world self.name = "BASETILE" self.img = img self.location = Vector2(0, 0) self.walkable = 0 self.plantable = 0 self.buildable = 0 self.buildable_w = 0 self.id = 0 self.rect = pygame.Rect((0, 0), self.img.get_size())
def __init__(self, world, tile_name="NULL"): self.world = world self.name = tile_name self.img = pygame.image.load("Images/Tiles/" + tile_name + ".png").convert() self.location = Vector2(0, 0) self.walkable = False self.fishable = False self.plantable = False self.tillable = False self.crop_plantable = False self.buildable = False self.buildable_w = False self.darkness = 0 self.id = 0 self.rect = pygame.Rect((0, 0), self.img.get_size())
def random_dest(entity, recurse=False, r_num=0, r_max=6): # Function for going to a random destination if recurse: entity.orientation += 30 else: entity.orientation += random.randint(-30, 30) angle = math.radians(entity.orientation) distance = random.randint(50, 100) possible_dest = Vector2(entity.location.x + math.cos(angle) * distance, entity.location.y + math.sin(angle) * distance) # If the destination will go off the map, it is NOT a valid move under any circumstances. bad_spot = False if (0 > possible_dest.x > entity.world.world_size[0] or \ 0 > possible_dest.y > entity.world.world_size[1]): bad_spot = True if DEBUG: "BAD SPOT IS TRUE" walk = TileFuncs.get_tile(entity.world, possible_dest).walkable == entity.land_based depth_max = r_num >= r_max if ((not walk and not depth_max) or bad_spot): random_dest(entity, True, r_num + 1, r_max) return else: entity.destination = possible_dest if DEBUG: print "Current Tile: " + TileFuncs.get_tile( entity.world, entity.location).__class__.__name__ print "Destination Tile: " + TileFuncs.get_tile( entity.world, entity.destination).__class__.__name__ print "r_num: %d" % r_num print "walk: ", walk print "" if bad_spot: print "BAD SPOT, WTF"
def plant_seed(self): # Function for planting trees # Test to see if the tile the farmer is on is a tile that a tree can be planted on if self.farmer.world.get_tile(self.farmer.location).plantable == 1: self.farmer.hit = 0 self.farmer.image = self.farmer.start self.farmer.image.set_colorkey((255, 0, 255)) old_tile = self.farmer.world.get_tile(Vector2(self.farmer.location)) darkness = pygame.Surface((32, 32)) darkness.set_alpha(old_tile.darkness) new_tile = Baby_Tree(self.farmer.world, Tile_image) new_tile.darkness = old_tile.darkness new_tile.location = self.farmer.world.get_tile_pos(self.farmer.destination)*32 new_tile.rect.topleft = new_tile.location new_tile.color = old_tile.color # Give it an ID so it can be found new_tile.id = self.farmer.world.Baby_TreeID self.farmer.world.Baby_TreeID += 1 self.farmer.world.TileArray[int(new_tile.location.y/32)][int(new_tile.location.x/32)] = new_tile self.farmer.world.background.blit(new_tile.img, new_tile.location) self.farmer.world.background.blit(darkness, new_tile.location) # Add the location to a dictionary so villagers can see how far they are from it. self.farmer.world.baby_tree_locations[str(self.farmer.world.Baby_TreeID)] = new_tile.location # Goes to a random destination no matter what self.farmer.hit = 0 self.random_dest()
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 random_dest(self): w, h = self.Villager.world.size self.Villager.destination = Vector2(randint(0, w / 12), randint(0, h / 12))
def entry_actions(self): #TODO: Make dropoff point dynamic (e.g. it's own building) self.angler.destination = Vector2(self.angler.world.w / 2, self.angler.world.h / 2)
def check_conditions(self): if self.arborist.location.get_distance_to(self.arborist.destination) < 15: self.arborist.destination = Vector2(self.arborist.location) self.arborist.update()
def entry_actions(self): self.lumberjack.destination = Vector2(self.lumberjack.world.w / 2, self.lumberjack.world.h / 2)
def check_conditions(self): if self.farmer.location.get_distance_to(self.farmer.destination) < 2: self.farmer.destination = Vector2(self.farmer.location) self.farmer.update()