本文整理汇总了Python中pygame.Rect类的典型用法代码示例。如果您正苦于以下问题:Python Rect类的具体用法?Python Rect怎么用?Python Rect使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Rect类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: make_stats_box
def make_stats_box(screen, player, dungeon_level, box_x_start, box_heigth, box_width):
"""Create the box displaying the stats
@param screen: the screen to draw on
@param player: the player object
@param dungeon_level: the current dungeon level
@param box_x_start: rectangle upper left corner x-coordinate
@param box_heigth: height of rectangle
@param box_width: width of rectangle
"""
#create the rectangle
stats_box = Rect(box_x_start, 0, box_width, box_heigth)
#set font type
stats_font = font.SysFont('arial', 20)
#render game info
player_HP = stats_font.render("Hit Points: " + str(player.getHP()), True, Color('white'))
player_AP = stats_font.render("Attack Power: " + str(player.getAttackPower()), True, Color('white'))
player_Armor = stats_font.render("Armor: " + str(player.getArmor()), True, Color('white'))
level = stats_font.render("Dungeon Level: " + str(dungeon_level), True, Color('white'))
#For each line of text, draw it on the screen and move the rectangle for the next line
screen.fill(Color('Black'), stats_box)
screen.blit(player_HP, stats_box)
screen.blit(player_AP, stats_box.move(0, player_HP.get_height()))
screen.blit(player_Armor, stats_box.move(0, player_HP.get_height() + player_AP.get_height()))
screen.blit(level, stats_box.move(0, player_HP.get_height() + player_AP.get_height() + player_Armor.get_height()))
开发者ID:daniehol,项目名称:Roguelike,代码行数:26,代码来源:Gamescreen.py
示例2: __init__
class Button:
def __init__(self):
self.buttonOn = ButtonOn()
self.buttonOff = ButtonOff()
self.isOn = False;
self.rect = Rect(0,0,0,0)
def get_sprite(self):
if self.isOn:
return self.buttonOn
else:
return self.buttonOff
def set_x(self, x):
self.buttonOn.rect.x = x
self.buttonOff.rect.x = x
self.rect = self.buttonOn.rect
def set_y(self, y):
self.buttonOn.rect.y = y
self.buttonOff.rect.y = y
self.rect = self.buttonOn.rect
def check_pressed(self, pressed, x, y):
if pressed:
if self.rect.collidepoint(x, y):
self.isOn = True
return self.isOn
def check_point(self, x, y):
return self.rect.collidepoint(x, y)
def set_pressed(self, pressed):
self.isOn = pressed
开发者ID:MattGuerrette,项目名称:ZeroQuest,代码行数:35,代码来源:button.py
示例3: pick_rect
def pick_rect(points, rects):
ox, oy = sorted([ (sum(p), p) for p in points ])[0][1]
x = ox
y = oy
ex = None
while 1:
x += 1
if not (x, y) in points:
if ex is None:
ex = x - 1
if ((ox, y+1) in points):
if x == ex + 1 :
y += 1
x = ox
else:
y -= 1
break
else:
if x <= ex: y-= 1
break
c_rect = Rect(ox*tilewidth,oy*tileheight,\
(ex-ox+1)*tilewidth,(y-oy+1)*tileheight)
rects.append(c_rect)
rect = Rect(ox,oy,ex-ox+1,y-oy+1)
kill = [ p for p in points if rect.collidepoint(p) ]
[ points.remove(i) for i in kill ]
if points:
pick_rect(points, rects)
开发者ID:bitcraft,项目名称:lpc1,代码行数:35,代码来源:utils.py
示例4: get_item_at_pos
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
开发者ID:BGCX067,项目名称:eyestabs-svn-to-git,代码行数:31,代码来源:ListViewPort.py
示例5: bar
def bar(self, surface, text, pos, w=300):
rect = Rect(pos, (w, 60))
draw.rect(surface, (0, 200, 200), rect)
sf = font.SysFont('blah', 60, bold=False, italic=False)
txt = sf.render(text, True, (0, 0, 0))
surface.blit(txt, rect.inflate(-10, -10))
开发者ID:keltoff,项目名称:Demiurge,代码行数:7,代码来源:screen.py
示例6: Projectile
class Projectile(sprite.Sprite):
def __init__(self, pos, tower, target, image, speed, damage):
sprite.Sprite.__init__(self)
self.pos = pos
self.tower = tower
self.target = target
self.image = image
self.speed = speed
self.damage = damage
self.rect = Rect(self.pos, self.image.get_size())
self.angle = atan2((self.target.rect.centery-self.rect.centery),
(self.target.rect.centerx-self.rect.centerx))
self.x_speed = cos(self.angle)*self.speed
self.y_speed = sin(self.angle)*self.speed
def update(self, monsters, screen):
# Kills the projectile if it doesn't get there before the target dies
if self.target is None:
self.kill()
return
# Calculates where the projectile needs to go
self.angle = atan2((self.target.rect.centery-self.rect.centery),
(self.target.rect.centerx-self.rect.centerx))
distance = hypot(self.target.rect.centerx - self.rect.centerx,
self.target.rect.centery - self.rect.centery)
mod = self.target.speed+1
# Calculates the X and Y speed
xspeed, yspeed = cos(self.angle)*mod, sin(self.angle)*mod
self.x_speed = xspeed + xspeed/abs(xspeed) if xspeed != 0 else 0
self.y_speed = yspeed + yspeed/abs(yspeed) if yspeed != 0 else 0
# If the projectile is within range, it hit the target
if abs(self.rect.centerx - self.target.rect.centerx) <= 20:
if abs(self.rect.centery - self.target.rect.centery) <= 20:
self.do_damage(monsters)
self.kill()
else:
self.rect.move_ip((self.x_speed, self.y_speed))
else:
self.rect.move_ip((self.x_speed, self.y_speed))
# Destroys the projectile if it goes off screen
if not screen.contains(self.rect):
self.kill()
def do_damage(self, monsters):
for monster in monsters:
# Does damage to the target, and adds kills and money rewards if it dies, also adds damage_done to tower
if monster == self.target:
# dmg_result returns (None/Value of monster, Damage done by projectile)
dmg_result = monster.damage(self.damage)
if dmg_result[0] is not None:
self.tower.kills += 1
self.tower.turn_yield += dmg_result[0]
self.tower.damage_done += dmg_result[1]
break
开发者ID:brandonsturgeon,项目名称:Tower_Defense,代码行数:60,代码来源:projectile.py
示例7: init
def init (self):
n_pads = pg.joystick.get_count()
if n_pads == 0:
img = 'nopads'
elif n_pads == 1:
img = '1pad'
else:
img = '2pads'
gm_r = Rect((0, 0), self.graphics.size)
frame = gm_r.inflate(*(-2 * w for w in conf.INTRO_FRAME_WIDTH))
viewport = GraphicsManager(self.scheduler, frame.size)
setup_bg(viewport, 1, self.scheduler)
text = Graphic('intro-{0}.png'.format(img))
viewport.add(text)
text.align()
self.graphics.add(
util.tile_graphic(Graphic('solid.png'), gm_r, 1),
viewport
)
viewport.align()
for i in xrange(n_pads):
pg.joystick.Joystick(i).init()
self.evthandler.add((pg.KEYDOWN, pg.JOYBUTTONDOWN,
lambda: conf.GAME.switch_world(Level)))
开发者ID:ikn,项目名称:boom,代码行数:26,代码来源:level.py
示例8: get_aa_round_rect
def get_aa_round_rect(size, radius, color):
surface = Surface(size, flags=SRCALPHA).convert_alpha()
rect = Rect((0, 0), size)
color = Color(*color)
alpha = color.a
color.a = 0
rectangle = Surface(size, SRCALPHA)
#here 5 is an arbitrary multiplier, we will just rescale later
circle = Surface([min(size) * 5, min(size) * 5], SRCALPHA)
draw.ellipse(circle, (0,0,0), circle.get_rect(), 0)
circle = transform.smoothscale(circle, (2*radius, 2*radius))
#now circle is just a small circle of radius
#blit topleft circle:
radius_rect = rectangle.blit(circle, (0, 0))
#now radius_rect = Rect((0, 0), circle.size), rect=Rect((0, 0), size)
#blit bottomright circle:
radius_rect.bottomright = rect.bottomright #radius_rect is growing
rectangle.blit(circle, radius_rect)
#blit topright circle:
radius_rect.topright = rect.topright
rectangle.blit(circle, radius_rect)
#blit bottomleft circle:
radius_rect.bottomleft = rect.bottomleft
rectangle.blit(circle, radius_rect)
#black-fill of the internal rect
rectangle.fill((0, 0, 0), rect.inflate(-radius_rect.w, 0))
rectangle.fill((0, 0, 0), rect.inflate(0, -radius_rect.h))
#fill with color using blend_rgba_max
rectangle.fill(color, special_flags=BLEND_RGBA_MAX)
#fill with alpha-withe using blend_rgba_min in order to make transparent
#the
rectangle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN)
surface.blit(rectangle, rect.topleft)
return surface
开发者ID:YannThorimbert,项目名称:Thorpy-1.5.2a,代码行数:34,代码来源:graphics.py
示例9: CircleUnionObject
class CircleUnionObject(PhysicsObject):
def __init__(self,particle,color):
'''
Constructor
'''
PhysicsObject.__init__(self,particle)
self.rect = Rect(0,0,0,0)
for c in particle.collider.colliders:
radius = int(c.r)
rect = pygame.Rect(c.p.x-radius,c.p.y-radius,radius*2+1, radius*2+1)
self.rect.union_ip(rect)
self.original = pygame.surface.Surface([self.rect.width,self.rect.height],SRCALPHA)
for c in particle.collider.colliders:
radius = int(c.r)
x = int(c.p.x)
y = int(c.p.y)
pygame.gfxdraw.aacircle(self.original, x-self.rect.left,y-self.rect.top,radius,color)
pygame.gfxdraw.filled_circle(self.original,x-self.rect.left,y-self.rect.top,radius,color)
self.view = self.original
self.update()
开发者ID:jeamesbone,项目名称:N-Arcs,代码行数:25,代码来源:object.py
示例10: __init__
def __init__(self, pygameWindow,gameVariable,images):
# bool hodnoty
self.notOver = True
self.startGame = False
self.closeGame = False
self.loadGame = False
self.gameVariable = gameVariable
# okno
self.window = pygameWindow
self.images = images
#velkosti
self.rectWidth = pygameWindow.get_width() / 3
self.rectHeight = pygameWindow.get_height() / 10
self.rectX = (pygameWindow.get_width() / 2) - (self.rectWidth / 2)
# Rect(top,left, width, height)
self.nazovHryRect = Rect(0,0,self.window.get_width(),pygameWindow.get_height()/3)
self.newGameRect = Rect(self.rectX, 150, self.rectWidth, self.rectHeight)
self.loadGameRect = Rect(self.rectX, 250, self.rectWidth, self.rectHeight)
self.exitRect = Rect(self.rectX, 350, self.rectWidth, self.rectHeight)
# font a texty
font.init()
self.font = font.SysFont("norasi",int(self.rectHeight/2))
#iniclializacia lablov
self._initLabel()
开发者ID:salian2,项目名称:Semestralka-UNIX,代码行数:34,代码来源:Menu.py
示例11: testUnionAll
def testUnionAll( self ):
r1 = Rect( 0, 0, 1, 1 )
r2 = Rect( -2, -2, 1, 1 )
r3 = Rect( 2, 2, 1, 1 )
r4 = r1.unionall( [r2,r3] )
self.assertEqual( Rect(-2, -2, 5, 5), r4 )
开发者ID:BSierakowski,项目名称:personal_code,代码行数:7,代码来源:rect_test.py
示例12: draw
def draw(self, screen, work_rects=None):
"""Draw the map to the screen"""
if work_rects is None:
work_rects = [Rect(0,0,screen.get_width(),screen.get_height())]
extra_things = collections.defaultdict(list)
for (k,v) in self.map_to_draw.objects.items():
extra_things[k].extend(v)
extra_things[self.map_coords(pygame.mouse.get_pos(), screen)].append(self.mouse_cursor)
for i in range(self.map_to_draw.w):
for j in range(self.map_to_draw.h):
view_x = 32*i-32*j
view_y = 16*i+16*j
x, y = view_x - self.view_x + screen.get_width()//2, view_y-self.view_y + screen.get_height()//2
t = self.map_to_draw.terrain((i,j))
s = t.sprite
s_pos = Rect(x-32,y+16-s.get_height(),s.get_width(),s.get_height())
if s_pos.collidelist(work_rects)>=0:
screen.blit(s, s_pos)
if (i,j) in extra_things:
for t in extra_things[(i,j)]:
if isinstance(t, pygame.Surface):
s = t
else:
s = t.sprite
screen.blit(s,(x-32,y+16-s.get_height()))
开发者ID:jjvv,项目名称:gearhead-prime,代码行数:25,代码来源:sdlmap.py
示例13: _draw_animation
def _draw_animation(self, entity):
height = entity.cliche.surface.get_height()
dest = Rect(0, 0, height, height)
dest.center = self._on_screen(entity.position)
self.screen.blit(entity.cliche.surface, dest,
entity.cliche.active_rect)
开发者ID:mrhania,项目名称:studies,代码行数:7,代码来源:systems.py
示例14: update_world_window
def update_world_window(self, screen, world_s, camera, floor_s, map_data):
blit_area = Rect(0, 0, 64, 64)
# Get the dirty tile -coordinates from map_data
for dirty_tile_position in map_data.get_dirty_tile_positions:
# Calculate the screen position for given tile
dirty_world_position = world_pos_of_tile(dirty_tile_position)
area_x, area_y = dirty_tile_position
blit_area.topleft = (area_x * 64, area_y * 64)
# Blit the tile from floor, to screen.
world_s.blit(floor_s, dirty_world_position, blit_area)
# Blit items in the tile
# Try if this position contains a list of items. Note: these may fail :s
items_in_position = map_data.get_items_on_position(dirty_tile_position)
if items_in_position is not None:
self.draw_items_on_tile(world_s, items_in_position, dirty_world_position)
# Blit character in here.
character = map_data.get_character_on_position(dirty_tile_position)
if character is not None:
world_s.blit(character.surface, dirty_world_position)
camera_rectangle = camera.get_viewport_rect
assert isinstance(camera_rectangle, Rect)
screen.blit(world_s, (16,16), area=world_rect_of_tile_rect(camera_rectangle))
map_data.reset_dirty_tiles()
开发者ID:rytai,项目名称:ReStart,代码行数:29,代码来源:dirty_drawing.py
示例15: create_platforms_rect
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)
开发者ID:glhrmfrts,项目名称:rgb,代码行数:30,代码来源:physics.py
示例16: draw_sprite
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)
开发者ID:HampshireCS,项目名称:CS112-Spring2012,代码行数:28,代码来源:shadows.py
示例17: __init__
def __init__(self,midbottom):
Rect.__init__(self,self.rect)
self.midbottom = midbottom
self.dx = random()-0.5
self.X = self.x
self.dy = 4+random()
self.Y = self.y
开发者ID:amoliu,项目名称:curriculum-deep-RL,代码行数:7,代码来源:enemy.py
示例18: draw_round_border
def draw_round_border(surface, width=4, color=None, bounds=None):
"""draw_round_border(Surface, line_width, Color, Rect) => None
only Surface is required"""
h = surface.get_height()
w = surface.get_width()
img = dialog.circle_border_image
if color == None: color = pygame.color.Color("white")
if bounds == None: bounds = surface.get_rect()
r = Rect(0,0,8,8)
border = bounds.inflate(-8,-8)
border.topleft = bounds.topleft
surface.blit(img, border.topleft, r)
r.top = 8
surface.blit(img, border.bottomleft, r)
r.left = 8
surface.blit(img, border.bottomright, r)
r.top = 0
surface.blit(img, border.topright, r)
i = bounds.inflate(-17,-17)
o = bounds.inflate(-3,-3)
o.topleft = bounds.topleft
draw.line(surface, color, (i.left,o.top),(i.right,o.top), 4 )
draw.line(surface, color, (o.left,i.top),(o.left,i.bottom), 4 )
draw.line(surface, color, (i.left,o.bottom),(i.right,o.bottom), 4 )
draw.line(surface, color, (o.right,i.top),(o.right,i.bottom), 4 )
开发者ID:bpa,项目名称:renegade,代码行数:28,代码来源:dialog.py
示例19: _prepare_sprite
def _prepare_sprite(self, sprite):
"""Prepare a sprite before drawing.
Return the corresponding rectangle.
"""
# Aliases
old_rect_dct = self.spritedict
use_update = self._use_update
# Test dirty rects
try: sprite.dirty_rects
except AttributeError: sprite.dirty_rects = None
# Update dirtyness
if not sprite.dirty and sprite.dirty_rects:
sprite.dirty = 1
# Get actual size
try: size_rect = Rect((0,0), sprite.source_rect.size)
except AttributeError: size_rect = Rect((0,0), sprite.rect.size)
# Compare rect
new_rect = size_rect.move(sprite.rect.topleft)
has_moved = new_rect != old_rect_dct[sprite]
# Whole rect is dirty
if not use_update or has_moved or sprite.dirty_rects is None:
sprite.dirty_rects = [size_rect]
return new_rect
# Clip the dirty rects
for k, area in enumerate(sprite.dirty_rects):
sprite.dirty_rects[k] = area.clip(size_rect)
# Return
return new_rect
开发者ID:YoannQDQ,项目名称:pyweek-dojo,代码行数:29,代码来源:view.py
示例20: grow_rect
def grow_rect(self, rect, growth, rects, direction):
"""Tries to grow a rectangle in the specified direction
Returns whether the growth succeeded"""
changed = False
if growth[direction]:
left, top, width, height = rect.x, rect.y, rect.w, rect.h
if direction == LEFT:
left -= 1
width += 1
elif direction == RIGHT:
width += 1
elif direction == DOWN:
height += 1
elif direction == UP:
top -= 1
height += 1
new = Rect(left, top, width, height)
if not (set(util.points_in(new)) - self.space) and len(new.collidelistall(rects)) == 1:
rect.left = left
rect.width = width
rect.top = top
rect.height = height
changed = True
#if rect.width >= 8 and rect.height >= 8 and random.randrange(5) == 0:
# growth[direction] = False
else:
growth[direction] = False
return changed
开发者ID:Ragzouken,项目名称:agutaywusyg,代码行数:30,代码来源:castle.py
注:本文中的pygame.Rect类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论