def __init__(self, current, max, info, color, pos): self.info = info self.current = current self.max = max self.color = color self.info = info self.hovering = ALWAYS_SHOW_STATS self.image = pygame.Surface((176, 16)) self.rect = self.image.get_rect() self.rect.x = pos[0] self.rect.y = pos[1] self.image.fill((84, 84, 84)) width = int((self.current / self.max) * self.image.get_width()) self.image.fill(color, rect=pygame.Rect((0, 0), (width, self.image.get_height()))) self.image.blit( render_textrect(str(info), 50, self.rect, (255, 255, 255)), (3, 0)) self.text = '{0}/{1}'.format(self.current, self.max) if self.hovering: self.image.blit( render_textrect( self.text, 50, self.rect, (255, 255, 255), None, 1, ), (0, 0))
def __init__(self, world): self.width = 180 self.height = 95 Gui.__init__( self, 'character', (0, 200), pygame.Surface((self.width, self.height), pygame.SRCALPHA, 32), True) self.image.convert_alpha() self.player_image = world.player.image.copy() self.time_passed = 0.0 self.player_image = pygame.transform.scale( self.player_image, (self.player_image.get_width() / 2, self.player_image.get_height() / 2)) self.name = render_textrect(world.player.name, 30, self.image.get_rect(), (255, 255, 255)) self.time_passed_text = render_textrect(self.time_passed, 30, pygame.Rect(0, 0, 30, 30), (255, 255, 255), (54, 54, 54)) self.exp = Bar(world.player.exp, world.player.stats['EXP'], 'Lvl {0}'.format(world.player.lvl), (50, 255, 10), (5, 20)) self.hp = Bar(world.player.hp, world.player.stats['HP'], 'HP', (224, 52, 52), (5, 45)) self.mp = Bar(world.player.mp, world.player.stats['MP'], 'MP', (96, 132, 224), (5, 70)) register_handler(TIME_PASSED, self.handle_event)
def draw_minimap(self): self.waterline = self.get_waterline() for y in range(0, self.height): for x in range(0, self.width): self.tiles[y][x] = int(self.tiles[y][x]) if self.tiles[y][x] > 255: self.tiles[y][x] = 255 tile = int(self.tiles[y][x]) if tile <= self.waterline: color = (25, 25, tile + 75) elif tile > self.waterline and tile <= self.waterline + 10: color = (tile + 80, tile + 80, 100) elif tile > self.waterline + 10 and tile <= self.waterline + 40: color = (0, 255 - tile, 0) elif tile > self.waterline + 40 and tile <= 190: color = (0, 255 - tile, 0) elif tile > 190: color = (255 - tile, 255 - tile, 255 - tile) #color = (tile, tile, tile) image = pygame.Surface((self.tile_size, self.tile_size)) image.fill(color) self.minimap.blit(image, (x * self.tile_size, y * self.tile_size))
def makeTransformsPermanent(self): self._images = [ pygame.Surface(surfObj.get_size(), 0, surfObj) for surfObj in self._transformedImages ] for i in range(len(self._transformedImages)): self._images[i].blit(self._transformedImages[i], (0, 0))
def __init__(self): self.width = WIDTH - MENU_WIDTH self.height = CONSOLE_HEIGHT Gui.__init__(self, 'console', (0, HEIGHT - CONSOLE_HEIGHT), pygame.Surface((self.width, self.height)), True) self.log = [] self.log_processed = [] register_handler(POST_TO_CONSOLE, self.handle_event)
def __init__(self, dir, pos): self.arrow = pygame.Surface((12, 6), pygame.SRCALPHA, 32).convert_alpha() self.color = (255, 255, 255) self.rect = self.arrow.get_rect() self.rect.x = pos[0] self.rect.y = pos[1] self.dir = dir self.zoom_level = 0 self.max_zoom_level = 2
def __init__(self, world): self.world = world self.rect = pygame.Rect((0, 0), (2, 2)).copy() self.color = (255, 0, 0) self.in_map = True self.image = pygame.Surface((32, 32), pygame.SRCALPHA, 32) self.image.convert_alpha() self.offset = pygame.Rect(0, 0, 0, 0) register_handler([pygame.MOUSEMOTION, TIME_PASSED], self.handle_event)
def __init__(self, x, y, object=None): self.object = object self.type = 'inventory' if object: self.image = object else: self.image = pygame.Surface((32, 32)) self.x = x self.y = y self.rect = pygame.Rect(x, y, 32, 32)
def draw_feet(self): self.feet = pygame.Surface((32, 32), pygame.SRCALPHA, 32) # If its a pathfinding tile, draw out footsteps based on the directions of the feet. if self.dirs[0] != 9: f = pygame.image.load('../res/other/footsteps/parent/0.png') f = self.rot_center(f, self.dirs[0] * -45) self.feet.blit(f, (0, 0)) if self.dirs[1] != 9: f2 = pygame.image.load('../res/other/footsteps/current/0.png') f2 = self.rot_center(f2, self.dirs[1] * -45) self.feet.blit(f2, (0, 0))
def __init__(self, world): self.rect = pygame.Rect((WIDTH - MENU_WIDTH, 0), (MENU_WIDTH, HEIGHT)) self.bg = pygame.Surface((self.rect.w, self.rect.h)) self.char_stat = CharStats(world) self.minimap = MiniMap(world) self.explorer = Explorer() self.inventory = InventoryGui() register_handler( [pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION], self.handle_event) self.held_item = None
def update_data(self): self.show_window = False self.image = pygame.Surface((200, 100), pygame.SRCALPHA, 32) i = 0 for key, event in self.options.items(): string = "{0}: {1}".format(key, event[0]) rect = pygame.Rect((0, 15 * i), (len(string) * 7, 15)) i += 1 self.image.blit( render_textrect(string, 15, rect, (128, 128, 128), (0, 0, 0)), (rect.x, rect.y))
def __init__(self): pygame.init() get_item_sprite('test', 0) self.event_manager = EventManager() self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) self.world_screen = pygame.Surface( (WIDTH - MENU_WIDTH, HEIGHT - CONSOLE_HEIGHT)) self.clock = GameClock(40) self.camera = self.world_screen.get_rect().copy() self.events = None self.menu = Menu() register_handler(MENU_NEW_GAME, self.new_game)
def __init__(self, type, map): self.type = type self.tiles = map self.width = len(self.tiles[0]) self.height = len(self.tiles) self.tile_size = 32 self.waterline = 0 self.minimap = pygame.Surface( (self.width * self.tile_size, self.height * self.tile_size)) self.draw_minimap()
def __init__(self): self.width = MENU_WIDTH self.height = 80 Gui.__init__(self, 'inventory', (30, 300), pygame.Surface((self.width, self.height), pygame.SRCALPHA, 32).convert_alpha(), False) self.slots = [] self.slots_rect = [] for y in range(0, 2): for x in range(0, 4): self.slots_rect.append(pygame.Rect(5 + x * 35, 5 + y * 35, 32, 32)) self.slots.append(Slot(5 + x * 35, 5 + y * 35)) register_handler(PLAYER_PICKUP_ITEM, self.handle_event)
def handle_event(self, event): etype = get_event_type(event) if etype == GUI_EXPLORER_ITEMS: self.active = True for i, item in enumerate(event.items): self.slots[i].object = item elif etype == GUI_EXPLORER_CLEAR: for slot in self.slots: slot.object = None self.active = False slot.image = pygame.Surface((32, 32)) elif etype == PLAYER_PICKUP_ITEM: if event.target in self.slots: self.slots.remove(event.target)
def __init__(self, world): self.world = world self.data = None image = pygame.Surface((200, 100), pygame.SRCALPHA, 32) #image.convert_alpha() self.options = {} self.target = None self.show_window = False self.item_tooltip = None self.mouse = Mouse_select(world) Gui.__init__(self, 'character', (0, 0), image, True) register_handler([ GUI_TOOLTIP_CLEAR, GUI_TOOLTIP_POST, GUI_EXAMINE_ITEM, GUI_EXAMINE_ITEM_CLEAR, pygame.MOUSEBUTTONDOWN ], self.handle_event)
def __init__(self): self.width = WIDTH self.height = HEIGHT #self.logo = render_textrect('Dungeon of Doom (Alpha)', 30, self.logo_rect, (255, 255, 0), None, 1) self.main_bg = pygame.image.load('../res/gui/main_menu_bg.png') self.main_bg = pygame.transform.scale(self.main_bg, (self.width, self.height)) Gui.__init__(self, 'menu', (0, 0), pygame.Surface((self.width, self.height)), True) self.main = True register_handler([MENU_NEW_GAME, MENU_CLASS_SELECT, MENU_ENTER_NAME, MENU_QUIT_GAME, pygame.MOUSEBUTTONDOWN], self.handle_event) self.buttons = [ Button('New game', MENU_NEW_GAME, pygame.Rect((self.width / 2 - 100, self.height / 2 - 40), (200, 50))), Button('Load game', MENU_NEW_GAME, pygame.Rect((self.width / 2 - 100, self.height / 2), (200, 50))), Button('Quit', MENU_QUIT_GAME, pygame.Rect((self.width / 2 - 100, self.height / 2 + 40), (200, 50))) ]
def get_item_sprite(sheet, id='rand'): items = pygame.image.load( "../res/spritesheets/items/{0}.png".format(sheet)) spritesheet_width = items.get_width() spritesheet_height = items.get_height() if id == 'rand': max = (spritesheet_width * spritesheet_height) / 32 id = random.randint(max) x = id * 32 % spritesheet_width y = int(id * 32 / spritesheet_width) y *= 32 if y > spritesheet_height: print("Cant find given item image") return None rect = pygame.Rect((x, y), (32, 32)) image = pygame.Surface((32, 32)) image.blit(items, (0, 0), rect) return image
def __init__(self, world): self.world = world self.tile_size = 6 self.width = 192 self.height = 192 self.camera = pygame.Rect((0, 0), (self.width, self.height)).copy() self.map = Map() self.map.load_dungeon(world.map.dungeon, True, self.tile_size) self.zoom_in_arrow = ZoomArrow(0, (174, 10)) self.zoom_in_arrow.create() self.zoom_out_arrow = ZoomArrow(1, (174, 20)) self.zoom_out_arrow.create() Gui.__init__(self, 'minimap', (4, 4), pygame.Surface((self.width, self.height)), True) x = self.world.player.x * self.tile_size y = self.world.player.y * self.tile_size self.camera.center = (x, y) self.map.draw(self.image, self.camera, self.world.map.explored_tiles) self.image.fill((0, 255, 0), pygame.Rect((x - self.camera.x, y - self.camera.y), (self.tile_size, self.tile_size))) self.image.blit(self.zoom_in_arrow.arrow, (self.zoom_in_arrow.rect.x, self.zoom_in_arrow.rect.y)) self.image.blit(self.zoom_out_arrow.arrow, (self.zoom_out_arrow.rect.x, self.zoom_out_arrow.rect.y)) register_handler([TIME_PASSED, pygame.MOUSEBUTTONDOWN], self.handle_event)
def handle_event(self, event): mouse_rect = pygame.Rect(event.pos, (2, 2)).copy() mouse_rect.x -= self.rect.x + 3 mouse_rect.y -= self.rect.y + 26 slot = None gui = self.get_gui(mouse_rect) if gui == self.char_stat: self.char_stat.mouse(mouse_rect, event) elif gui == self.minimap: self.minimap.mouse(mouse_rect, event) elif gui == self.explorer: self.explorer.mouse(mouse_rect, event) slot = self.explorer.get_slot(mouse_rect) if event.type == pygame.MOUSEBUTTONUP and self.held_item: if slot and not slot.object: if self.held_item.type == 'inventory': post_event(GUI_EXPLORER_CLEAR) post_event(PLAYER_DROP_ITEM, target=self.held_item.object) elif self.held_item.type == 'explorer': slot.object = self.held_item.object else: post_event(GUI_EXAMINE_ITEM_CLEAR) if gui == self.inventory: self.inventory.mouse(mouse_rect, event) slot = self.inventory.get_slot(mouse_rect) if event.type == pygame.MOUSEBUTTONUP and self.held_item: if slot and not slot.object: if self.held_item.type == 'explorer': post_event(PLAYER_PICKUP_ITEM, target=self.held_item.object, slot=slot) elif self.held_item.type == 'inventory': slot.object = self.held_item.object if event.type == pygame.MOUSEBUTTONDOWN: if slot and slot.object: self.held_item = copy.copy(slot) slot.object = None pygame.mouse.set_visible(False) slot.image = pygame.Surface((32, 32)) post_event(GUI_EXAMINE_ITEM_CLEAR) if event.type == pygame.MOUSEBUTTONUP: if self.held_item: if not slot: if self.held_item.type == 'inventory': if mouse_rect.x < 0: post_event(PLAYER_DROP_ITEM, target=self.held_item.object) else: self.inventory.slots[ self.inventory.slots_rect.index( self.held_item.rect )].object = self.held_item.object elif self.held_item.type == 'explorer': self.explorer.slots[self.explorer.slots_rect.index( self.held_item.rect )].object = self.held_item.object self.held_item = None pygame.mouse.set_visible(True)
def render_textrect(string, maxsize, rect, text_color, background_color=None, justification=0): """Returns a surface containing the passed text string, reformatted to fit within the given rect, word-wrapping as necessary. The text will be anti-aliased. Takes the following arguments: string - the text you wish to render. \n begins a new line. font - a Font object rect - a rectstyle giving the size of the surface requested. text_color - a three-byte tuple of the rgb value of the text color. ex (0, 0, 0) = BLACK background_color - a three-byte tuple of the rgb value of the surface. justification - 0 (default) left-justified 1 horizontally centered 2 right-justified Returns the following values: Success - a surface object with the text rendered onto it. Failure - raises a TextRectException if the text won't fit onto the surface. """ import pyj2d as pygame font = pygame.font.Font('../res/other/visitor2.ttf', maxsize) final_lines = [] string = str(string) requested_lines = string.splitlines() # Create a series of lines that will fit on the provided # rectangle. for requested_line in requested_lines: if font.size(requested_line)[0] > rect.width: words = requested_line.split(' ') # if any of our words are too long to fit, return. for word in words: if font.size(word)[0] >= rect.width: return render_textrect(string, maxsize - 1, rect, text_color, background_color, justification) #raise TextRectException, "The word " + word + " is too long to fit in the rect passed." # Start a new line accumulated_line = "" for word in words: test_line = accumulated_line + word + " " # Build the line while the words fit. if font.size(test_line)[0] < rect.width: accumulated_line = test_line else: final_lines.append(accumulated_line) accumulated_line = word + " " final_lines.append(accumulated_line) else: final_lines.append(requested_line) # Let's try to write the text out on the surface. surface = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA, 32) surface.convert_alpha() if background_color: surface.fill(background_color) accumulated_height = 0 for line in final_lines: height = font.size(line)[1] if accumulated_height + height >= rect.height: return render_textrect(string, maxsize - 1, rect, text_color, background_color, justification) #raise TextRectException, "Once word-wrapped, the text string was too tall to fit in the rect." if line is not "": tempsurface = font.render(line, 1, text_color) if justification == 0: surface.blit(tempsurface, (0, accumulated_height)) elif justification == 1: surface.blit(tempsurface, ((rect.width - tempsurface.get_width()) / 2, accumulated_height)) elif justification == 2: surface.blit( tempsurface, (rect.width - tempsurface.get_width(), accumulated_height)) else: raise TextRectException("Invalid justification argument: " + str(justification)) accumulated_height += height return surface
def anchor(self, anchorPoint=NORTHWEST): # If the Surface objects are of different sizes, align them all to a # specific "anchor point" (one of the NORTH, SOUTH, SOUTHEAST, etc. constants) # # By default, they are all anchored to the NORTHWEST corner. if self.areFramesSameSize(): return # nothing needs to be anchored # This check also prevents additional calls to anchor() from doing # anything, since anchor() sets all the image to the same size. # The lesson is, you can only effectively call anchor() once. self.clearTransforms( ) # clears transforms since this method anchors the original images. maxWidth, maxHeight = self.getMaxSize() halfMaxWidth = int(maxWidth / 2) halfMaxHeight = int(maxHeight / 2) for i in range(len(self._images)): # go through and copy all frames to a max-sized Surface object # NOTE: This makes changes to the original images in self._images, not the transformed images in self._transformedImages newSurf = pygame.Surface( (maxWidth, maxHeight) ) # TODO: this is probably going to have errors since I'm using the default depth. # set the expanded areas to be transparent newSurf = newSurf.convert_alpha() newSurf.fill((0, 0, 0, 0)) frameWidth, frameHeight = self._images[i].get_size() halfFrameWidth = int(frameWidth / 2) halfFrameHeight = int(frameHeight / 2) # position the Surface objects to the specified anchor point if anchorPoint == NORTHWEST: newSurf.blit(self._images[i], (0, 0)) elif anchorPoint == NORTH: newSurf.blit(self._images[i], (halfMaxWidth - halfFrameWidth, 0)) elif anchorPoint == NORTHEAST: newSurf.blit(self._images[i], (maxWidth - frameWidth, 0)) elif anchorPoint == WEST: newSurf.blit(self._images[i], (0, halfMaxHeight - halfFrameHeight)) elif anchorPoint == CENTER: newSurf.blit(self._images[i], (halfMaxWidth - halfFrameWidth, halfMaxHeight - halfFrameHeight)) elif anchorPoint == EAST: newSurf.blit( self._images[i], (maxWidth - frameWidth, halfMaxHeight - halfFrameHeight)) elif anchorPoint == SOUTHWEST: newSurf.blit(self._images[i], (0, maxHeight - frameHeight)) elif anchorPoint == SOUTH: newSurf.blit( self._images[i], (halfMaxWidth - halfFrameWidth, maxHeight - frameHeight)) elif anchorPoint == SOUTHEAST: newSurf.blit(self._images[i], (maxWidth - frameWidth, maxHeight - frameHeight)) self._images[i] = newSurf