如何在贪吃蛇游戏中为蛇的身体部位使用不同的图像?(Python、Pygame、蛇)

Poi*_*ter 4 python pygame game-development game-engine python-3.x

解释

我目前正在使用 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)
Run Code Online (Sandbox Code Playgroud)

问题总结

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

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

slo*_*oth 5

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

在此输入图像描述头.png

在此输入图像描述正文.png

在此输入图像描述L.png

在此输入图像描述尾巴.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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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)
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

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()
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,蛇的每个部分都有一个对前面部分的引用(除了头部),还有一个对后面部分的引用(除了尾巴)。

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

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

这是完整的代码:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述