python - Pygame:当 Sprite 遇到它的踪迹时如何 "lose"游戏

标签 python pygame python-3.4

我创建了一个类似蛇的游戏,其中用户移动 Sprite , Sprite 会留下痕迹。如果用户遇到了他创建的踪迹,我希望游戏结束,玩家失败。

我的一个想法是以某种方式跟踪 Sprite 过去的位置(可能在列表中),然后创建一个“if”语句,这将导致游戏失败(但是,我有点不清楚如何来做到这一点)。

我收到了为此列表编码的这个问题的答案:

"I think you could declare a two dimensional list like this: pastPositions = [[400, 300]] Then every time the player's position moves, check the list:
for row in pastPositions: If (player.rect.x == pastPositions[row][0] and player.rect.y == >pastPositions[row][1]): done = true # game over If the player hasn't been there yet, then add that position to the list. pastPositions.append([player.rect.x, player.rect.y])"

这看起来应该可以工作,但是当我尝试运行代码(在 Python Interactive 中)时,我收到一条错误消息,内容如下:“第 86 行,在 ifplayer.rect.x == astPositions[row][ 0] 和 player.rect.y == pastPositions[row][1]: IndexError: 列表索引超出范围"– Gal 2 天前

您建议我将范围更改为什么以避免这种情况发生?我尝试将其设置为 pygame 窗口的宽度和高度。

import pygame

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

class Player(pygame.sprite.Sprite):

   def __init__(self, x, y):
       super().__init__()

       self.image = pygame.Surface([15, 15])
       self.image.fill(WHITE)

       self.rect = self.image.get_rect()
       self.rect.x = x
       self.rect.y = y

       self.change_x = 0
       self.change_y = 0


   def changespeed(self, x, y):
       self.change_x += x
       self.change_y += y


   def update(self):
       self.rect.x += self.change_x
       self.rect.y += self.change_y

pygame.init()

screen = pygame.display.set_mode([800, 600])

pygame.display.set_caption('The Etch-a-Sketch Game')

myfont = pygame.font.SysFont('Times', 20)
textsurface = myfont.render('This is the Etch-a-Sketch Game', False, (255, 255, 255))
screen.blit(textsurface,(0,0))

myfont = pygame.font.SysFont('Times', 15)
textsurface = myfont.render('Feel free to draw, but if you cross your own    path, you will die.', False, (255, 255, 255))
screen.blit(textsurface,(0,20))


player = Player(400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)

clock = pygame.time.Clock()
done = False

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 3)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -3)

    player.update()

    all_sprites_list.draw(screen)

    pygame.display.flip()
    clock.tick(75)

pygame.quit ()

最佳答案

我认为你可以像这样声明一个二维列表:

过去的位置 = [[400, 300]]

然后每次玩家位置移动时,检查列表:

对于过去位置中的行: if (player.rect.x == row[0] 且player.rect.y == row[1]): 完成 = true # 游戏结束

如果玩家尚未到达那里,则将该位置添加到列表中。

pastPositions.append([player.rect.x,player.rect.y])

您正在寻找类似的东西吗?

关于python - Pygame:当 Sprite 遇到它的踪迹时如何 "lose"游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54115617/

相关文章:

python - 改变标题栏的颜色

Python 3 字节码格式

python - 使用 groupby 对 Pandas DataFrame 进行计算,然后将其传回到 DataFrame 中?

python - 将 Django 与 SSL 结合使用以集成 PayPal

python - 通过 ssh 隧道访问远程数据库 (Python 3)

python - pygame 中的 surface.blit() 函数是什么?它有什么作用?它是如何工作的?

python - 错误: Incompatible function arguments

python - pygame.quit() 可靠吗?

python - PyOpenGL + PyGame 上的奇怪 "X"

windows - python : bad handshake error on get request when executed on windows but not linux