本文整理汇总了Python中spritesheet_functions.SpriteSheet.grab_image方法的典型用法代码示例。如果您正苦于以下问题:Python SpriteSheet.grab_image方法的具体用法?Python SpriteSheet.grab_image怎么用?Python SpriteSheet.grab_image使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spritesheet_functions.SpriteSheet
的用法示例。
在下文中一共展示了SpriteSheet.grab_image方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Enemy1
# 需要导入模块: from spritesheet_functions import SpriteSheet [as 别名]
# 或者: from spritesheet_functions.SpriteSheet import grab_image [as 别名]
class Enemy1(Enemy):
frames = None
def __init__(self, x, y, change_x, level):
self.spritesheet = SpriteSheet("resources/graphics/enemy.bmp", (36, 50, 63))
self.frames = []
image = self.spritesheet.grab_image(0, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
image = self.spritesheet.grab_image(16, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
image = self.spritesheet.grab_image(32, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
self.index = 0
self.MAX_FRAMES = 9
self.image = self.frames[0]
'''
self.image = pygame.Surface([constants.TILE_WIDTH, constants.TILE_HEIGHT])
self.image.fill(constants.RED)
'''
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.level = level
self.game = self.level.game
self.change_x = change_x
self.change_y = 0
def animate(self):
self.index += 1
if self.index >= self.MAX_FRAMES:
self.index = 0
self.image = self.frames[self.index//3]
示例2: __init__
# 需要导入模块: from spritesheet_functions import SpriteSheet [as 别名]
# 或者: from spritesheet_functions.SpriteSheet import grab_image [as 别名]
class Player:
# Create walking frames
frames_u = []
frames_d = []
frames_l = []
frames_r = []
# Speed
change_x = 0
change_y = 0
# Health
health = 100
# Recovery time
recovery = 0
def __init__(self, x, y, game):
self.game = game
self.bullet_list = self.game.bullet_list
try:
self.spritesheet = SpriteSheet("resources/player.png")
except pygame.error:
print "player.png does not exist."
raw_input("<press enter>")
pygame.quit()
sys.exit()
# Loading sprites from sheet.
# Down facing images
image = self.spritesheet.grab_image(16, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_d.append(image)
image = self.spritesheet.grab_image(32, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_d.append(image)
# Up facing images
image = self.spritesheet.grab_image(64, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_u.append(image)
image = self.spritesheet.grab_image(80, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_u.append(image)
# Left facing images
image = self.spritesheet.grab_image(112, 0, 16, 16)
image = pygame.transform.scale2x(image)
image = pygame.transform.flip(image, True, False)
self.frames_l.append(image)
image = self.spritesheet.grab_image(128, 0, 16, 16)
image = pygame.transform.scale2x(image)
image = pygame.transform.flip(image, True, False)
self.frames_l.append(image)
# Right facing images
image = self.spritesheet.grab_image(112, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_r.append(image)
image = self.spritesheet.grab_image(128, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames_r.append(image)
self.index = 0
self.image = self.frames_d[self.index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.facing = "d"
def update(self):
if self.recovery > 0:
self.recovery -= 1
# Animate the image
if (self.change_x != 0) or (self.change_y != 0):
self.index += 1
if self.index >= 12:
self.index = 0
if self.facing == "u":
self.image = self.frames_u[self.index//6]
elif self.facing == "d":
self.image = self.frames_d[self.index//6]
elif self.facing == "l":
self.image = self.frames_l[self.index//6]
elif self.facing == "r":
self.image = self.frames_r[self.index//6]
# Move the object horizontally
self.rect.x += self.change_x
for wall in self.game.wall_list:
if self.rect.colliderect(wall.rect):
if self.change_x > 0:
self.rect.right = wall.rect.left
else:
self.rect.left = wall.rect.right
for healthpack in self.game.healthpack_list:
if self.rect.colliderect(healthpack.rect):
if self.health < 100:
self.health += 10
self.game.healthpack_list.remove(healthpack)
if self.rect.colliderect(self.game.finish.rect):
self.game.update_screen(pygame.display.get_surface())
self.game.levelno += 1
try:
self.game.set_level(self.game.level_list[self.game.levelno - 1])
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from spritesheet_functions import SpriteSheet [as 别名]
# 或者: from spritesheet_functions.SpriteSheet import grab_image [as 别名]
class Enemy:
frames = []
def __init__(self, x, y, game):
try:
self.spritesheet = SpriteSheet("resources/enemy.png")
except pygame.error:
print "enemy.png does not exist."
raw_input("<press enter>")
pygame.quit()
sys.exit()
# Loading sprites from the sheet.
image = self.spritesheet.grab_image(0, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
image = self.spritesheet.grab_image(16, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
image = self.spritesheet.grab_image(32, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
image = self.spritesheet.grab_image(48, 0, 16, 16)
image = pygame.transform.scale2x(image)
self.frames.append(image)
self.index = 0
self.image = self.frames[self.index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_direction()
'''
direction = random.choice([[0, -5], [0, 5], [-5, 0], [5, 0]])
self.change_x = direction[0]
self.change_y = direction[0]
'''
self.game = game
self.dead = False
self.steps = 0
self.threshold = 20
self.cool_down = 0
def update(self):
if not self.dead:
if self.cool_down <= 0:
self.steps += 1
if self.steps >= self.threshold:
self.steps = 0
#self.threshold = 20
self.threshold = random.randint(10, 35)
self.cool_down = 20
self.change_direction()
self.rect.x += self.change_x
# Check horizontal collision
for wall in self.game.wall_list:
if self.rect.colliderect(wall.rect):
if self.change_x > 0:
self.rect.right = wall.rect.left
else:
self.rect.left = wall.rect.right
self.change_direction()
if self.rect.colliderect(self.game.finish.rect):
if self.change_x > 0:
self.rect.right = self.game.finish.rect.left
else:
self.rect.left = self.game.finish.rect.right
self.change_direction()
#self.change_x *= -1
#self.change_y = random.choice([5, -5])
if self.rect.left < 0:
self.rect.left = 0
self.steps = 0
self.threshold = 20
self.change_x *= -1
elif self.rect.right > constants.SCREEN_WIDTH:
self.rect.right = constants.SCREEN_WIDTH
self.steps = 0
self.threshold = 20
self.change_x *= -1
self.rect.y += self.change_y
# Check vertical collision
for wall in self.game.wall_list:
if self.rect.colliderect(wall.rect):
if self.change_y > 0:
self.rect.bottom = wall.rect.top
else:
self.rect.top = wall.rect.bottom
self.change_direction()
if self.rect.colliderect(self.game.finish.rect):
if self.change_y > 0:
self.rect.bottom = self.game.finish.rect.top
#.........这里部分代码省略.........