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 update_body(self): cell.Cell.update_body(self) val = self.input("A0") * 1 val += self.input("A1") * 2 val += self.input("A2") * 4 val += self.input("A3") * 8 val += self.input("A4") * 16 val += self.input("A5") * 32 val += self.input("A6") * 64 val += self.input("A7") * 128 h = int(self.rect_rel.height / 3); pos = Rect(self.rect_rel) pos.height = h self.parent.draw_text(self.surface, "%02X" % val, pos) pos = Rect(self.rect_rel) pos.y = h * 1 pos.height = h self.parent.draw_text(self.surface, "%03u" % val, pos) if (val < 0b01111111): sig = val else: sig = -(256 - val) pos = Rect(self.rect_rel) pos.y = h * 2 pos.height = h self.parent.draw_text(self.surface, "%+04d" % sig, pos)
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 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 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 setSlider(self, ratio): ''' Create a Rectangle corresponding to the slider position position will be relative to the surface so modify the top acc. to ratio ''' rect = Rect( (0, 0), self.rect.size ) # set position to origin as fill rect is relative to surface rect.top = rect.height - int(rect.height * ratio) # change position rect.height = int(rect.height * ratio) # change size self.surf.fill(TEAL, rect=rect) # now fill in what's remaining with background brect = Rect((0, 0), self.rect.size) brect.height = rect.top self.surf.fill(BLACK, rect=brect)
def test_height(self): "Changing the height resizes the rect from the top-left corner" r = Rect(1, 2, 3, 4) new_height = 10 old_topleft = r.topleft old_width = r.width r.height = new_height self.assertEqual(new_height, r.height) self.assertEqual(old_width, r.width) self.assertEqual(old_topleft, r.topleft)
def test_height( self ): "Changing the height resizes the rect from the top-left corner" r = Rect( 1, 2, 3, 4 ) new_height = 10 old_topleft = r.topleft old_width = r.width r.height = new_height self.assertEqual( new_height, r.height ) self.assertEqual( old_width, r.width ) self.assertEqual( old_topleft, r.topleft )
def update_body(self): cell.Cell.update_body(self) h = int(self.rect_rel.height / 4); pos = Rect(self.rect_rel) pos.height = h self.parent.draw_text(self.surface, "MEMORY", pos) pos = Rect(self.rect_rel) pos.y = h * 1 pos.height = h self.parent.draw_text(self.surface, self.filename, pos) pos = Rect(self.rect_rel) pos.y = h * 2 pos.height = h self.parent.draw_text(self.surface, "address: %04X" % self.address, pos) pos = Rect(self.rect_rel) pos.y = h * 3 pos.height = h self.parent.draw_text(self.surface, "data: %02X" % self.data, pos)
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 draw(self, screen, camera_position): blit_position = Rect(-camera_position.left, 0, camera_position.width, camera_position.height) area_vertical = (self.rect.height - self.camera_height + camera_position.top) % (-self.rect.height) screen.blit(self.image, blit_position, area=Rect(self.area_horizontal, area_vertical, self.camera_width, self.camera_height)) if area_vertical < 0: blit_position.height = -area_vertical new_area_vertical = self.rect.height + area_vertical screen.blit(self.image, blit_position, area=Rect(self.area_horizontal, new_area_vertical, self.camera_width, -area_vertical))
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
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"
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:
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