本文整理汇总了Python中pygame.Rect.width方法的典型用法代码示例。如果您正苦于以下问题:Python Rect.width方法的具体用法?Python Rect.width怎么用?Python Rect.width使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.Rect
的用法示例。
在下文中一共展示了Rect.width方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_item_at_pos
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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
示例2: create_platforms_rect
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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)
示例3: draw_sprite
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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)
示例4: test_width
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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 )
示例5: makeRooms
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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)
示例6: _DrawTextLines
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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
示例7: calculateIntersectPoint
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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
示例8: intersection_pt
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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"
示例9: Rect
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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:
示例10: calculateIntersectPoint
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import width [as 别名]
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