回复:当pygame精灵碰到尾巴时,如何检测并结束游戏(使用screen.get_at)?

时间:2019-01-15 18:02:14

标签: python pygame sprite

我创建了一个类似蛇的游戏,用户在其中移动精灵,精灵留下轨迹。如果用户遇到了他创建的足迹,我希望游戏结束,而玩家则输了。

随着精灵移动,它将在黑色背景上产生白色痕迹。如果我可以检测到用户控制的精灵会击中白色区域之一(其“尾巴”),则可以创建一个if语句,导致游戏失败。

我使用[Surface.get_at()] [1]函数获取精灵将移动到的空间的颜色,并检查该颜色是黑色还是白色。如果是白色,则表示它是精灵的尾巴,游戏以失败告终。

代码如下:

player.update()中:

if (screen.get_at((player_next_x, player_next_y)) != BACKGROUND_COLOUR):
    # player hit tail

但是,这不起作用,我知道原因:

1)考虑到用户在方向键上的输入,我不知道如何定义(player_next_x)(player_next_y)

2)我仅使用已经定义的(player.rect.x)(player.rect.y)进行了尝试,并且系统立即关闭,因为它检测到其当前位置(为白色)“碰撞” )。

以下是完整的代码,如果有帮助的话:

import pygame
import os
import time

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

class Player(pygame.sprite.Sprite):

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

    if (screen.get_at((player.rect.x, player.rect.y)) != WHITE):
        myfont = pygame.font.SysFont('Times', 20)
        textsurface = myfont.render('You have lost the Etch-a-Sketch Game.', False, (255, 255, 255))
        screen.blit(textsurface,(0,0))

    all_sprites_list.draw(screen)

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

pygame.quit ()

1 个答案:

答案 0 :(得分:0)

直接的方法是计算步长,并检查是否只是踩到空白处。如果这与您的计时引擎的设计不符,请照看下一个位置,就像执行以下步骤一样:

def peek_next(self):
    return (self.rect.x + self.change_x,
            self.rect.y + self.change_y)

...
player_next_x, player_next_y = player.peek_next()
if (screen.get_at((player_next_x, player_next_y)) != BACKGROUND_COLOUR):
# player hit tail

或将新职位直接提供给get_at

if screen.get_at(player.peek_next()) != BACKGROUND_COLOUR:
# player bit own tail; dies of poisoning