本文整理汇总了Python中pygame.Rect.contains方法的典型用法代码示例。如果您正苦于以下问题:Python Rect.contains方法的具体用法?Python Rect.contains怎么用?Python Rect.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.Rect
的用法示例。
在下文中一共展示了Rect.contains方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_contains
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
def test_contains(self):
r = Rect(1, 2, 3, 4)
self.failUnless(r.contains(Rect(2, 3, 1, 1)), "r does not contain Rect(2,3,1,1)")
self.failUnless(r.contains(Rect(r)), "r does not contain the same rect as itself")
self.failUnless(r.contains(Rect(2, 3, 0, 0)), "r does not contain an empty rect within its bounds")
self.failIf(r.contains(Rect(0, 0, 1, 2)), "r contains Rect(0,0,1,2)")
self.failIf(r.contains(Rect(4, 6, 1, 1)), "r contains Rect(4,6,1,1)")
self.failIf(r.contains(Rect(4, 6, 0, 0)), "r contains Rect(4,6,0,0)")
示例2: _get_group_bounding
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
def _get_group_bounding(tag_store, sizeRect):
if not isinstance(sizeRect, pygame.Rect):
sizeRect = Rect(0, 0, sizeRect[0], sizeRect[1])
if tag_store:
rects = [tag.rect for tag in tag_store]
union = rects[0].unionall(rects[1:])
if sizeRect.contains(union):
return union
return sizeRect
示例3: test_fit
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
def test_fit(self):
# __doc__ (as of 2008-08-02) for pygame.rect.Rect.fit:
# Rect.fit(Rect): return Rect
# resize and move a rectangle with aspect ratio
#
# Returns a new rectangle that is moved and resized to fit another.
# The aspect ratio of the original Rect is preserved, so the new
# rectangle may be smaller than the target in either width or height.
r = Rect(10, 10, 30, 30)
r2 = Rect(30, 30, 15, 10)
f = r.fit(r2)
self.assertTrue(r2.contains(f))
f2 = r2.fit(r)
self.assertTrue(r.contains(f2))
示例4: Unit
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
class Unit(object):
def __init__(self, player, coord, ai, hp, speed):
"""
Arguments:
- `player`: The player that this unit fights for.
- `coord`: The coordinates of the unit.
- `ai`: An AI object to control the unit.
"""
self.player = player
self.rect = Rect(coord[0] - 5, coord[1] - 5, 10, 10)
self.coord = coord
self.ai = ai
self.hp = hp
self.speed = speed
def move(self, world, dest):
"Move along the shortest path to the destination"
if dist(self.rect.center, dest) <= self.speed:
self.coord = dest
self.rect.center = dest
else:
if dest == self.rect.center:
return
a = dest[0] - self.coord[0]
b = dest[1] - self.coord[1]
d = math.sqrt(a ** 2 + b ** 2) / self.speed
self.coord = (self.coord[0] + a / d, self.coord[1] + b / d)
self.rect.center = self.coord
def act(self, world):
raise NotImplemented("%s has not implemented act()" % type(self))
def blit(self, screen):
raise NotImplemented("%s has not implemented blit()" % type(self))
def contains(self, p):
if type(p) == pygame.Rect:
return self.rect.contains(p)
else:
return self.rect.contains(pygame.Rect(p[0], p[1], 1, 1))
示例5: Canvas
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
class Canvas(object):
def __init__(self, size, slot_size):
self.area_rect = Rect((0,0), size)
self.slot_size = slot_size
self._rects_of_slot = {}
self._slots_of_entry = {}
def _slot_keys_of_rect(self, rect):
slot_width, slot_height = self.slot_size
for x in xrange(rect.left//slot_width, 1+rect.right//slot_width):
for y in xrange(rect.top//slot_height, 1+rect.bottom//slot_height):
yield x, y
def _add(self, rect, entry):
if entry in self._slots_of_entry:
raise EntryAlreadyExists("Entry already in Canvas")
assert entry not in self._slots_of_entry
assert self.area_rect.contains(rect)
self._slots_of_entry[entry] = slots = []
for slot_key in self._slot_keys_of_rect(rect):
self._rects_of_slot.setdefault(slot_key, {})[entry] = rect
slots.append(slot_key)
def add(self, entry):
self._add(entry.bounding_rect(), entry)
def remove(self, entry):
for slot_key in self._slots_of_entry[entry]:
del self._rects_of_slot[slot_key][entry]
del self._slots_of_entry[entry]
def clear(self):
self._rects_of_slot.clear()
self._slots_of_entry.clear()
def _rect_collisions(self, rect):
for slot_key in self._slot_keys_of_rect(rect):
for entry, cur_rect in self._rects_of_slot.get(slot_key, {}).iteritems():
if rect.colliderect(cur_rect):
yield entry
def collisions(self, entry):
for other_entry in self._rect_collisions(entry.bounding_rect()):
if entry.collides_with(other_entry):
yield other_entry
def items(self):
return self._slots_of_entry.iterkeys()
示例6: collision_check
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
def collision_check(self):
""" Method for checking if the entity has run into a tree or something
and move it back a pixel if it has
"""
if self.wall_collides:
# Move the entity inside of the window (border collision)
entity_rect = Rect(self.x, self.y, self.width,self.height)
window_rect = Rect(0, 0, g.width * c.TILE_SIZE, g.height * c.TILE_SIZE)
if not window_rect.contains(entity_rect):
entity_rect.clamp_ip(window_rect)
self.x = entity_rect.left
self.y = entity_rect.top
collided = False
if self.collides:
# Make sure collision rectangles are up to date
self.update_collision_rects()
# Get the tile the entity is standing on
tile_pos = self.get_tile()
checked_tiles = []
# Loop through a 3x3 tile square around the entity, to not check the entire map
for i in range(tile_pos[0] - 1, tile_pos[0] + 2):
for j in range(tile_pos[1] - 1, tile_pos[1] + 2):
try:
if c.IMAGES[g.map[i][j].type].collides:
checked_tiles.append(g.map[i][j].rect())
except IndexError:
# That index was apparently outside of the map
pass
# Check if each of the zones collides with any of the tiles
# If so, move it in the appropriate direction, specified in update_collision_rects() as the keys
for rect in self.rects:
if self.rects[rect].collidelist(checked_tiles) != -1:
self.x += rect[0]
self.y += rect[1]
collided = True
self.collided = collided
示例7: MapBase
# 需要导入模块: from pygame import Rect [as 别名]
# 或者: from pygame.Rect import contains [as 别名]
#.........这里部分代码省略.........
self.map_tile_coverage.right):
column = self.tiles[col]
for row in range(self.map_tile_coverage.top, \
self.map_tile_coverage.bottom):
blit(column[row].tile, (x,y), rect)
y = y + TILE_SIZE
x = x + TILE_SIZE
y = 0
def init(self):
self.offset.width = core.screen.get_rect().width
self.offset.height = core.screen.get_rect().height
self.entities.run_command('enter_map')
def handle_event(self,event):
if event.type == PUSH_ACTION_EVENT: self.character_activate()
if event.type == PUSH_ACTION2_EVENT: menu.run_main_menu()
if event.type == QUIT_EVENT: core.wm.running = False
def check_heal(self):
self.heal_points = self.heal_points + 1
if self.heal_points >= self.regen_rate:
core.game.save_data.hero.regenerate()
self.heal_points = 0
def character_activate(self):
if not self.character.moving:
target = add(self.character.pos,MOVE_VECTORS[self.character.facing])
entities = self.non_passable_entities.entity_collisions(target)
for e in entities:
e.activate()
def move_character(self, dir):
self.character.move(dir)
if not self.scrolling:
if self.character.moving:
nsr = self.map_non_scroll_region
x,y = self.character.pos
if dir % 2 == 0: # North or south
if y < nsr.bottom and \
y >= nsr.top:
return
if y < SCROLL_EDGE or \
y >= self.height - SCROLL_EDGE:
return
self.scroll_axis = 1
else: # East or west
if x < nsr.right and \
x >= nsr.left:
return
if x < SCROLL_EDGE or \
x >= self.width - SCROLL_EDGE:
return
self.scroll_axis = 0
self.scrolling = True
vector = MOVE_VECTORS[dir]
self.map_non_scroll_region.move_ip(vector)
old_viewport = self.viewport
self.viewport = old_viewport.move(vector)
self.scroll_anchor = self.character.rect[self.scroll_axis]
if not self.map_tile_coverage.contains(self.viewport):
self.calculate_tile_coverage(old_viewport)
self.dirty()
def dirty(self):
for f in range(4):
self.map_frames_dirty[f] = True
def move_ok(self, target_pos, character):
x, y = target_pos
target = self.get(x,y)
if target is not None and target.is_walkable():
entities = self.non_passable_entities.entity_collisions(target_pos)
if len(entities) > 0:
if character.can_trigger_actions:
for e in entities:
e.touch()
else:
for e in entities:
if e.can_trigger_actions: character.touch()
return 0
else:
self.sound.play()
return 1
else:
return 0
def get_tiles_from_ascii(self,ascii,tile_map):
for y in range(self.height):
line = ascii[y]
for x in range(self.width):
c = line[x]
args = tile_map[c]
pos = None
if len(args) > 1:
pos = args[1]
self.set_location( (x,y), args[0],
tile_map['walkable'].find(c)!=-1, pos )