gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:44:08 24 4
gpt4 key购买 nike

说明

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

我还添加了一个示例图像,以便您可以更好地理解我所说的不同 body 部位的含义。

enter image description here

<小时/>

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

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)
<小时/>

问题摘要

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

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

最佳答案

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

enter image description here头.png

enter image description here正文.png

enter image description here L.png

enter image description here尾部.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()

enter image description here

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

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()

enter image description here

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

完整代码如下:

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()

enter image description here

关于python - 如何在贪吃蛇游戏中为蛇的 body 部位使用不同的图像? (Python、Pygame、蛇),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60823576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com