本文整理汇总了Python中pygame.Surface.get_rect方法的典型用法代码示例。如果您正苦于以下问题:Python Surface.get_rect方法的具体用法?Python Surface.get_rect怎么用?Python Surface.get_rect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.Surface
的用法示例。
在下文中一共展示了Surface.get_rect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Player
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Player(Sprite):
def __init__(self, bounds):
Sprite.__init__(self)
self.bounds = bounds
self.image = Surface((80, 80))
self.rect = self.image.get_rect()
self.image.fill((0,0,0))
self.image.fill(PLAYER_COLOR, self.rect.inflate(-4,-4))
self.x, self.y = bounds.center
self.vx = PLAYER_SPEED
self.vy = PLAYER_SPEED
def update(self, dt):
dt = dt / 1000.0
keys = pygame.key.get_pressed()
if keys[K_UP]:
self.y -= self.vy * dt
if keys[K_DOWN]:
self.y += self.vy * dt
if keys[K_LEFT]:
self.x -= self.vx * dt
if keys[K_RIGHT]:
self.x += self.vx * dt
# update player
self.rect = self.image.get_rect()
self.rect.center = int(self.x), int(self.y)
self.rect.clamp_ip(self.bounds)
示例2: Enemy
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Enemy(Sprite):
width = 40
height = 40
y = -40
def __init__(self):
self.x = randrange(0,scrWidth-self.width)
Sprite.__init__(self)
#Self.rect and self.image
self.rect = Rect((self.x,self.y),(self.width,self.height))
self.image = Surface(self.rect.size)
self.image.fill((0,0,0))
self.image.set_colorkey((0,0,0))
#Draw stuff
pygame.draw.ellipse(self.image,red,self.image.get_rect())
pygame.draw.ellipse(self.image,blue2,self.image.get_rect(),15)
#Randomly picking trajectory
self.down = randrange(1,5)
self.side = randrange(-6,6,4)
def update(self):
"Movement behavior"
self.rect.y += self.down
self.rect.x += self.side
if self.rect.right > scrWidth or self.rect.left < 0:
self.side *=-1
if self.rect.top > scrHeight:
self.kill()
示例3: change_size
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
def change_size(self, new_width):
"""
Function used to change the size of the palette. Rebuilds image from source and
generates new collision Rect
:param new_width: new Paddle width in pixels (min. 22px)
:return:
"""
paddleAsset = Assets.paddles[self.paddle_color]
new_width = max(new_width, 22) # 22 = (border(8) + colorbar(3))*2
paddle_mid = Surface((38, 15)) # (2*paddle.rect.width, paddle.rect.height))
# paddle_mid.blit(self.image, (0, 0), Rect(11, 0, 38, 15))
paddle_mid.blit(paddleAsset, (0, 0), Rect(11, 0, 38, 15))
paddle_mid = transform.scale(paddle_mid, (new_width-22, self.rect.height))
new_paddle = Surface((new_width, self.rect.height), SRCALPHA) # blank surface
# new_paddle.fill(pygame.Color(0, 0, 0, 0), new_paddle.get_rect())
new_paddle.blit(paddle_mid, (11, 0))
new_paddle.blit(paddleAsset, (0, 0), Rect(0, 0, 11, 15))
new_paddle.blit(paddleAsset, (new_width - 11, 0),
Rect(paddleAsset.get_rect().width - 11, 0, 11, 15))
paddle_new_x = self.rect.x + self.rect.width/2 - new_paddle.get_rect().width/2
self.rect = Rect(paddle_new_x, self.rect.y, new_paddle.get_rect().width,
new_paddle.get_rect().height)
self.mask = mask.from_surface(new_paddle)
self.image = new_paddle
self.attachment_points[1] = (self.rect.width-8, 0)
# self.paddle.attachment_points[1] =
if self.rect.x <= PLAYFIELD_PADDING[0]:
self.rect.x = PLAYFIELD_PADDING[0] + 1
elif self.rect.x + self.rect.width >= LEVEL_WIDTH - PLAYFIELD_PADDING[0]:
self.rect.x = LEVEL_WIDTH - PLAYFIELD_PADDING[0] - self.rect.width - 1
示例4: Shield
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Shield(Sprite):
color = 255,255,255
transparent = 0,0,0
size = 120,120
kind = "null"
def __init__(self, parent, kind):
self.parent=parent
Sprite.__init__(self)
self.kind = kind
self.hiss_sfx = load_sfx("hiss")
if kind == "baseball":
self.color = 0,90,90
elif kind == "cigar":
self.color = 255,100,0
elif kind == "health":
self.color = 180, 200, 180
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = self.parent.rect.center
self.image.fill(self.transparent)
self.image.set_colorkey(self.transparent)
pygame.draw.ellipse(self.image, (self.color), self.image.get_rect())
pygame.draw.ellipse(self.image, self.transparent, self.image.get_rect().inflate(-10, -10))
#pygame.draw.ellispe(self.image, (30,255,30), self.rect.center, ((self.rect.width)/2))
def update(self):
if not self.parent.alive():
self.kill()
self.rect.center = self.parent.rect.center
示例5: __init__
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
def __init__(self, parentSurface:Surface, rect:pygame.Rect, filmstrip:Surface, borderColor:tuple):
Widget.__init__(self, parentSurface, rect, borderColor, None)
self.images = []
self.numImages = int(filmstrip.get_rect().height / filmstrip.get_rect().width)
self.currentImage = 0
clipRect = pygame.Rect(0, 0, rect.width, rect.height)
for x in range(0, self.numImages):
self.images.append(filmstrip.subsurface(clipRect))
clipRect.move_ip(0, rect.height)
self.draw()
示例6: Enemy2
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Enemy2(Sprite):
y = -50
health = 3
width = height = 50
def __init__(self):
Sprite.__init__(self)
self.x = randrange(0,scrWidth-self.width)
self.rect = Rect((self.x,self.y),(self.width,self.height))
self.image = Surface(self.rect.size)
self.image.fill((0,0,0))
self.image.set_colorkey((0,0,0))
pygame.draw.ellipse(self.image,yellow,self.image.get_rect())
self.down = 2
self.side = randrange(-1,1,2)
self.bombSplode = False
def update(self):
self.rect.x+=self.side
self.rect.y+=self.down
if self.rect.right > scrWidth or self.rect.left < 0:
self.side *=-1
if self.rect.top > scrHeight:
self.kill
if self.health<3:
pygame.draw.ellipse(self.image,bOrange,self.image.get_rect(),15)
if self.health<2:
pygame.draw.ellipse(self.image,(0,0,0),self.image.get_rect(),15)
if self.bombSplode:
count = 3
while count >0:
pygame.draw.ellipse(self.image,(bORange),self.image.get_rect(),15)
self.kill
check = randrange(0,100)
if check<3:
eBulletGroup.add(bulletEnemy(self))
def damage(self):
self.health-=1
if self.health == 0:
check = randrange(0,100)
pygame.draw.ellipse(self.image,bOrange,self.image.get_rect(),15)
self.kill()
if check <=10:
bombGroup.add(Bomb(self))
示例7: Enemy
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Enemy (Sprite):
size = 50, 30
color = 255,0,0
vx, vy = 6,6
spawntime = 30
spawnticker = 0
def __init__(self,loc, bounds):
Sprite.__init__(self)
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.bottomleft = loc
self.image.fill(self.color)
self.bounds = bounds
self.vx = randrange(-6,6)
self.vy = randrange(3,6)
def update(self):
self.rect.x += self.vx
self.rect.y += self.vy
#spawn
#bounce off side
if self.rect.right > self.bounds.right or self.rect.left < self.bounds.left:
self.vx *= -1
#kill if out of bounds
if self.rect.top > self.bounds.bottom:
self.kill()
示例8: Player
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Player(Sprite):
color = 255, 255, 0
size = 20, 20
def __init__(self, loc, bounds):
Sprite.__init__(self)
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.bounds = bounds
self.image.fill(self.color)
self.bullets = Group()
def update(self):
self.rect.center = pygame.mouse.get_pos()
self.rect.clamp_ip(self.bounds)
def shoot(self):
if not self.alive():
return
bullet = Bullet(self.bounds)
bullet.rect.midbottom = self.rect.midtop
self.bullets.add(bullet)
示例9: Impact
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Impact(Meteor):
size = (100,100)
COLOR = 0,140,0
duration = 5
dir = 0
damage = 10
def __init__(self,loc, bounds, duration, kind, dir = 0):
#print "explosion!"
self.dir = dir
# Ice should have moving impacts
Sprite.__init__(self)
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.image.fill(self.COLOR)
self.bounds = bounds
self.duration = duration
self.kind = kind
self.get_sprite()
self.get_sound()
self.impact_sfx.stop()
self.impact_sfx.play()
def get_sprite(self):
#return
#-----------This will attempt to load an image, and fill if it fails.
try:
self.image = load_image('explosion_'+self.kind)
print "impact explosion yay"
except:
self.image.fill(self.COLOR)
print "failed to find explosion"
#Scale the image to the proper size and add random rotation
#if randint(0,2) == 0:
# self.image = pygame.transform.flip(self.image, True, False)
#self.image = pygame.transform.rotate(self.image, randint(-360,360))
self.image = pygame.transform.scale(self.image, self.size) #temp
#Anything that's pure white will be transparent
self.image.set_colorkey((255,255,255))
#-------
def get_sound(self):
self.impact_sfx = load_sfx("stockmeteorhit")
def update(self):
self.duration -= 1
if self.duration <= 0:
self.coord_x, self.coord_y = self.rect.center
self.kill()
#def collision(self, robot):
def kill (self):
Sprite.kill(self)
示例10: get_aa_round_rect
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
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
示例11: Sprite
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Sprite(DirtySprite):
"""The base sprite class that all sprites inherit."""
def __init__(self, scene, size, color):
DirtySprite.__init__(self)
self.scene = scene
self.image = Surface(size).convert_alpha()
self.image.fill(color)
self.rect = self.image.get_rect()
self.x, self.y = 0, 0
self.facing = [0,2]
def move(self):
"""Move the sprite and set it to dirty for the current frame."""
self.dirty = 1
self.rect.move_ip([self.x, self.y])
def update(self):
"""Update the sprite each frame."""
if (self.x or self.y):
self.facing = [self.x, self.y]
self.move()
self.collide()
示例12: Enemy
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Enemy(Sprite):
size = width,height = 50,50
color = 255,0,0
vx, vy = 6,6
def __init__(self,loc,bounds):
Sprite.__init__(self)
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.bounds = bounds
self.image.fill(self.color)
self.rect.bottomleft = loc
self.vx = randrange(4,8)
direction = 2 * randrange(2)- 1
self.vx *= direction
def update(self):
self.rect.x += self.vx
self.rect.y += self.vy
if self.rect.left < self.bounds.left or self.rect.right > self.bounds.right:
self.rect.x -= 2 * self.vx
self.vx = -self.vx
if self.rect.top > self.bounds.bottom:
self.kill()
示例13: render
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
def render(self, text):
""" Renders the text using the options first used. """
lines = text.splitlines()
img = Surface(self._calculate_image_size(self.font,
self.text, self.size, self.line_spacer, self.layers))
if self.bg_transparent:
img.set_colorkey(self.background)
full_rect = img.get_rect()
y = 0
for l in lines:
r = self.font.render(l, self.size, self.background, self.bg_transparent, self.layers)
r_rect = r.get_rect()
if self.bg_transparent:
r.set_colorkey(self.background)
if self.align == self.A_CENTER:
x = self._center_rect_inside_rect(r_rect, full_rect)
elif self.align == self.A_LEFT:
x = 0
elif self.align == self.A_RIGHT:
x = full_rect[3] - r_rect[3]
img.blit(r, (x, y))
y += self.line_spacer + r_rect[3]
return img
示例14: __init__
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
def __init__(self, tag, initial_position, rotation=0, fontname=DEFAULT_FONT, fontzoom=5):
Sprite.__init__(self)
self.tag = copy(tag)
self.rotation = rotation
font_spec = load_font(fontname)
#fonter = font.Font(os.path.join(FONT_DIR, font_spec['ttf']), int(tag['size'] * fontzoom)).render(tag['tag'], True, tag['color'])
# changing to allow for arbitrary local fonts
fonter = font.Font(font_spec['ttf'], int(tag['size'] * fontzoom)).render(tag['tag'], True, tag['color'])
self.tag['size'] *= fontzoom
fonter = transform.rotate(fonter, rotation)
frect = fonter.get_bounding_rect()
frect.x = -frect.x
frect.y = -frect.y
self.fontoffset = (-frect.x, -frect.y)
font_sf = Surface((frect.width, frect.height), pygame.SRCALPHA, 32)
font_sf.blit(fonter, frect)
self.image = font_sf
self.rect = font_sf.get_rect()
self.rect.width += TAG_PADDING
self.rect.height += TAG_PADDING
self.rect.x = initial_position[0]
self.rect.y = initial_position[1]
self.mask = mask.from_surface(self.image)
self.mask = self.mask.convolve(CONVMASK, None, (TAG_PADDING, TAG_PADDING))
示例15: Player
# 需要导入模块: from pygame import Surface [as 别名]
# 或者: from pygame.Surface import get_rect [as 别名]
class Player (Sprite):
size = 20,20
color = 255,255,0
shottimer = 0
def __init__(self, loc, bounds):
Sprite.__init__(self) # required for sprites
self.image = Surface(self.size)
self.rect = self.image.get_rect()
self.rect.center = loc
self.bounds = bounds # passes the bounds to the object so it can be saved.
self.image.fill(self.color)
self.bullets = Group()
def update(self):
if self.shottimer > 0:
self.shottimer -= 1
self.rect.center = pygame.mouse.get_pos()
self.rect.clamp_ip(self.bounds) #makes sure it doesn't go outside the provided rectangle
def shoot(self):
if not self.alive(): #return just stops everything in hte function
return
if self.shottimer == 0:
bullet = Bullet(self.bounds)
self.bullets.add(bullet) #adds the new bullet to the Bullets list
bullet.rect.midbottom = self.rect.midtop
self.shottimer = 10