- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 Pygame 开发蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由方 block 组成,但如果蛇包含蛇头、 body 、尾部的绘制 25x25 图片,我会发现它会更好对于弯曲的 body 部分,这样当蛇改变其高度和方向时,该部分看起来仍然与蛇相连。
我还添加了一个示例图像,以便您可以更好地理解我所说的不同 body 部位的含义。
<小时/>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 部分,并使它们的大小相同。这将使我们的任务变得更容易:
让我们使用基本的 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 功能,例如 Sprite和 Group 。让我们看看如何创建一些代表蛇的 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()
太棒了。剩下的就是如果方向改变,实际改变每个 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()
关于python - 如何在贪吃蛇游戏中为蛇的 body 部位使用不同的图像? (Python、Pygame、蛇),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60823576/
我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。 目前,每次我按 W、A、S 或 D 键,蛇都会移动 1 平方/20 像素。问题是我希望这个 Action
如果您熟悉任何梦幻体育选秀,选秀顺序网格如下所示: EXAMPLE 1 (3-teams): Round Team 1 Team 2 Team 3 1 1 (1.1) 2 (
我从这里得到了蛇算法的代码(在MatLab中实现) http://www.mathworks.com/matlabcentral/fileexchange/28109-snakes-active-co
我遵循了这个 link 中的示例.然而,轮廓从初始点开始收缩。是否可以做展开的轮廓?我想要像显示的图像那样的东西。左边的图像是它的样子,右边的图像是我想要的样子——向外扩展而不是收缩。红色圆圈为起点,
我正在编写一款类似于贪吃蛇的游戏。目前,我正在努力编写 move() 和 Growth() 方法。这个游戏的运作方式是,蠕虫从 1 block 开始,每 move 一步就增加 1 block ,直到达
说明 我目前正在使用 Pygame 开发蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由方 block 组成,但如果蛇包含蛇头、 body 、尾部的绘制 25x25 图片,我会发现它会更好对于弯曲的 b
我是一名优秀的程序员,十分优秀!