def init(self): # Initialize the drawing window. pygame.init () self.screen = pygame.display.set_mode ((400, 400)) self.screen.fill ((250, 250, 250)) pygame.display.set_caption ('Hexploration') # Draw a rectangle, which can moev around on the screen. self.rect = Draw.draw_rect (55, 40, (255, 0, 0)) self.screen.blit (self.rect, (20, 20)) self.x,self.y = 20, 20 # Partial screen assignment for the Renderer. self.renderer = Renderer () surface = pygame.Surface ((200, 400)) self.renderer.screen = surface self.renderer.color = 100, 100, 100 # Some widgets. button = Button ("Stop/Go") button.topleft = 10, 10 button.connect_signal (SIG_CLICKED,self.Switch) self.entry = Entry ("Awesome...") self.entry.topleft = 30, 50 self.renderer.add_widget (button, self.entry) # Blit the Renderer's contents at the desired position. self.renderer.topleft = 200,0 self.screen.blit (self.renderer.screen, self.renderer.topleft) # Set up the tick event for timer based widgets. pygame.time.set_timer (Constants.SIG_TICK, 10)
def draw_board(self): f1=self.coords[0] c1=self.coords[1] f2=f1+TILESHEIGHT c2=c1+TILESWIDTH if f2>=BOARDSIZE: f2=BOARDSIZE f1=f2-TILESHEIGHT if c2>=BOARDSIZE: c2=BOARDSIZE c1=c2-TILESHEIGHT for y in range(f1,f2): for x in range(c1,c2): a=self.W.board.cell((x,y)) if a.is_hab(): id=a.hab[0] color=self.W.habs[id].color elif a.has_food(CARN): color=RED elif a.has_food(HERB): color=GREEN else: color=BROWN pygame.draw.rect(self.re.screen,color,((y-f1)*TILESIZE,(x-c1)*TILESIZE,TILESIZE,TILESIZE)) rect=Draw.draw_rect(TILESIZE,TILESIZE,color) # Draws each tile in the board self.screen.blit(rect,((y-f1)*TILESIZE,(x-c1)*TILESIZE))
def draw (self): """V.draw () -> None Draws the VScale surface and its slider. """ Scale.draw (self) cls = self.__class__ style = base.GlobalStyle border = style.get_border_size (cls, self.style, StyleInformation.get ("SCALE_BORDER")) active = StyleInformation.get ("ACTIVE_BORDER") border_active = style.get_border_size (cls, self.style, active) slider = StyleInformation.get ("VSCALE_SLIDER_SIZE") # Creates the slider surface. sf_slider = style.engine.draw_slider (slider[0], slider[1], self.state, cls, self.style) rect_slider = sf_slider.get_rect () # Dashed border. if self.focus: b = border + border_active r = Rect (b, b, rect_slider.width - 2 * b, rect_slider.height - 2 * b) style.engine.draw_border \ (sf_slider, self.state, cls, self.style, active, r, StyleInformation.get ("ACTIVE_BORDER_SPACE")) size = self.width / 3 - 2 * border, self.height - 2 * border # Fill the scale line. sf_fill = Draw.draw_rect (size[0], size[1], StyleInformation.get ("SCALE_COLOR")) self.image.blit (sf_fill, (self.width / 3 + border, border)) # Blit slider at the correct position. rect_slider.centerx = self.image.get_rect ().centerx rect_slider.centery = self._get_coords_from_value () self.image.blit (sf_slider, rect_slider) # Fill until the slider start. if rect_slider.y > 0: sf_fill = Draw.draw_rect (size[0], rect_slider.y - border, StyleInformation.get ("PROGRESS_COLOR")) self.image.blit (sf_fill, (self.width / 3 + border, border))
def draw(self): """V.draw () -> None Draws the VScale surface and its slider. """ Scale.draw(self) cls = self.__class__ style = base.GlobalStyle border = style.get_border_size(cls, self.style, StyleInformation.get("SCALE_BORDER")) active = StyleInformation.get("ACTIVE_BORDER") border_active = style.get_border_size(cls, self.style, active) slider = StyleInformation.get("VSCALE_SLIDER_SIZE") # Creates the slider surface. sf_slider = style.engine.draw_slider(slider[0], slider[1], self.state, cls, self.style) rect_slider = sf_slider.get_rect() # Dashed border. if self.focus: b = border + border_active r = Rect(b, b, rect_slider.width - 2 * b, rect_slider.height - 2 * b) style.engine.draw_border \ (sf_slider, self.state, cls, self.style, active, r, StyleInformation.get ("ACTIVE_BORDER_SPACE")) size = self.width / 3 - 2 * border, self.height - 2 * border # Fill the scale line. sf_fill = Draw.draw_rect(size[0], size[1], StyleInformation.get("SCALE_COLOR")) self.image.blit(sf_fill, (self.width / 3 + border, border)) # Blit slider at the correct position. rect_slider.centerx = self.image.get_rect().centerx rect_slider.centery = self._get_coords_from_value() self.image.blit(sf_slider, rect_slider) # Fill until the slider start. if rect_slider.y > 0: sf_fill = Draw.draw_rect(size[0], rect_slider.y - border, StyleInformation.get("PROGRESS_COLOR")) self.image.blit(sf_fill, (self.width / 3 + border, border))
def draw (self): """_L.draw () -> Surface Draws the _ListViewPort surface and returns it. Creates the visible surface of the _ListViewPort and returns it to the caller. """ cls = self.scrolledlist.__class__ style = base.GlobalStyle st = self.scrolledlist.style or style.get_style (cls) border = style.get_border_size (cls, self.style, BORDER_FLAT) color = style.get_style_entry (cls, st, "selcolor", self.state) width, height = self._update_items () # The surface of the view should match the visible area, if it # is smaller. tmp = self.scrolledlist.get_visible_area ()[0] - \ self.scrolledlist.padding - 2 * border if (width > 0) and (width < tmp): width = tmp surface = style.draw_rect (width, height, self.state, cls, self.style) posy = 0 for item in self.itemcollection: # The items are already up to date, so we just need to blit # their surfaces. if item.selected: sel_height = item.rect.height + 2 * border surface_select = Draw.draw_rect (width, sel_height, color) surface_select = style.draw_border (surface_select, self.state, cls, self.style, BORDER_FLAT, space=1) item.rect.x = border item.rect.y = border surface_select.blit (item.image, item.rect) item.rect.y += posy # Set the correct position. surface.blit (surface_select, (0, posy)) else: item.rect.x = border item.rect.y = posy + border surface.blit (item.image, item.rect) posy += item.rect.height + self.scrolledlist.spacing + 2 * border return surface
def _draw_axes(self, surface, rect, origin, unitx, unity): """G._draw_axes (...) -> None Draws the coordinate axes on the surface. """ style = base.GlobalStyle cls = self.__class__ if self.orientation == ORIENTATION_VERTICAL: right = (origin[0], rect.bottom) left = (origin[0], rect.top) top = (rect.right, origin[1]) bottom = (rect.left, origin[1]) else: left = (rect.left, origin[1]) right = (rect.right, origin[1]) top = (origin[0], rect.top) bottom = (origin[0], rect.bottom) start = None end = None scx = 1 scy = 1 # Draw both, positive and negative axes if self.negative: Draw.draw_line(surface, self._axiscolor, left, right, 1) Draw.draw_line(surface, self._axiscolor, bottom, top, 1) else: Draw.draw_line(surface, self._axiscolor, origin, right, 1) Draw.draw_line(surface, self._axiscolor, origin, top, 1) # Axis names and units. if self.show_names: st = "%s / %s " % (self.axes[0], self.scale_units[0]) surface_x = style.engine.draw_string(st, self.state, cls, self.style) st = "%s / %s " % (self.axes[1], self.scale_units[1]) surface_y = style.engine.draw_string(st, self.state, cls, self.style) rect_sx = surface_x.get_rect() rext_sy = surface_y.get_rect() if self.orientation == ORIENTATION_VERTICAL: surface.blit(surface_x, (right[0] - 1 - 2 * scx - rect_sx.width, right[1] - rect_sx.height)) surface.blit(surface_y, (top[0] - rect_sy.width, top[1] + 1 + 2 * scy)) else: surface.blit( surface_x, (right[0] - rect_sx.width, right[1] + 1 + 2 * scx)) surface.blit(surface_y, (top[0] + 1 + 2 * scy, top[1])) # Draw the scale unit marks. # From the origin right and up y = origin[1] x = origin[0] while y > rect.top: start = (origin[0] - scy, y) end = (origin[0] + scy, y) Draw.draw_line(surface, self._axiscolor, start, end, 1) y -= unity while x < rect.right: start = (x, origin[1] - scx) end = (x, origin[1] + scx) Draw.draw_line(surface, self._axiscolor, start, end, 1) x += unitx # From the origin down and left. if self.negative: y = origin[1] while y < rect.bottom: start = (origin[0] - scy, y) end = (origin[0] + scy, y) Draw.draw_line(surface, self._axiscolor, start, end, 1) y += unity x = origin[0] while x > rect.left: start = (x, origin[1] - scx) end = (x, origin[1] + scx) Draw.draw_line(surface, self._axiscolor, start, end, 1) x -= unitx
# Draw.draw_line () usage example. import pygame, pygame.locals from ocempgui.draw import Draw # Initialize the drawing window. pygame.init () screen = pygame.display.set_mode ((200, 200)) screen.fill ((250, 250, 250)) pygame.display.set_caption ('Draw.draw_line ()') # Draw horizontal lines in different colors and sizes. for i in range (10): val = i * 10 Draw.draw_line (screen, (0 + val, 50 + val, 40 + 2 * val), (5, val), (195, val), i) # Draw vertical lines in different colors and sizes. for i in range (10): val = i * 8 Draw.draw_line (screen, (0 + 2 * val, 30 + val, 35 + 2 * val), (5 + i * 10, 100), (5 + i * 10, 195), i) # Draw a cross. Draw.draw_line (screen, (0, 0, 0), (120, 100), (195, 195), 3) Draw.draw_line (screen, (0, 0, 0), (195, 100), (120, 195), 3) # Show anything. pygame.display.flip () # Wait for input. while not pygame.event.get ([pygame.locals.QUIT]):
def _draw_axes (self, surface, rect, origin, unitx, unity): """G._draw_axes (...) -> None Draws the coordinate axes on the surface. """ style = base.GlobalStyle cls = self.__class__ if self.orientation == ORIENTATION_VERTICAL: right = (origin[0], rect.bottom) left = (origin[0], rect.top) top = (rect.right, origin[1]) bottom = (rect.left, origin[1]) else: left = (rect.left, origin[1]) right = (rect.right, origin[1]) top = (origin[0], rect.top) bottom = (origin[0], rect.bottom) start = None end = None scx = 1 scy = 1 # Draw both, positive and negative axes if self.negative: Draw.draw_line (surface, self._axiscolor, left, right, 1) Draw.draw_line (surface, self._axiscolor, bottom, top, 1) else: Draw.draw_line (surface, self._axiscolor, origin, right, 1) Draw.draw_line (surface, self._axiscolor, origin, top, 1) # Axis names and units. if self.show_names: st = "%s / %s " % (self.axes[0], self.scale_units[0]) surface_x = style.engine.draw_string (st, self.state, cls, self.style) st = "%s / %s " % (self.axes[1], self.scale_units[1]) surface_y = style.engine.draw_string (st, self.state, cls, self.style) rect_sx = surface_x.get_rect() rext_sy = surface_y.get_rect() if self.orientation == ORIENTATION_VERTICAL: surface.blit (surface_x, (right[0] - 1 - 2 * scx - rect_sx.width, right[1] - rect_sx.height)) surface.blit (surface_y, (top[0] - rect_sy.width, top[1] + 1 + 2 * scy)) else: surface.blit (surface_x, (right[0] - rect_sx.width, right[1] + 1 + 2 * scx)) surface.blit (surface_y, (top[0] + 1 + 2 * scy, top[1])) # Draw the scale unit marks. # From the origin right and up y = origin[1] x = origin[0] while y > rect.top: start = (origin[0] - scy, y) end = (origin[0] + scy, y) Draw.draw_line (surface, self._axiscolor, start, end, 1) y -= unity while x < rect.right: start = (x, origin[1] - scx) end = (x, origin[1] + scx) Draw.draw_line (surface, self._axiscolor, start, end, 1) x += unitx # From the origin down and left. if self.negative: y = origin[1] while y < rect.bottom: start = (origin[0] - scy, y) end = (origin[0] + scy, y) Draw.draw_line (surface, self._axiscolor, start, end, 1) y += unity x = origin[0] while x > rect.left: start = (x, origin[1] - scx) end = (x, origin[1] + scx) Draw.draw_line (surface, self._axiscolor, start, end, 1) x -= unitx
# Embedding example of the Renderer in an own pygame mainloop with # partial screen assignment. import sys, random import pygame, pygame.locals from ocempgui.draw import Draw from ocempgui.widgets import * # Initialize the drawing window. pygame.init () screen = pygame.display.set_mode ((400, 400)) screen.fill ((250, 250, 250)) pygame.display.set_caption ('Draw.draw_rect ()') # Draw a rectangle, which can moev around on the screen. rect = Draw.draw_rect (55, 40, (255, 0, 0)) screen.blit (rect, (20, 20)) x, y = 20, 20 # Partial screen assignment for the Renderer. renderer = Renderer () surface = pygame.Surface ((200, 200)) renderer.screen = surface renderer.color = 100, 100, 100 # Some widgets. button = Button ("Hello :-)") button.topleft = 10, 10 entry = Entry ("Awesome...") entry.topleft = 30, 50 renderer.add_widget (button, entry)
# Draw.draw_rect () usage example. import random import pygame, pygame.locals from ocempgui.draw import Draw # Initialize the drawing window. pygame.init () screen = pygame.display.set_mode ((200, 200)) screen.fill ((250, 250, 250)) pygame.display.set_caption ('Draw.draw_rect ()') # Draw rectangles with various colors. rect = Draw.draw_rect (55, 40, (255, 0, 0)) screen.blit (rect, (5, 5)) rect = Draw.draw_rect (55, 40, (0, 255, 0)) screen.blit (rect, (65, 5)) rect = Draw.draw_rect (55, 40, (0, 0, 255)) screen.blit (rect, (125, 5)) # Draw encapsulated rectangles. for i in range (30): val = i + 3 rnd = (random.randint (0, 5), random.randint (0, 5), random.randint (0, 5)) color = (rnd[0] * i + 100, rnd[1] * i + 100, rnd[2] * i + 100) rect = Draw.draw_rect (100 - 2 * val, 100 - 2 * val, color) screen.blit (rect, (5 + val, 50 + val)) # Show anything. pygame.display.flip ()
# Draw.draw_triangle () usage example. import pygame, pygame.locals from ocempgui.draw import Draw # Initialize the drawing window. pygame.init () screen = pygame.display.set_mode ((200, 200)) screen.fill ((250, 250, 250)) pygame.display.set_caption ('Draw.draw_triangle ()') # Draw three triangles. Draw.draw_triangle (screen, (255, 0, 0), (20, 5), (5, 30), (35, 30), 0) Draw.draw_triangle (screen, (0, 255, 0), (25, 5), (40, 30), (55, 5), 0) Draw.draw_triangle (screen, (0, 0, 255), (60, 5), (45, 30), (75, 30), 0) # Draw a 'tunnel effect' of triangles. for i in range (30): val = i + 3 color = (val * 4, val * 7, val * 5) Draw.draw_triangle (screen, color, (5 + 2 * val, 50 + val), (195 - 2 * val, 50 + val), (100, 195 - 2 * val), 1) # Show anything. pygame.display.flip () # Wait for input. while not pygame.event.get ([pygame.locals.QUIT]): pass