def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # let's have a background background = ImageNode.from_image(0, 0, 'sprites.wallpaper') # we want a window to display with a HorizontalContainer node1 = GuiImage(Resources.colour_surface(100, 30, (200, 32, 32))) node2 = GuiImage(Resources.colour_surface(100, 30, (32, 200, 32))) node3 = GuiImage(Resources.colour_surface(100, 30, (32, 32, 200))) c1 = VerticalContainer([node1, node2, node3], (214, 214, 214), align_children=Align.TOP) node6 = GuiImage(Resources.colour_surface(100, 50, (32, 32, 200))) node5 = GuiImage(Resources.colour_surface(100, 50, (32, 200, 32))) node4 = GuiImage(Resources.colour_surface(100, 100, (200, 32, 32))) c2 = VerticalContainer([node6, node5, node4], (214, 214, 214), align_children=Align.TOP) node7 = GuiImage(Resources.colour_surface(100, 30, (200, 32, 32))) node8 = GuiImage(Resources.colour_surface(100, 30, (32, 200, 32))) node9 = GuiImage(Resources.colour_surface(100, 30, (32, 32, 200))) c3 = VerticalContainer([node7, node8, node9], (214, 214, 214), align_children=Align.TOP) hcontainer = HorizontalContainer([c1, c2, c3], (214, 214, 214), align_children=Align.TOP) window = Window(hcontainer) # add the window to a scene scene = Scene([background, window]) controller.add_scene('start', scene) controller.run('start')
def __init__(self, resource_directory): self.setup_logging(resource_directory) logger.info('Starting Wargame v{0}'.format(VERSION)) system_checks() self.screen = pygame.display.set_mode((640, 480), pygame.DOUBLEBUF) pygame.display.set_caption('HexGame') Resources.load_resources(resource_directory, self.screen) self.clock = pygame.time.Clock() self.scenes = {} self.current_scene = None self.last_tick = 0
def get_highlight(self): highlight = self.image.copy() alpha = Resources.alpha_surface(self.image.get_width(), self.image.get_height(), 64) alpha.fill((255, 255, 255, 64)) highlight.blit(alpha, (0, 0)) return highlight
def build_simple_image(self): # we should be the minimum size at least width, height, fill = self.minimum_size image = Resources.colour_surface(width, height, self.background) # now draw the nodes. Account for the border ypos = self.border xpos = self.border # this is the size of the largest widget max_width = width - (2 * self.border) for i in self.nodes: widget_xpos = xpos # we know the images will fit vertically, but they may differ horizontally # is the image smaller? if i.minimum_size[0] < max_width: # either it goes to the left, centre or None. First let's ask the widget if i.align != Align.NONE: widget_align = i.align else: widget_align = self.align_children direction = Align.horizontal(widget_align) if direction == Align.RIGHT: widget_xpos += max_width - i.minimum_size[0] elif direction == Align.CENTRE: widget_xpos += (max_width - i.minimum_size[0]) // 2 # if left, we don't need to do anything if i.image is None: i.build_image() # now we have the xpos and ypos i.update_position(widget_xpos, ypos) image.blit(i.image, (widget_xpos, ypos)) ypos += i.image.get_height() + (self.border * 2) return image
def build_image(self, width=0, height=0): # this function MUST be overridden. # to ensure no errors, we set a horrible red square if self.image is None: size = 128 self.image = Resources.colour_surface(size, size, (255, 0, 0)) self.rect = pygame.Rect(0, 0, size, size) self.visible = True
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # we need 2 images to display mouse over and mouse not over red = Resources.colour_surface(200, 200, (255, 0, 0)) blue = Resources.colour_surface(200, 200, (0, 0, 255)) # we need a node node = MouseOverNode(blue, red, pygame.Rect(220, 140, 200, 200)) # I add the node to a SCENE scene = Scene([node]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # let's have a background background = ImageNode.from_image(0, 0, 'sprites.wallpaper') # we want a window to display with a VerticalContainer node1 = GuiImage(Resources.colour_surface(200, 80, (200, 32, 32))) node2 = GuiImage(Resources.colour_surface(175, 80, (32, 200, 32)), align=Align.CENTRE_RIGHT) node3 = GuiImage(Resources.colour_surface(150, 80, (32, 32, 200))) container = VerticalContainer([node1, node2, node3], (214, 214, 214), align_children=Align.CENTRE_LEFT) window = Window(container) # add the window to a scene scene = Scene([background, window]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def build_widget_display(self, border, base_image): # base image has already been built at this point image = add_border(base_image, border, Resources.get_image(border.image)) # only relative to the window # have to add widths and border AND screen position border_size = border.border_size deltax = border.rects.middle_left[2] + border_size deltay = border.rects.top_left[3] + border_size self.container.update_position(deltax, deltay) return image
def __init__(self, contents, xpos=-1, ypos=-1): self.container = contents # get the contents to render themselves self.container.build_image() border = Resources.configs.get(self.border_config) image = self.build_widget_display(border, self.container.image) if xpos < 0: xpos, ypos = Resources.get_centre(image.get_width(), image.get_height()) rect = pygame.Rect(xpos, ypos, image.get_width(), image.get_height()) super().__init__(rect, image)
def __init__(self, text, signal, align=Align.NONE): # make a label of the text - but we only want the image label = GuiLabel(text, (0, 0, 0), (214, 214, 214)).image # get the contents to render themselves border = Resources.configs.get(self.border_config) image = add_border(label, border, Resources.get_image(border.image)) rect = pygame.Rect(0, 0, image.get_width(), image.get_height()) super().__init__(rect, image, align, False) self.messages = [ pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN ] self.highlight = self.get_highlight() self.normal_image = self.image self.signal = signal self.changed = False
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # this time, we want 1 node to control 2 other nodes # we need 2 images to display mouse over and mouse not over red = Resources.colour_surface(128, 128, (255, 0, 0)) blue = Resources.colour_surface(128, 128, (0, 0, 255)) green = Resources.colour_surface(128, 128, (0, 255, 0)) # we need a node sender = NodeTransmit(green, blue, pygame.Rect(256, 173, 128, 128)) receive1 = NodeReceive(blue, red, pygame.Rect(64, 176, 128, 128), sender.message_id) receive2 = NodeReceive(blue, red, pygame.Rect(448, 176, 128, 128), sender.message_id) # add the nodes to a SCENE scene = Scene([sender, receive1, receive2]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def __init__(self, text, colour, background, border=4, align=Align.NONE, fill=False): # we need to make a rect and an image # let's start with the image font = Resources.get_font() label = font.render(text, True, colour, background).convert() rect = pygame.Rect(0, 0, label.get_width(), label.get_height()) if border > 0: width = rect.width + (2 * border) height = rect.height + (2 * border) image = pygame.Surface((width, height)).convert() image.fill(background) image.blit(label, (border, border)) label = image super().__init__(rect, label, align, fill)
def build_full_image(self, width, height): if height == -1: # use rect size width = self.minimum_size[1] image = Resources.colour_surface(width, height, self.background) # we fill the given space with the widgets. # what is our minimum size? sizes = [x.minimum_size for x in self.nodes] min_width = sum([x[0] for x in sizes]) if min_width < width: # we have spare space, since we only worry about the vertical size # how do we distribute the space? if self.align_children == Align.LEFT: # everything to the left node_image = self.build_simple_image() # (0,0) -> no need to update positions image.blit(node_image, (0, 0)) return image else: # must be same size return self.build_simple_image()
def build_display(self): hex_image = Resources.get_hex('green') self.image = pygame.Surface((400, 300)).convert() for i in self.hexes: self.image.blit(hex_image, i.pixel_position)
def from_image(xpos, ypos, image): image = Resources.get_image(image) rect = pygame.Rect(xpos, ypos, image.get_width(), image.get_height()) return ImageNode(rect, image)
def from_image(image_name): image = Resources.get_image(image_name) return GuiImage(image)
def from_colour(width, height, colour): image = Resources.colour_surface(width, height, colour) return ImageNode.from_surface(0, 0, image)