def add_sprites(self, *args): """ adds sprites to the factory """ for fnum in args: #f[0] = name to refer to it by #f[1] = filename if isinstance(fnum[1], str): #make a sprite for it #load it into a dictionary with f[1] as the key Factory.__sprites[fnum[0]] = Helpers.load_image(fnum[1]) else: #not a filename, don't care pass
def __init__(self, screen=None, *args): """ init """ if screen: Factory.__screen = screen for fnum in args: #f[0] = name to refer to it by #f[1] = filename if isinstance(fnum[1], str): #make a sprite for it #load it into a dictionary with f[1] as the key Factory.__sprites[fnum[0]] = Helpers.load_image(fnum[1]) else: #not a filename, don't care pass
def __init__(self, pos, image_index=None, **components): super().__init__() images = components.get("images") colors = components.get("colors") blends = components.get("blends") if not image_index: image_index = random.randint(0, len(images) - 1) img = images[image_index] if images else "None" img_color = colors[random.randint(0, len(colors) - 1)] if colors else None img_blend = blends[random.randint(0, len(blends) - 1)] if blends else None self.name = img self.image = load_image(*(img, img_color, img_blend)) self.rect = self.image.get_rect(center=pos) if components.get("interactions"): self.interactions = components.get("interactions")
def __init__(self, cursor_image='cursor.bmp'): super().__init__() self.image = load_image(cursor_image, -1) self.rect = self.image.get_rect() self.clicking = 0
def main(): global ZONERECT pygame.init() screen = pygame.display.set_mode(SCREENRECT.size) pygame.display.set_caption('Medieval Life Sim') pygame.mouse.set_visible(1) # Loading background tiles bgdtile = load_image('background.jpg') background = pygame.Surface(bgdtile.get_rect().size) background.blit(bgdtile, (0, 0)) # for x in range(0, SCREENRECT.width, bgdtile.get_width()): # background.blit(bgdtile, (x, 0)) screen.blit(background, (0, 0)) pygame.display.flip() allsprites, player = spawn_zone() # Clock controls frame rate clock = pygame.time.Clock() interacting = False while True: clock.tick(100) # Handling Exit events separately for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return # Clean up screen and watch for events allsprites.clear(screen, background) keystate = pygame.key.get_pressed() # Checking for and handling interactions if not interacting and keystate[pygame.K_SPACE]: target = player.rect.collidelistall(allsprites.sprites()) if target is not None: target_objs = list( set([ base for x in target for base in allsprites.sprites()[x].groups() if isinstance(base, base_object) ])) # Creating selection of interactable Sprites in location window = interact_window(None, options=target_objs) allsprites.add(window) interacting = True if interacting: pygame.event.wait() key_press = None if keystate[pygame.K_DOWN]: key_press = "down" elif keystate[pygame.K_UP]: key_press = "up" elif keystate[pygame.K_RETURN]: key_press = "select" elif keystate[pygame.K_ESCAPE]: key_press = "exit" interacting = False window.kill() choice = window.update(key_press) if choice: window.kill() print(choice, target) interacting = False # If choice was what to interact with, continue interacting if isinstance(choice, base_object): target = choice window = interact_window(choice) allsprites.add(window) interacting = True else: resolution = resolve_action(allsprites, target, player, choice) if resolution: allsprites = resolution # if not interacting, operating under normal conditions if not interacting: x_direction = keystate[pygame.K_RIGHT] - keystate[pygame.K_LEFT] y_direction = keystate[pygame.K_DOWN] - keystate[pygame.K_UP] view_x_direction = -1 * (keystate[pygame.K_d] - keystate[pygame.K_a]) view_y_direction = -1 * (keystate[pygame.K_s] - keystate[pygame.K_w]) scrolling = False req_move = (x_direction, y_direction) best_move = view_move = (view_x_direction, view_y_direction) if view_x_direction or view_y_direction: scrolling, best_move = check_move_bounds( ZONERECT, SCREENRECT, *(view_move)) if scrolling: ZONERECT = scrolling allsprites.update(*best_move, scrolling) player.move(*req_move) allsprites.draw(screen) pygame.display.flip() # pygame.display.update(dirty) pygame.quit()