【问题标题】:How can I use different images for my snake's body parts in my snake game? (Python, Pygame, Snake)如何在我的蛇游戏中为我的蛇的身体部位使用不同的图像? (Python、Pygame、Snake)
【发布时间】:2020-07-04 12:10:45
【问题描述】:

说明

我目前正在使用 Pygame 进行蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由正方形组成,但如果蛇包含蛇头、身体、尾巴的 25x25 绘制图片会更好对于弯曲的身体部位,当蛇改变高度和方向时,这部分看起来仍然与蛇相连。

我还添加了一个示例图像,以便您更好地理解我所说的不同身体部位的意思。


这是我的代码的相关部分,因此您可以看到不断增长的蛇体当前是如何工作的。

block_size = 25
black = (0, 0, 0)

# This function contains a list with the current coordinates of the snake head (coordinates) 
# and then draws rectangles of size 25x25 (block_size).

def body_segments(block_size, coordinates):
    for XnY in coordinates:
        pygame.draw.rect(screen, black, [XnY[0], XnY[1], block_size, block_size])


coordinates = []
snake_lenght = 0

# Game Loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Background
    screen.blit(BackgroundImg,(0, 0))

    # Check for a collision with the food
    if distance_SF() < 20:
        FoodX = random.randrange(50, 500, 25)
        FoodY = random.randrange(50, 500, 50)

        # Increase the Snake lenght
        snake_lenght += 1

    # I hereby create a list (HeadCorList) with the coordinates of the snake's head as elements
    # and then I attach these elements to the "coordinates" list.

    HeadCorList = []
    HeadCorList.append(headX) # headX contains the X coordinates of the snake's head
    HeadCorList.append(headY) # headY contains the Y coordinates of the snake's head
    coordinates.append(HeadCorList)

    # This makes sure that the growing body does not get too long.
    if len(segments) > snake_lenght:
        del segments[0]
        
    body_segments(block_size, coordinates)

问题总结

我不知道如何解决这些问题,因为我不知道如何将图片附加到移动的蛇的头部而不是矩形,因为我不知道如何将尾巴附加到蛇的身体末端并且因为我不知道如何实现弯曲的身体部位功能,因为当蛇改变它的高度和方向时,只有一个弯曲的身体部位要插入。

我希望我能解释清楚,因为英语不是我的主要语言,Python 3 是我的第一编程语言,而这个游戏只是我的第二个程序。

【问题讨论】:

    标签: python python-3.x pygame game-engine game-development


    【解决方案1】:

    首先,让我们将您的图像分成 4 部分,并使它们都具有相同的大小。这将使我们的任务更容易:

    head.png

    body.png

    L.png

    tail.png

    让我们使用一个基本的 pygame 游戏来加载它们:

    import pygame
    
    TILESIZE = 24
    def main():
        pygame.init()
        screen = pygame.display.set_mode((600, 480))
    
        load = lambda part: pygame.image.load(part + '.png').convert_alpha()
        parts = ('head', 'body', 'tail', 'L')
        head_img, body_img, tail_img, L_img = [load(p) for p in parts]
    
        clock = pygame.time.Clock()
        dt = 0
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == pygame.QUIT:
                    return
    
            screen.fill((30, 30, 30))
    
            screen.blit(head_img, (100, 100))
            screen.blit(body_img, (100, 100 + TILESIZE))
            screen.blit(L_img,    (100, 100 + TILESIZE*2))
            screen.blit(tail_img, (100, 100 + TILESIZE*3))
    
    
            dt = clock.tick(60)
            pygame.display.flip()
    
    main()
    

    但我们实际上还需要这些旋转形式的图像,所以让我们在开始时创建它们:

    def build_images():
        load = lambda part: pygame.image.load(part + '.png').convert_alpha()
        parts = ('head', 'body', 'tail', 'L')
        head_img, body_img, tail_img, L_img = [load(p) for p in parts]
    
        return {
            'HEAD_N': head_img,
            'HEAD_S': pygame.transform.rotate(head_img, 180),
            'HEAD_E': pygame.transform.rotate(head_img, 90),
            'HEAD_W': pygame.transform.rotate(head_img, -90),
            'BODY_NN': body_img,
            'BODY_SS': body_img,
            'BODY_WW': pygame.transform.rotate(body_img, 90),
            'BODY_EE': pygame.transform.rotate(body_img, 90),
            'BODY_NE': pygame.transform.rotate(L_img, 180),
            'BODY_WS': pygame.transform.rotate(L_img, 180),
            'BODY_WN': pygame.transform.rotate(L_img, 90),
            'BODY_SE': pygame.transform.rotate(L_img, 90),
            'BODY_ES': pygame.transform.rotate(L_img, -90),
            'BODY_NW': pygame.transform.rotate(L_img, -90),
            'BODY_EN': pygame.transform.rotate(L_img, 0),
            'BODY_SW': pygame.transform.rotate(L_img, 0),
            'TAIL_N': tail_img,
            'TAIL_S': pygame.transform.rotate(tail_img, 180),
            'TAIL_E': pygame.transform.rotate(tail_img, 90),
            'TAIL_W': pygame.transform.rotate(tail_img, -90)
        }
    

    使用带有字符串键的字典可以让我们根据蛇的每个部分及其父部分的方向轻松获得正确的图像。

    例如BODY_SE是当蛇的部分朝东时我们使用的图像,但父母要向南移动。

    现在我们可以开始实现我们的游戏了。由于我们使用的是 pygame,我将使用基本的 pygame 功能,例如 SpriteGroup。让我们看看如何创建一些代表蛇的 Sprite:

    import pygame
    TILESIZE = 24
    
    class Snake(pygame.sprite.Sprite):
        images = None
        def __init__(self, grp, pos, length, parent=None):
            super().__init__(grp)
            self.parent = parent
            self.child = None
            if not self.parent:
                self.image = Snake.images['HEAD_N']
            elif length == 1:
                self.image = Snake.images['TAIL_N']
            else:
                self.image = Snake.images['BODY_NN']
            self.pos = pos
            self.rect = self.image.get_rect(x=pos[0]*TILESIZE, y=pos[1]*TILESIZE)
            if length > 1:
                self.child = Snake(grp, (pos[0], pos[1]+1), length-1, self)
    
    def build_images():
       ...
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((600, 480))
        Snake.images = build_images()
    
        all_sprites = pygame.sprite.Group()
        snake = Snake(all_sprites, (4, 4), 6)
        clock = pygame.time.Clock()
        dt = 0
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == pygame.QUIT:
                    return
    
            screen.fill((30, 30, 30))
    
            all_sprites.update()
            all_sprites.draw(screen)
    
            dt = clock.tick(60)
            pygame.display.flip()
    
    main()
    

    如您所见,蛇的每个部分都有对前面部分的引用(头部除外),对后面部分的引用(除了尾巴)。

    到目前为止,一切都很好。让我们移动蛇:

    import pygame
    TILESIZE = 24
    
    class Snake(pygame.sprite.Sprite):
        images = None
        def __init__(self, grp, pos, length, parent=None):
            ...
    
        def move(self):
            # if we have a parent, let's look were it moves
            parent_direction = self.parent.direction if self.parent else None
    
            if self.direction == 'N': self.pos = self.pos[0], self.pos[1] - 1
            elif self.direction == 'S': self.pos = self.pos[0], self.pos[1] + 1
            elif self.direction == 'E': self.pos = self.pos[0] - 1, self.pos[1]
            elif self.direction == 'W': self.pos = self.pos[0] + 1, self.pos[1]
    
            self.rect = self.image.get_rect(x=self.pos[0]*TILESIZE, y=self.pos[1]*TILESIZE)
    
            # move the child
            if self.child:
                self.child.move()
    
            # follow the parent
            if parent_direction:
                self.direction = parent_direction
    
        def update(self):
            # no parent means we're the head of the snake
            # and we should move we a key is pressed
            if not self.parent:
                pressed = pygame.key.get_pressed()
                if pressed[pygame.K_w]: self.direction = 'N'
                if pressed[pygame.K_s]: self.direction = 'S'
                if pressed[pygame.K_a]: self.direction = 'E'
                if pressed[pygame.K_d]: self.direction = 'W'
    
    def main():
        ...
        # let's trigger the MOVE event every 500ms
        MOVE = pygame.USEREVENT + 1
        pygame.time.set_timer(MOVE, 500)
        ...
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == pygame.QUIT:
                    return
                if e.type == MOVE:
                    snake.move()
    

    太棒了。如果方向改变,剩下的就是实际改变每个身体部位的图像。

    这是完整的代码:

    import pygame
    TILESIZE = 24
    
    class Snake(pygame.sprite.Sprite):
        images = None
        def __init__(self, grp, pos, length, parent=None):
            super().__init__(grp)
            self.parent = parent
            self.child = None
            self.direction = 'N'
    
            if not self.parent: self.image = Snake.images['HEAD_N']
            elif length == 1: self.image = Snake.images['TAIL_N']
            else: self.image = Snake.images['BODY_NN']
    
            self.pos = pos
            self.rect = self.image.get_rect(x=self.pos[0]*TILESIZE, y=self.pos[1]*TILESIZE)
            if length > 1:
                self.child = Snake(grp, (pos[0], pos[1]+1), length-1, self)
    
        def move(self):
            # if we have a parent, let's look were it moves
            parent_direction = self.parent.direction if self.parent else None
    
            if self.direction == 'N': self.pos = self.pos[0], self.pos[1] - 1
            elif self.direction == 'S': self.pos = self.pos[0], self.pos[1] + 1
            elif self.direction == 'E': self.pos = self.pos[0] - 1, self.pos[1]
            elif self.direction == 'W': self.pos = self.pos[0] + 1, self.pos[1]
    
            self.rect = self.image.get_rect(x=self.pos[0]*TILESIZE, y=self.pos[1]*TILESIZE)
    
            # move the child
            if self.child:
                self.child.move()
    
            if not self.parent: self.image = Snake.images['HEAD_' + self.direction]
            elif not self.child: self.image = Snake.images['TAIL_' + parent_direction]
            else: self.image = Snake.images['BODY_' + parent_direction + self.direction]
    
            # follow the parent
            if parent_direction:
                self.direction = parent_direction
    
        def update(self):
            # no parent means we're the head of the snake
            # and we should move we a key is pressed
            if not self.parent:
                pressed = pygame.key.get_pressed()
                if pressed[pygame.K_w]: self.direction = 'N'
                if pressed[pygame.K_s]: self.direction = 'S'
                if pressed[pygame.K_a]: self.direction = 'E'
                if pressed[pygame.K_d]: self.direction = 'W'
    
    
    def build_images():
        load = lambda part: pygame.image.load(part + '.png').convert_alpha()
        parts = ('head', 'body', 'tail', 'L')
        head_img, body_img, tail_img, L_img = [load(p) for p in parts]
    
        return {
            'HEAD_N': head_img,
            'HEAD_S': pygame.transform.rotate(head_img, 180),
            'HEAD_E': pygame.transform.rotate(head_img, 90),
            'HEAD_W': pygame.transform.rotate(head_img, -90),
            'BODY_NN': body_img,
            'BODY_SS': body_img,
            'BODY_WW': pygame.transform.rotate(body_img, 90),
            'BODY_EE': pygame.transform.rotate(body_img, 90),
            'BODY_NE': pygame.transform.rotate(L_img, 180),
            'BODY_WS': pygame.transform.rotate(L_img, 180),
            'BODY_WN': pygame.transform.rotate(L_img, 90),
            'BODY_SE': pygame.transform.rotate(L_img, 90),
            'BODY_ES': pygame.transform.rotate(L_img, -90),
            'BODY_NW': pygame.transform.rotate(L_img, -90),
            'BODY_EN': pygame.transform.rotate(L_img, 0),
            'BODY_SW': pygame.transform.rotate(L_img, 0),
            'TAIL_N': tail_img,
            'TAIL_S': pygame.transform.rotate(tail_img, 180),
            'TAIL_E': pygame.transform.rotate(tail_img, 90),
            'TAIL_W': pygame.transform.rotate(tail_img, -90)
        }
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((600, 480))
        Snake.images = build_images()
    
        # let's trigger the MOVE event every 500ms
        MOVE = pygame.USEREVENT + 1
        pygame.time.set_timer(MOVE, 500)
    
        all_sprites = pygame.sprite.Group()
        snake = Snake(all_sprites, (4, 4), 8)
        clock = pygame.time.Clock()
        dt = 0
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == pygame.QUIT:
                    return
                if e.type == MOVE:
                    snake.move()
    
            screen.fill((30, 30, 30))
    
            all_sprites.update()
            all_sprites.draw(screen)
    
            dt = clock.tick(60)
            pygame.display.flip()
    
    main()
    

    【讨论】:

    • 哇,非常感谢您为回答我的问题所做的所有工作!这对我有很大帮助:)
    • 又一个很棒的答案 Sloth!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多