Pygame-防撞面具

时间:2018-09-11 21:52:23

标签: pygame

根据屏幕上的图像位置,我认为我没有正确更新蒙版位置。我不明白为什么,因为在更新图像的x和y坐标之后,我重新绘制了蒙版以匹配其位置。我相信,即使在绘制蒙版之后,蒙版也不会发生碰撞,因为蒙版不会被更新以匹配图像的坐标。那是这里发生的事吗? 最小代码如下:

import pygame
import random

pygame.init()

black = (0, 0, 0)
height = 600
width = 800
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
FPS = 60
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((40, 50))
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.x = 0 + self.rect.x
        self.rect.y = height - self.rect.height
        self.lead_x_change = 0
        self.lead_y_change = 0
        self.speed = 5
        self.health = 100
        self.mask = pygame.mask.from_surface(self.image)

    def move_x(self):

        self.rect.x += self.lead_x_change
        self.mask = pygame.mask.from_surface(self.image)

    def move_y(self):

        self.rect.y += self.lead_y_change
        self.mask = pygame.mask.from_surface(self.image)

    def move_xd_change(self):
        self.lead_x_change += self.speed

    def move_xa_change(self):
        self.lead_x_change += -self.speed

    def move_yu_change(self):
        self.lead_y_change += -self.speed

    def move_yd_change(self):
        self.lead_y_change += self.speed

    def stop_x_change(self):
        self.lead_x_change = 0

    def stop_y_change(self):
        self.lead_y_change = 0

class Apple(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((40, 50))
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(100, 700)
        self.rect.y = random.randint(100, 500)
        self.mask = pygame.mask.from_surface(self.image)



apple = Apple()
player = Player()

all_sprites = pygame.sprite.Group()
all_sprites.add(player, apple)
gameExit = False

while not gameExit:

    screen.fill(white)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:

                player.move_yu_change()

            if event.key == pygame.K_s:

                player.move_yd_change()

            if event.key == pygame.K_d:

                player.move_xd_change()

            if event.key == pygame.K_a:

                player.move_xa_change()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:

                player.stop_y_change()

            if event.key == pygame.K_d or event.key == pygame.K_a:

                player.stop_x_change()

    player.move_x()
    player.move_y()
    all_sprites.draw(screen)

    hit = pygame.sprite.collide_mask(player, apple)
    if hit:
        apple.rect.x = random.randint((100, 700))
        apple.rect.y = random.randint((100, 500))
        apple.mask = pygame.mask.from_surface(apple.image)

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

pygame.quit()
quit()

1 个答案:

答案 0 :(得分:2)

pygame.mask.from_surface仅在表面具有Alpha通道的情况下有效。将pygame.SRCALPHA作为flags的{​​{1}}(第二个)参数传递

pygame.Surface

或致电self.image = pygame.Surface((40, 50), pygame.SRCALPHA)

convert_alpha

或致电self.image = pygame.Surface((40, 50)).convert_alpha()

set_colorkey

顺便说一句,没有理由在self.image.set_colorkey((0, 0, 0)) move_x方法中创建新的掩码。仅当您修改move_y表面时,才需要这样做。对于性能而言,最好先创建蒙版,然后在需要时再切换一个蒙版。