def image_at(self, rectangle, colorkey = None): "Loads image from x,y,x+offset,y+offset" rect = pygame.Rect(rectangle) image = pygame.Surface(rect.size).convert() image.blit(self.sheet, (0, 0), rect) if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0,0)) image.set_colorkey(colorkey, pygame.RLEACCEL) return image
def image(self, rect, colorkey=None, alpha=False): rect = Rect(rect) if alpha: image = Surface(rect.size).convert_alpha() else: image = Surface(rect.size).convert() image.blit(self.sheet, (0,0), rect) if colorkey is not None: if colorkey == -1: colorkey = image.get_at((0,0)) image.set_colorkey(colorkey, RLEACCEL) return image
def imageAt(self, rectangle, colorkey=-1): """ Note: When calling images_at the rect is the format: (x, y, x + offset, y + offset) """ "Loads image from x,y,x+offset,y+offset" rect = Rect(rectangle) image = Surface(rect.size).convert() image.blit(self.sheet, (0, 0), rect) if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0, 0)) image.set_colorkey(colorkey, RLEACCEL) return image
def get_image(self, x, y, width, height): """ Grab a single image out of a larger spritesheet Pass in the x, y location of the sprite and the width and height of the sprite. """ # Create a new blank image image = Surface([width, height]).convert() # Copy the sprite from the large sheet onto the smaller image image.blit(self.sprite_sheet, (0, 0), (x, y, width, height)) # Assuming black works as the transparent color image.set_colorkey(self.color) # Return the image return image
def mouseon(self): # we want the ORT to behave like an exit if self.nick == 'ORT_Number_1': self.server.whichmouseover('ov_tram_exit') if self.label is not None: print self.nick, 'on' self.label.die() # Strip color codes from the nick for display nicklist = list(self.nick) nick = '' avoid = list(string.digits) avoid.extend([',', '\x03']) found = 0 for n in nicklist: if n == '\x03': found = 1 if found == 1: try: test = avoid.index(n) except: found = 0 if found == 0: nick = nick + n sx, sy = _font.size(nick) s = _font.render(nick, 0, (255, 255, 255)) image = pygame.Surface((sx+2, sy+2)) image.set_colorkey((255,165,0)) image.fill((255,165,0)) for x in range(3): for y in range(3): image.blit(s, (x, y)) image.blit(_font.render(nick, 1, (0,0,0)), (1, 1)) image.set_alpha(127) x, y = self.rect.center nx, ny = self.noffset self.label = ClampingSprite((x+nx, y+ny), image, Rect(0, 0, 640, 480)) self.group.add(self.label) return [self.label.rect]
def renderText(image): dateFont = font.SysFont('arial', 20, bold=True, italic=False) addressFont = font.SysFont('arial', 14, bold=False, italic=True) date = datetime.datetime.now().strftime('%b %d, %Y %I:%M %p') srfDate = dateFont.render(date, True, (255, 255, 255)) srfDateShadow = dateFont.render(date, True, (0, 0, 0)) address = 'Boulevard East, Weehawken, NJ' srfAddress = addressFont.render(address, True, (255, 255, 255)) srfAddressShadow = addressFont.render(address, True, (0, 0, 0)) image.blit(srfDateShadow, (11, 11)) image.blit(srfDate, (10, 10)) image.blit(srfAddressShadow, (11, 36)) image.blit(srfAddress, (10, 35))
def renderText(image): dateFont = font.SysFont('arial', 20, bold=True, italic=False) addressFont = font.SysFont('arial', 14, bold=False, italic=True) date = datetime.datetime.now().strftime('%b %d, %Y %I:%M %p') srfDate = dateFont.render(date, True, (255,255,255)) srfDateShadow = dateFont.render(date, True, (0,0,0)) address = 'Boulevard East, Weehawken, NJ' srfAddress = addressFont.render(address, True, (255,255,255)) srfAddressShadow = addressFont.render(address, True, (0,0,0)) image.blit(srfDateShadow, (11, 11)) image.blit(srfDate, (10,10)) image.blit(srfAddressShadow, (11, 36)) image.blit(srfAddress, (10, 35))
def image_at(self, area): image = Surface((self.width, self.height)) image.blit(self.image, (0, 0), area) return image
def cropImage(self, x, y, w, h): image = Surface((w, h), SRCALPHA) image.blit(self.spritesheet, (0, 0), (x, y, w, h)) return image
def render(self): """Render the text, returning the affected rect.""" pad = 3 maxdist = 200 screen_rect = Rect(0, 0, 640, 480 - (linesize(_font)+2)) size = linesize(_font) # Try not overlapping anything balloonrects = map(lambda s: s.rect, self.group.list) try: balloonrects.remove(self.rect) except ValueError: pass # First, try to avoid everything rects1 = invertrect(screen_rect, balloonrects + map(lambda s: s.rect, self.avgroup.list)) rects1.sort(self.nearer) # Next, try to avoid just balloons and my av rects2 = invertrect(screen_rect, balloonrects + [self.avatar.rect]) rects2.sort(self.nearer) # Finally, just avoid my own av rects3 = invertrect(screen_rect, [self.avatar.rect]) rects3.sort(self.nearer) rects = filter(lambda r,p=self.pos,m=maxdist: rectdist(r,p) < m, rects1 + rects2 + rects3) + [screen_rect] notdone = 1 while notdone: # Loop until we can fit the balloon into *some* rect for r in rects: try: lines = wrap_lines(self.text, r.width-pad*2, _font) except ValueError: continue if len(lines) * size + pad*2 < r.height: notdone = 0 break else: # Couldn't render the balloon, delete some lines del self.text[0] del self.timeouts[0] # Count the number of lines in each sequence of text surfaces = map(lambda t,f=_font,c=self.fgcolor: f.render(t, 1, c), lines) width = max(map(lambda l: _font.size(l)[0], lines)) + (pad * 2) height = len(surfaces) * size + (pad * 2) x, y = self.pos rect = Rect(x-width/2, y-height/2, width, height).clamp(r) bigrect = rect.union((self.pos, (1, 1))) image = pygame.Surface(bigrect.size) image.set_colorkey((255,165,0)) image.fill((255,165,0)) cx, cy = closest(rect, self.pos) cx = cx - bigrect.left cy = cy - bigrect.top x = x - bigrect.left y = y - bigrect.top left = rect.left - bigrect.left right = rect.right - bigrect.left - 1 top = rect.top - bigrect.top bottom = rect.bottom - bigrect.top - 1 # Calculate arcs for corners nwarc = arc(pad, (left+pad, top+pad), pi*1.5, pi*2, 5) nearc = arc(pad, (right-pad, top+pad), 0, pi*0.5, 5) searc = arc(pad, (right-pad, bottom-pad), pi*0.5, pi, 5) swarc = arc(pad, (left+pad, bottom-pad), pi, pi*1.5, 5) # Go in a clockwise direction if x > right-pad*2: # Drawing to the east if y > bottom-pad*2: # Draw the arrow to the southeast points = ((right, bottom-pad), (x, y), (right-pad, bottom)) + swarc + nwarc + nearc elif y < top+pad*2: # Draw the arrow to the northeast points = ((right-pad, top), (x, y), (right, top+pad)) + searc + swarc + nwarc elif x > right: # Due east points = ((right, cy-pad), (x, y), (right, cy+pad)) + \ searc + swarc + nwarc + nearc else: # Arrow is inside balloon points = searc + swarc + nwarc + nearc elif x < left+pad*2: # Drawing to the west if y > bottom-pad*2: # Southwest points = ((left+pad, bottom), (x, y), (left, bottom-pad)) + \ nwarc + nearc + searc elif y < top+pad*2: # Northwest points = ((left, top+pad), (x, y), (left+pad, top)) + \ nearc + searc + swarc elif x < left: # Due west points = ((left, cy+pad), (x, y), (left, cy-pad)) + \ nwarc + nearc + searc + swarc else: # Arrow is inside balloon points = nwarc + nearc + searc + swarc elif y < top: # Due north points = ((cx-pad, top), (x, y), (cx+pad, top)) + \ nearc + searc + swarc + nwarc elif y > bottom: # Due south points = ((cx+pad, bottom), (x, y), (cx-pad, bottom)) + \ swarc + nwarc + nearc + searc else: print >> sys.stderr, 'Oops, arrow point is inside balloon!' points = swarc + nwarc + nearc + searc pygame.draw.polygon(image, (255,255,255), points, 0) pygame.draw.polygon(image, (0,0,0), points, 1) y = rect.top - bigrect.top + pad x = rect.left - bigrect.left for s in surfaces: image.blit(s, (((width - s.get_width())/2+x), y)) y = y + size self.image = image #rect = rect.clamp((0, 0, 640, 480)) self.rect = bigrect return bigrect