本文整理匯總了Python中rect.Rect類的典型用法代碼示例。如果您正苦於以下問題:Python Rect類的具體用法?Python Rect怎麽用?Python Rect使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Rect類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TextItem
class TextItem(DocItem):
"""Prints some form of text"""
def __init__(self, text = '', style = 'body'):
DocItem.__init__(self)
self.text = text
self.style = style
def writePDF(self, pdf = None):
"""Write itself to a FPDF object.
Args:
pdf (FPDF): the FPDF object to write to.
"""
return self.getText()
def resizePDF(self, pdf, x = 0, y = 0):
"""Resize internal Rect according to current settings of pdf"""
width = pdf.get_string_width( self.getText() )
height = pdf.font_size_pt / pdf.k
self.rect = Rect( x, y, x + width, y + height )
def cellPDF(self, pdf, r = None):
if r:
x_shift = r.x0()
y_shift = r.y0()
else:
x_shift = 0.0
y_shift = 0.0
pdf.set_y( self.rect.y0() - y_shift )
pdf.set_x( self.rect.x0() - x_shift )
pdf.cell( self.rect.width(), self.rect.height(), self.getText() )
def refit(self):
"""Doesn't need to do anything as cellPDF uses self.rect to output the content"""
pass
示例2: __init__
def __init__(self, x, y, w, h, objects = [], creatures = [],
start = False):
self.inner_rect = Rect.from_dimensions(x + WALL_WIDTH, y + int(WALL_WIDTH * 1.25),
w + WALL_WIDTH, h + int(WALL_WIDTH * 1.5))
self.outer_rect = Rect.from_dimensions(x, y,
2 * WALL_WIDTH + w,
2 * WALL_WIDTH + h)
示例3: __init__
def __init__(self, game, zone):
self.game = game
self.zone = zone
self.screen = self.game.screen
self.battles = [x for x in game.battles if x.zone == self.zone]
self.targets = [x for x in game.targets if x.zone == self.zone]
self.terrain = [x for x in game.terrain if x.zone == self.zone]
# Determine the size required for the map surface
map_size_rect = Rect()
for ter in self.terrain:
map_size_rect.union(ter.rect)
self.map_surface = pygame.Surface(map_size_rect.size)
# Draw all static terrain and targets to the map surface
for ter in self.terrain:
if ter.static:
self.map_surface.fill((255, 0, 0), ter)
ter.draw(self.map_surface)
for tar in self.targets:
if tar.static:
tar.draw(self.map_surface)
示例4: dredgeRiverSingle
def dredgeRiverSingle(nature,direction,CX,CY):
rivert=[]
# This is the start coordinate of the river, perpendicular to the
# axis of the river. N/S rivers choose positions on the E/W axis,
# and vice-versa
axis_size = CX if direction else CY
line_pos=random.randint(0, axis_size)
#river width, no more than half the map
river_size=min(random.randint(3,120), axis_size//2)
for i in range(0,axis_size):
if direction:
element=Rect(Point(i,line_pos),Point(i,line_pos+river_size))
else:
element=Rect(Point(line_pos,i),Point(line_pos+river_size,i))
element.set_name('RIVER')
rivert.append(element)
prev=0;
for f in rivert:
prev=prev+random.randint(-1,1)
f.move(direction+2,prev) # in rect, 1=up, 2=right
nature.extend(rivert)
示例5: getOuterRect
def getOuterRect(self):
r = Rect()
r.size = (
self.marginLeft + self.image.size[0] + self.marginRight,
self.marginTop + self.image.size[1] + self.marginBottom,
)
return r
示例6: MathPower
class MathPower(MultiItem):
"""Container for inline maths"""
def __init__(self):
MultiItem.__init__(self)
self.style = 'math-var', 1
def resizePDF(self, pdf, x = 0, y = 0):
if len(self.items) < 2 or not self.items[0] or not self.items[1]:
raise Exception('MathPower must have two items.')
self.rect = Rect(x,y,x,y)
dx = pdf.get_string_width(' ') * self.style[1]
base = self.items[0]
if hasattr(base,'style'):
setFontPDF(pdf, base.style, self.styles)
base.resizePDF(pdf,x,y)
index = self.items[1]
index.scaleFont(0.8)
if hasattr(index,'style'):
setFontPDF(pdf, index.style, self.styles)
index.resizePDF(pdf, base.rect.x1() + dx, y - base.rect.height() * 0.4)
self.rect.unite(base.rect)
self.rect.unite(index.rect)
self.refit()
示例7: InlineMathBlock
class InlineMathBlock(MultiItem):
"""Container for inline maths"""
def __init__(self, *items):
MultiItem.__init__(self)
self.style = ('math-var',1)
for item in items:
self.appendItem(item)
def resizePDF(self, pdf, x = 0, y = 0):
self.rect = Rect(x,y,x,y)
dx = pdf.get_string_width(' ')
dx *= self.style[1]
rectList = []
width = 0.0
style = ''
for item in self.items:
if item:
if hasattr(item,'style') and item.style != style:
setFontPDF(pdf, item.style, self.styles)
item.resizePDF(pdf,x,y)
rectList.append(item.rect)
width += item.rect.width() + dx
rect.alignLeft(rectList, x, x+width, dx)
for r in rectList:
self.rect.unite(r)
self.refit()
示例8: MathSubSuperscript
class MathSubSuperscript(MultiItem):
"""An item with a subscript"""
def __init__(self, base, subscript = None, superscript = None):
MultiItem.__init__(self)
self.appendItem(base)
self.base = base
self.subscript = subscript
self.superscript = superscript
if subscript:
subscript.scaleFont(0.8)
self.appendItem(subscript)
if superscript:
superscript.scaleFont(0.8)
self.appendItem(superscript)
def resizePDF(self, pdf, x = 0, y = 0):
self.resizeItemsPDF(pdf, x, y)
dx = pdf.get_string_width(' ') * self.style[1]
self.rect = Rect(x,y,x,y)
h = self.base.rect.height()
w = self.base.rect.width() + dx
if self.subscript:
self.subscript.rect.translate(w, h*0.5)
self.rect.unite(self.subscript.rect)
if self.superscript:
self.superscript.rect.translate(w, - h*0.5)
self.rect.unite(self.superscript.rect)
self.refit()
示例9: update
def update(self):
last = Rect(*(self.pos + self.size))
dx = 0
if keys.get(Keyboard.keycodes['left']):
dx -= 2 * params.scale
if keys.get(Keyboard.keycodes['right']):
dx += 2 * params.scale
if keys.get(Keyboard.keycodes['spacebar']) and self.resting:
self.dy = 9 * params.scale
self.resting = False
self.dy = max(-8 * params.scale, self.dy - .5 * params.scale)
self.x += dx
self.y += self.dy
new = Rect(*(self.pos + self.size))
for cell in self.map.layers['objects'].collide(new, 'blocker'):
blocker = cell['blocker']
if 'l' in blocker and last.right <= cell.left and new.right > cell.left:
new.right = cell.left
if 'r' in blocker and last.left >= cell.right and new.left < cell.right:
new.left = cell.right
if 't' in blocker and last.bottom >= cell.top and new.bottom < cell.top:
self.resting = True
new.bottom = cell.top
self.dy = 0
if 'b' in blocker and last.top <= cell.bottom and new.top > cell.bottom:
new.top = cell.bottom
self.dy = 0
self.pos = new.bottomleft
示例10: aggro_check
def aggro_check(self, size):
aggro_range = self.map.tile_width * size
offset = (self.map.tile_width * (size / 2))
collided = []
for player in self.player_characters:
rect = Rect(player.x - offset, player.y - offset, aggro_range, aggro_range)
collided += [e for e in self.entities if rect.intersect(Rect(*e.pos + e.size)) and e.data.type == 'enemy']
return collided
示例11: get_rect
def get_rect(self, **attr):
"""
Return rect of the surface.
An optional keyword argument of the rect position.
"""
rect = Rect(0, 0, self.width, self.height)
for key in attr:
rect.__setattr__(key,attr[key])
return rect
示例12: make_map
def make_map(self):
#fill map with "blocked" tiles
self.map = [[ Tile(True)
for y in range(self.height) ]
for x in range(self.width) ]
#create two rooms
rooms = []
num_rooms = 0
for r in range(self.max_rooms):
#random width and height
w = libtcod.random_get_int(0, self.room_min_size, self.room_max_size)
h = libtcod.random_get_int(0, self.room_min_size, self.room_max_size)
#random position without going out of the boundaries of the map
x = libtcod.random_get_int(0, 0, self.width - w - 1)
y = libtcod.random_get_int(0, 0, self.height - h - 1)
#"Rect" class makes rectangles easier to work with
new_room = Rect(x, y, w, h)
#run through the other rooms and see if they intersect with this one
#if we find an intersection we need to generate a new room
# if len(list(filter(new_room.intersect, rooms))) > 0:
# continue
#"paint" it to the map's tiles
self.create_room(new_room)
#center coordinates of new room, will be useful later
(new_x, new_y) = new_room.center()
if num_rooms == 0:
#this is the first room, where the player starts at
self.starting_pos = (new_x, new_y)
else:
#all rooms after the first:
#connect it to the previous room with a tunnel
#center coordinates of previous room
(prev_x, prev_y) = rooms[num_rooms-1].center()
#draw a coin (random number that is either 0 or 1)
if libtcod.random_get_int(0, 0, 1) == 1:
#first move horizontally, then vertically
self.create_h_tunnel(prev_x, new_x, prev_y)
self.create_v_tunnel(prev_y, new_y, new_x)
else:
#first move vertically, then horizontally
self.create_v_tunnel(prev_y, new_y, prev_x)
self.create_h_tunnel(prev_x, new_x, new_y)
#finally, append the new room to the list
rooms.append(new_room)
num_rooms += 1
示例13: create_leaf
def create_leaf(cls, rooto, leaf_obj, leaf_rect):
rect = Rect(leaf_rect.x, leaf_rect.y, leaf_rect.xx, leaf_rect.yy)
rect.swapped_x = True # Mark as leaf by setting the xswap flag.
res = _NodeCursor.create(rooto, rect)
idx = res.index
res.first_child = rooto.leaf_count
rooto.leaf_count += 1
res.next_sibling = 0
rooto.leaf_pool.append(leaf_obj)
res._save_back()
res._become(idx)
assert (res.is_leaf())
return res
示例14: generate_background
def generate_background(pict, bbxes, min_height, min_width):
for i in xrange(N_TRIALS):
rand_bbx = Rect(pict.shape[0], pict.shape[1])
intersects = True
if rand_bbx.height < min_height or rand_bbx.width < min_width:
continue
intersects = False
for bbx in bbxes:
intersects = intersects or rand_bbx.intersects(bbx)
if not intersects:
break
if not intersects:
return pict[rand_bbx.ymin:rand_bbx.ymax,rand_bbx.xmin:rand_bbx.xmax]
else:
return None
示例15: rectOverlaps
def rectOverlaps(self, rect):
minX,minY,maxX,maxY = self.getTileBoundsInclusive(rect)
tw = self.tileWidth
th = self.tileHeight
for layer in self.collideLayers:
for y in range(minY,maxY+1):
for x in range(minX,maxX+1):
idx = x + y*layer.width
if layer.tileGIDs[idx] != 0:
r = Rect(x*tw, y*th, tw,th)
if r.intersects(rect):
return True
return False