def get_item_at_pos (self, position): """L.get_item_at_pos (...) -> ListItem Gets the item at the passed position coordinates. """ eventarea = self.rect_to_client () if not eventarea.collidepoint (position): return None position = position[0] - eventarea.left, position[1] - eventarea.top border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("ACTIVE_BORDER")) * 2 posy = self.vadjustment items = self.scrolledlist.items width = eventarea.width bottom = eventarea.bottom images = self.images spacing = self.scrolledlist.spacing for item in items: rect = Rect (images [item][1]) rect.y = posy rect.width = width + border if rect.bottom > bottom: rect.height = bottom - rect.bottom + border if rect.collidepoint (position): return item posy += images[item][1].height + spacing + border return None
def calculateIntersectPoint(p1, p2, p3, p4): p = getIntersectPoint(p1, p2, p3, p4) if p is not None: width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width, height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() tolerance = 1 if r1.width < tolerance: r1.width = tolerance if r1.height < tolerance: r1.height = tolerance if r2.width < tolerance: r2.width = tolerance if r2.height < tolerance: r2.height = tolerance for point in p: try: res1 = r1.collidepoint(point) res2 = r2.collidepoint(point) if res1 and res2: point = [int(pp) for pp in point] return point except: str = "point was invalid ", point print(str) return None else: return None
def calculateIntersectPoint(p1, p2, p3, p4): p = getIntersectPoint(p1, p2, p3, p4) #print p if p is not None: width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width , height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() # Ensure both rects have a width and height of at least 'tolerance' else the # collidepoint check of the Rect class will fail as it doesn't include the bottom # and right hand side 'pixels' of the rectangle tolerance = 1 if r1.width < tolerance: r1.width = tolerance if r1.height < tolerance: r1.height = tolerance if r2.width < tolerance: r2.width = tolerance if r2.height < tolerance: r2.height = tolerance for point in p: try: res1 = r1.collidepoint(point) res2 = r2.collidepoint(point) if res1 and res2: point = [int(pp) for pp in point] return point except: # sometimes the value in a point are too large for PyGame's Rect class #str = "point was invalid ", point #print str pass # This is the case where the infinately long lines crossed but # the line segments didn't return None else: return None
def __draw_bar(self, camera, arg_rect, fraction, col_back, col_0, col_1): """ Draw a progress bar """ # The background self.__renderer.add_job_rect(arg_rect, colour=col_back, coords=Renderer.COORDS_SCREEN, level=Renderer.LEVEL_FORE, brightness=0.2) # The empty portion. rect = Rect(arg_rect) rect.inflate_ip(-4, -4) self.__renderer.add_job_rect(rect, colour=col_0, coords=Renderer.COORDS_SCREEN, level=Renderer.LEVEL_FORE, brightness=0.2) # The full portion. rect.width = int(fraction * rect.width) self.__renderer.add_job_rect(rect, colour=col_1, coords=Renderer.COORDS_SCREEN, level=Renderer.LEVEL_FORE, brightness=0.2)
def get_item_at_pos(self, position): """L.get_item_at_pos (...) -> ListItem Gets the item at the passed position coordinates. """ eventarea = self.rect_to_client() if not eventarea.collidepoint(position): return None position = position[0] - eventarea.left, position[1] - eventarea.top border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("ACTIVE_BORDER")) * 2 posy = self.vadjustment items = self.scrolledlist.items width = eventarea.width bottom = eventarea.bottom images = self.images spacing = self.scrolledlist.spacing for item in items: rect = Rect(images[item][1]) rect.y = posy rect.width = width + border if rect.bottom > bottom: rect.height = bottom - rect.bottom + border if rect.collidepoint(position): return item posy += images[item][1].height + spacing + border return None
def create_platforms_rect(self, tiles, layer): """ Create a collision rect for each non-empty tile segment, that is, one or more tiles together """ if len(tiles) < 1: return False x, y = tiles.pop(0) width = self.map.content['tilewidth'] height = self.map.content['tileheight'] rect = Rect((0, 0, 0, 0)) rect.left = x * width rect.top = y * height rect.width = width rect.height = height # self.platforms.append(Platform(rect, layer)) n_tiles_x, n_tiles_y = 0, 0 # exclude the tiles we're about to create a platform for excluded_tiles = [] for (n_x, n_y) in tiles: if abs(n_x - x) == n_tiles_x and n_y == y: rect.width += width n_tiles_x += 1 excluded_tiles.append((n_x, n_y)) if abs(n_y - y) == n_tiles_y and n_x == x: rect.height += height n_tiles_y += 1 excluded_tiles.append((n_x, n_y)) self.platforms.append(Platform(rect, layer)) self.create_platforms_rect([t for t in tiles if not t in excluded_tiles], layer)
def draw_sprite(self, sprite, ratio=None): if ratio is None: ratio = self.ratio # calculate the size of the shadow w = sprite.rect.width * ratio + 1 h = w/2 rect = Rect(0,0,w,h) # shrink the shadow according to height if hasattr(sprite, "height"): height0 = sprite.__class__.height ratio = (height0 - sprite.height) / float(height0) rect.width = max(8, rect.width * ratio) rect.height = max(4, rect.height * ratio) # move the the midbottom of the sprite rect.center = sprite.rect.midbottom rect.x -= 1 rect.y -= 3 # if the sprite has a height property, use it to move the shadow if hasattr(sprite, "height"): rect.y += sprite.height # draw to the layer pygame.draw.ellipse(self._layer, self.color, rect)
def test_width( self ): "Changing the width resizes the rect from the top-left corner" r = Rect( 1, 2, 3, 4 ) new_width = 10 old_topleft = r.topleft old_height = r.height r.width = new_width self.assertEqual( new_width, r.width ) self.assertEqual( old_height, r.height ) self.assertEqual( old_topleft, r.topleft )
def test_width(self): "Changing the width resizes the rect from the top-left corner" r = Rect(1, 2, 3, 4) new_width = 10 old_topleft = r.topleft old_height = r.height r.width = new_width self.assertEqual(new_width, r.width) self.assertEqual(old_height, r.height) self.assertEqual(old_topleft, r.topleft)
def makeRooms(self): numRooms = random.randint(NUMROOMSMIN, NUMROOMSMAX) for roomID in range(numRooms): attempts = 0 room = Rect(0,0,0,0) room.width = random.randint(ROOMMIN, ROOMMAX) room.height = random.randint(ROOMMIN, ROOMMAX) posFound = False while not posFound: if attempts == MAXROOMALLOCATTEMPTS: #print("Could not resolve room placement for room %i, bailing" % (roomID+1)) break room.x = random.randint(2, WORLDSIZE[0] - 1) room.y = random.randint(5, WORLDSIZE[1] - 1) if (room.x + room.width) >= (WORLDSIZE[0] - 1) or (room.y + room.height) >= (WORLDSIZE[1] - 4): attempts += 1 continue posFound = True for r,w in self.roomInfo: if r.inflate(2*ROOMSPACING, 2*ROOMSPACING).colliderect(room): posFound = False attempts += 1 break if not posFound: continue #Place waypoint wpX = random.randint(room.x + 1 + int(CORTHICKNESS / 2), room.x + room.width - 1 - int(CORTHICKNESS / 2)) wpY = random.randint(room.y + 1 + int(CORTHICKNESS / 2), room.y + room.height - 1 - int(CORTHICKNESS / 2) ) self.roomInfo.append( (room, (wpX, wpY)) ) for x in range(room.x, room.x + room.width): for y in range(room.y, room.y + room.height): self.mapGrid[x][y] = "#" #Sort rooms in order of Y coordinates self.roomInfo.sort(key=lambda r: r[0].y)
def _DrawTextLines(self, lines): linepadding =1 textlines = [] totalrect = Rect(0,0,0,0) for line in lines: text = self.font.render(line, 1, (255,255,255)) rect = text.get_rect() totalrect.inflate_ip(0, rect.height + linepadding) if rect.width > totalrect.width: totalrect.width = rect.width textlines.append(text) textsurf = Surface(totalrect.size, pygame.SRCALPHA) topoffset = 0 for linesurf in textlines: textsurf.blit(linesurf,(0,topoffset)) topoffset += linesurf.get_rect().height return textsurf
def _DrawTextLines(self, lines): linepadding = 1 textlines = [] totalrect = Rect(0, 0, 0, 0) for line in lines: text = self.font.render(line, 1, (255, 255, 255)) rect = text.get_rect() totalrect.inflate_ip(0, rect.height + linepadding) if rect.width > totalrect.width: totalrect.width = rect.width textlines.append(text) textsurf = Surface(totalrect.size, pygame.SRCALPHA) topoffset = 0 for linesurf in textlines: textsurf.blit(linesurf, (0, topoffset)) topoffset += linesurf.get_rect().height return textsurf
def rounded_rect(surface: Surface, rect: Rect, colour: Colour, corner_radius: int): ''' Draw a rectangle with rounded corners. Would prefer this: pygame.draw.rect(surface, colour, rect, border_radius=corner_radius) but this option is not yet supported in my version of pygame so do it ourselves. We use anti-aliased circles to make the corners smoother ''' if rect.width < 2 * corner_radius or rect.height < 2 * corner_radius: raise ValueError( f"Both height (rect.height) and width (rect.width) must be > 2 * corner radius ({corner_radius})" ) corner_radius = int(corner_radius) # need to use anti aliasing circle drawing routines to smooth the corners circle(surface, rect.left + corner_radius, rect.top + corner_radius, corner_radius, colour) circle(surface, rect.right - corner_radius - 1, rect.top + corner_radius, corner_radius, colour) circle(surface, rect.left + corner_radius, rect.bottom - corner_radius - 1, corner_radius, colour) circle(surface, rect.right - corner_radius - 1, rect.bottom - corner_radius - 1, corner_radius, colour) rect_tmp = Rect(rect) rect_tmp.width -= 2 * corner_radius rect_tmp.center = rect.center draw_rect(surface, colour, rect_tmp) rect_tmp.width = rect.width rect_tmp.height -= 2 * corner_radius rect_tmp.center = rect.center draw_rect(surface, colour, rect_tmp)
def calculateIntersectPoint(p1, p2, p3, p4): p = getIntersectPoint(p1, p2, p3, p4) if p is not None: width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width , height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() # Ensure both rects have a width and height of at least 'tolerance' else the # collidepoint check of the Rect class will fail as it doesn't include the bottom # and right hand side 'pixels' of the rectangle tolerance = 1 if r1.width < tolerance: r1.width = tolerance if r1.height < tolerance: r1.height = tolerance
width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width , height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() # Ensure both rects have a width and height of at least 'tolerance' else the # collidepoint check of the Rect class will fail as it doesn't include the bottom # and right hand side 'pixels' of the rectangle tolerance = 1 if r1.width < tolerance: r1.width = tolerance if r1.height < tolerance: r1.height = tolerance if r2.width < tolerance: r2.width = tolerance if r2.height < tolerance: r2.height = tolerance for point in p: try: res1 = r1.collidepoint(point) res2 = r2.collidepoint(point) if res1 and res2:
def intersection_pt(p1, p2, p3, p4): p = getIntersectPoint(p1, p2, p3, p4) if p is not None: width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width , height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() # Ensure both rects have a width and height of at least 'tolerance' else the # collidepoint check of the Rect class will fail as it doesn't include the bottom # and right hand side 'pixels' of the rectangle tolerance = 1 if r1.width <tolerance: r1.width = tolerance if r1.height <tolerance: r1.height = tolerance if r2.width <tolerance: r2.width = tolerance if r2.height <tolerance: r2.height = tolerance for point in p: try: res1 = r1.collidepoint(point) res2 = r2.collidepoint(point) if res1 and res2: point = [int(pp) for pp in point] return point except: # sometimes the value in a point are too large for PyGame's Rect class str = "point was invalid ", point print str # This is the case where the infinately long lines crossed but # the line segments didn't return None else: return None # # Test script below... # if __name__ == "__main__": # # line 1 and 2 cross, 1 and 3 don't but would if extended, 2 and 3 are parallel # # line 5 is horizontal, line 4 is vertical # p1 = (1,5) # p2 = (4,7) # p3 = (4,5) # p4 = (3,7) # p5 = (4,1) # p6 = (3,3) # p7 = (3,1) # p8 = (3,10) # p9 = (0,6) # p10 = (5,6) # p11 = (472.0, 116.0) # p12 = (542.0, 116.0) # assert None != calculateIntersectPoint(p1, p2, p3, p4), "line 1 line 2 should intersect" # assert None != calculateIntersectPoint(p3, p4, p1, p2), "line 2 line 1 should intersect" # assert None == calculateIntersectPoint(p1, p2, p5, p6), "line 1 line 3 shouldn't intersect" # assert None == calculateIntersectPoint(p3, p4, p5, p6), "line 2 line 3 shouldn't intersect" # assert None != calculateIntersectPoint(p1, p2, p7, p8), "line 1 line 4 should intersect" # assert None != calculateIntersectPoint(p7, p8, p1, p2), "line 4 line 1 should intersect" # assert None != calculateIntersectPoint(p1, p2, p9, p10), "line 1 line 5 should intersect" # assert None != calculateIntersectPoint(p9, p10, p1, p2), "line 5 line 1 should intersect" # assert None != calculateIntersectPoint(p7, p8, p9, p10), "line 4 line 5 should intersect" # assert None != calculateIntersectPoint(p9, p10, p7, p8), "line 5 line 4 should intersect" # print "\nSUCCESS! All asserts passed for doLinesIntersect"
def calculateIntersectPoint(p1, p2, p3, p4): p = getIntersectPoint(p1, p2, p3, p4) if p is not None: width = p2[0] - p1[0] height = p2[1] - p1[1] r1 = Rect(p1, (width, height)) r1.normalize() width = p4[0] - p3[0] height = p4[1] - p3[1] r2 = Rect(p3, (width, height)) r2.normalize() # Ensure both rects have a width and height of at least 'tolerance' else the # collidepoint check of the Rect class will fail as it doesn't include the bottom # and right hand side 'pixels' of the rectangle tolerance = 1 if r1.width < tolerance: r1.width = tolerance if r1.height < tolerance: r1.height = tolerance if r2.width < tolerance: r2.width = tolerance if r2.height < tolerance: r2.height = tolerance for point in p: try: res1 = r1.collidepoint(point) res2 = r2.collidepoint(point) if res1 and res2: point = [int(pp) for pp in point] return point except: # sometimes the value in a point are too large for PyGame's Rect class str = "point was invalid ", point print(str) # This is the case where the infinately long lines crossed but # the line segments didn't return None else: return None