Spritesheet帮助pygame

问题描述 投票:1回答:2

我目前正在开发一款游戏,试图提高我的初学蟒蛇技能。我的游戏在最基本的级别上运行 - 我无法让自己继续前进,直到我可以为我的精灵制作动画。它给了我最严重的头痛 - 由几小时盯着其他人的代码引起,复制和粘贴无济于事,并观看无数的YouTube视频。出于某种原因,我似乎无法理解如何从spritesheet中剥离图像的基本概念。我已经看过它的pygame网站,我只是不明白。我跟着孩子们可以编码,但他的spritesheet有一个'xml'?附加文件,以便他可以复制和粘贴坐标。我的spritesheet是192x192像素所以我试图通过48分隔的坐标加载/分割图像。例如 - (0,0,48,0)。我假设这些变量是他的例子中的x,y,previousSpriteEndingx,previousSpriteEndingy,还因为这是我唯一能理解的。我将每个精灵位置分组为相应方向的列表 - 向上,向下,向左和向右。 (我不想翻译和翻转我的L / R,但现在我正在考虑它,这会让我的游戏更快吗?因为它不需要加载4个新图像?)

我终于让它实际运行,但它不是显示我的精灵,它只是一个黑盒子,随着它的移动增加了宽度,但看起来它太大了我的精灵。所以我知道我的坐标有问题(当我按下时框会消失)。如果有人可以给我一个可以复制和粘贴以运行和剖析的工作示例,解释它以便初学者可以轻松理解,或者(我保存最好的最后)在下面粘贴的代码中实现一些东西,我会永远感激。上次我在这里发布了很多帮助,我希望能够复制这些结果。这是我的SpriteSheet https://ibb.co/eZbd2G的链接

import pygame

WIDTH = 1000
HEIGHT = 700
FPS = 60

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        width = 25
        height = 25
        image = pygame.image.load("CharacterSprite.png")
        self.image = pygame.transform.scale(image, (width, height))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2 - 480   #center of rectangle
        self.rect.bottom = HEIGHT - 5  #pixels up from the bottom
        self.speedx = 0
        self.speedy = 0
        self.walkingright = []
        self.walkingleft = []
        self.walkingup = []
        self.walkingdown = []
        self.direction = 'R'


    #Facing Down
    sprite_sheet = SpriteSheet('CharacterSprite.png')
    image = sprite_sheet.get_image(0,0,48,0)
    self.walkingdown.append(image) 
    image = sprite_sheet.get_image(48,0,96,0)
    self.walkingdown.append(image)
    image = sprite_sheet.get_image(96,0,144,0)
    self.walkingdown.append(image)
    image = sprite_sheet.get_image(144,0,192,0)
    self.walkingdown.append(image)


    #Facing Up
    image = sprite_sheet.get_image(192,144,48,144)
    self.walkingup.append(image) 
    image = sprite_sheet.get_image(48,144,96,144)
    self.walkingup.append(image)
    image = sprite_sheet.get_image(96,144,144,144)
    self.walkingup.append(image)
    image = sprite_sheet.get_image(144,144,192,144)
    self.walkingup.append(image)


    #Facing Right
    image = sprite_sheet.get_image(192,96,48,96)
    self.walkingright.append(image)
    image = sprite_sheet.get_image(48,96,96,96)
    self.walkingright.append(image)
    image = sprite_sheet.get_image(96,96,144,96)
    self.walkingright.append(image)
    image = sprite_sheet.get_image(144,96,192,96)
    self.walkingright.append(image)

    #FacingLeft
    image = sprite_sheet.get_image(192,48,48,48)
    self.walkingleft.append(image)
    image = sprite_sheet.get_image(48,48,96,48)
    self.walkingleft.append(image)
    image = sprite_sheet.get_image(96,48,144,48)
    self.walkingleft.append(image)
    image = sprite_sheet.get_image(144,48,192,48)
    self.walkingleft.append(image)


def update(self):
    pos = self.rect.x
    if self.direction == "R":
        frame = (pos // 30) % len(self.walkingright)
        self.image = self.walkingright[frame]
    if self.direction == "L":
        frame = (pos // 30) % len(self.walkingleft)
        self.image = self.walkingleft[frame]
    if self.direction == "U":
        frame = (pos // 30) % len(self.walkingup)
        self.image = self.walkingup[frame]
    if self.direction == "D":
        frame = (pos // 30) % len(self.walkingdown)
        self.image = self.walkingdown[frame]

    self.speedx = 0 #Need these to make sure
    self.speedy = 0 #Sprite stops moving on keyup
    keystate = pygame.key.get_pressed()
    if keystate[pygame.K_LEFT]:
        self.speedx = -5
        self.direction = 'L'
    if keystate[pygame.K_RIGHT]:
        self.speedx = 5
        self.direction = 'R'
    if keystate[pygame.K_UP]:
        self.speedy = -5
        self.direction = 'U'
    if keystate[pygame.K_DOWN]:
        self.speedy = 5
        self.direction = 'D'
    self.rect.x += self.speedx
    self.rect.y += self.speedy

    #Set Walls for Width and Height
    if self.rect.right > WIDTH:
        self.rect.rect = WIDTH
    if self.rect.left < 0:
        self.rect.left = 0
    if self.rect.top < 0:
        self.rect.top = 0
    if self.rect.bottom > HEIGHT:
        self.rect.bottom = HEIGHT


class SpriteSheet(object):
    def __init__(self, file_name):
        self.sprite_sheet = pygame.image.load(file_name)
    def get_image(self, x, y, width, height):
        image = pygame.Surface([width, height])
        image.blit(self.sprite_sheet, (0,0), (x, y, width, height))
        image.set_colorkey(BLUE)
        return image

def update(self):
    if self.rect.right > WIDTH:
        self.rect.right = WIDTH
    if self.rect.top < 0:
        self.rect.top = 0
    if self.rect.bottom > HEIGHT:
        self.rect.bottom = HEIGHT
    self.rect.x += self.speedx
    #kill if it goes off screen
    if self.rect.left > WIDTH:
        self.kill()  

#Loading Graphics
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
projectiles = pygame.sprite.Group()

#Spawn x amount of mobs, add to all sprites and mobs
running = True
while running:
    clock.tick(FPS)
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            player.shoot()

 #Update Game Loop           
all_sprites.update()
screen.fill(WHITE)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
python-3.x pygame sprite sprite-sheet
2个回答
1
投票

这是您的程序的工作版本(阅读评论)。主要问题是你没有传递正确的宽度和高度(48)作为sprite_sheet.get_image的第三和第四个参数。

要加载透明图像,您可以调用convert_alpha()

self.sprite_sheet = pygame.image.load(file_name).convert_alpha()

updatePlayer方法中,你还需要垂直运动的y坐标。

if self.direction == "U":
    frame = (pos_y // 30) % len(self.walkingup)

import pygame

WIDTH = 1000
HEIGHT = 700
FPS = 60

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()


class Player(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        width = 25
        height = 25
        sheet = pygame.image.load('Character_Sprite.png').convert_alpha()
        self.image = pygame.transform.scale(sheet, (48, 48))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2 - 480   #center of rectangle
        self.rect.bottom = HEIGHT - 5  #pixels up from the bottom
        self.speedx = 0
        self.speedy = 0
        self.walkingright = []
        self.walkingleft = []
        self.walkingup = []
        self.walkingdown = []
        self.direction = 'R'

        sprite_sheet = SpriteSheet('Character_Sprite.png')
        #Facing Down
        # Start at x = 0. Pass 48 as the third and
        # fourth argument (width and height).
        image = sprite_sheet.get_image(0,0,48,48)
        self.walkingdown.append(image) 
        image = sprite_sheet.get_image(48,0,48,48)
        self.walkingdown.append(image)
        image = sprite_sheet.get_image(96,0,48,48)
        self.walkingdown.append(image)
        image = sprite_sheet.get_image(144,0,48,48)
        self.walkingdown.append(image)

        #Facing Up
        image = sprite_sheet.get_image(0,144,48,48)
        self.walkingup.append(image) 
        image = sprite_sheet.get_image(48,144,48,48)
        self.walkingup.append(image)
        image = sprite_sheet.get_image(96,144,48,48)
        self.walkingup.append(image)
        image = sprite_sheet.get_image(144,144,48,48)
        self.walkingup.append(image)

        #Facing Right
        image = sprite_sheet.get_image(0,96,48,48)
        self.walkingright.append(image)
        image = sprite_sheet.get_image(48,96,48,48)
        self.walkingright.append(image)
        image = sprite_sheet.get_image(96,96,48,48)
        self.walkingright.append(image)
        image = sprite_sheet.get_image(144,96,48,48)
        self.walkingright.append(image)

        #Facing Left
        image = sprite_sheet.get_image(0,48,48,48)
        self.walkingleft.append(image)
        image = sprite_sheet.get_image(48,48,48,48)
        self.walkingleft.append(image)
        image = sprite_sheet.get_image(96,48,48,48)
        self.walkingleft.append(image)
        image = sprite_sheet.get_image(144,48,48,48)
        self.walkingleft.append(image)

    def update(self):
        pos_x = self.rect.x
        # You also need the y position for the vertical movement.
        pos_y = self.rect.y
        if self.direction == "R":
            frame = (pos_x // 30) % len(self.walkingright)
            self.image = self.walkingright[frame]
        if self.direction == "L":
            frame = (pos_x // 30) % len(self.walkingleft)
            self.image = self.walkingleft[frame]
        if self.direction == "U":
            frame = (pos_y // 30) % len(self.walkingup)
            self.image = self.walkingup[frame]
        if self.direction == "D":
            frame = (pos_y // 30) % len(self.walkingdown)
            self.image = self.walkingdown[frame]

        self.speedx = 0 #Need these to make sure
        self.speedy = 0 #Sprite stops moving on keyup
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -5
            self.direction = 'L'
        if keystate[pygame.K_RIGHT]:
            self.speedx = 5
            self.direction = 'R'
        if keystate[pygame.K_UP]:
            self.speedy = -5
            self.direction = 'U'
        if keystate[pygame.K_DOWN]:
            self.speedy = 5
            self.direction = 'D'
        self.rect.x += self.speedx
        self.rect.y += self.speedy

        #Set Walls for Width and Height
        if self.rect.right > WIDTH:
            self.rect.rect = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT


class SpriteSheet(object):
    def __init__(self, file_name):
        # You have to call `convert_alpha`, so that the background of
        # the surface is transparent.
        self.sprite_sheet = pygame.image.load(file_name).convert_alpha()

    def get_image(self, x, y, width, height):
        # Use a transparent surface as the base image (pass pygame.SRCALPHA).
        image = pygame.Surface([width, height], pygame.SRCALPHA)
        image.blit(self.sprite_sheet, (0,0), (x, y, width, height))
        return image


all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()
    screen.fill(WHITE)
    all_sprites.draw(screen)
    pygame.display.flip()

pygame.quit()

您还可以使用一些for循环缩短精灵表剪切代码:

# Facing Up
for x in range(0, 144+1, 48):  # 144+1 because the `stop` is exclusive.
    self.walkingdown.append(sprite_sheet.get_image(x, 0, 48, 48))
# Facing Up
for x in range(0, 144+1, 48):
    self.walkingup.append(sprite_sheet.get_image(x, 144, 48, 48))
# Facing Right
for x in range(0, 144+1, 48):
    self.walkingright.append(sprite_sheet.get_image(x, 96, 48, 48))
# Facing Left
for x in range(0, 144+1, 48):
    self.walkingleft.append(sprite_sheet.get_image(x, 48, 48, 48))

这是一种更短的切割方式。您可以迭代图像列表并枚举它们以获得y索引并为x坐标添加嵌套的for循环,然后使用pygame.Surface.subsurface来删除子表面。不再需要SpriteSheet课程了。

image_lists = (self.walkingdown, self.walkingleft, self.walkingright, self.walkingup)
for y, img_list in enumerate(image_lists):  # Enumerate to get the y-position.
    for x in range(4):
        # Multiply x and y by 48 to get the correct coords and use the
        # `subsurface` to cut the sheet into separate images.
        img_list.append(sheet.subsurface(x*48, y*48, 48, 48))

1
投票

@skrx已经描述了我只添加的所有问题,你可以使用pygame.Surface.subsurface来创建框架。

class SpriteSheet(object):

    def __init__(self, file_name):
        self.sprite_sheet = pygame.image.load(file_name).convert_alpha()

    def get_image(self, x, y, width, height):
        return self.sprite_sheet.subsurface((x, y, width, height))
© www.soinside.com 2019 - 2024. All rights reserved.