python - 在 Pygame 中将一条线作为 Sprite 并具有自己的碰撞

标签 python pygame sprite collision

我正在制作一款连接 2 名玩家之间的线的游戏。我想让它这样我就可以知道一个对象(也是一个 Sprite )何时与线碰撞。

我想到的方法是创建一条充当 Sprite 的线。该线将能够根据玩家所在的位置改变长度。

我对 PyGame 有点陌生,所以我不太确定到目前为止我所拥有的:

class Line(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([400,400])
        self.image.set_colorkey((0,0,0))
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0
        pygame.draw.line(screen,(0,0,255),(0,0),(400,400),2)

注意:A similar post这已经存在,但是,该人在该帖子中询问的内容与在该帖子中询问的内容不同。总体思路可能是一样的,但我想知道一个更简单的方法。

最佳答案

我建议使用位掩码来测试冲突。请参阅How can I made a collision mask?以及 pygame.sprite.collide_mask() 的文档:

Collision detection between two sprites, using masks.

collide_mask(SpriteLeft, SpriteRight) -> point

Tests for collision between two sprites, by testing if their bitmasks overlap. If the sprites have a "mask" attribute, that is used as the mask, otherwise a mask is created from the sprite image. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "mask" attribute.

您所要做的就是向 Sprites 类添加一个 mask 属性。使用pygame.mask.from_surface从给定的表面创建掩模:

class Line(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([400, 400])
        self.image.set_colorkey((0, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0
        pygame.draw.line(screen, (0, 0, 255), (0, 0 ), (400, 400), 2)

        self.mask = pygame.mask.from_surface(self.image)

使用 pygame.sprite.collide_mask() 检测 2 个 SpriteMask 之间的碰撞:

class SpriteObject(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect(center = (x, y))
        self.mask = pygame.mask.from_surface(self.image)
other_sprite = SpriteObject(0, 0, sprite_image)
line_sprite = Line(*window.get_rect().center)
if pygame.sprite.collide_mask(line_sprite, other_sprite):
    print("hit")

另请参阅Sprite mask


最小示例:

import math
import pygame

class SpriteObject(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect(center = (x, y))
        self.mask = pygame.mask.from_surface(self.image)
    def update(self):
        self.rect.center = pygame.mouse.get_pos()

class Line(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((200, 200))
        self.image.set_colorkey((0, 0, 0))
        self.rect = self.image.get_rect(center = (x, y))
        self.angle = 0
    def update(self):
        vec = round(math.cos(self.angle * math.pi / 180) * 100), round(math.sin(self.angle * math.pi / 180) * 100)
        self.angle = (self.angle + 1) % 360
        self.image.fill(0)
        pygame.draw.line(self.image, (255, 255, 0), (100 - vec[0], 100 - vec[1]), (100 + vec[0], 100 + vec[1]), 5)
        self.mask = pygame.mask.from_surface(self.image)
        
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

sprite_image = pygame.image.load('AirPlane.png').convert_alpha()
moving_object = SpriteObject(0, 0, sprite_image)
line_object = Line(*window.get_rect().center)
all_sprites = pygame.sprite.Group([moving_object, line_object])
red = 0

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
 
    all_sprites.update()

    if pygame.sprite.collide_mask(moving_object, line_object):
        red = min(255, red+4)
    else: 
        red = 0

    window.fill((red, 0, 0))
    all_sprites.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

关于python - 在 Pygame 中将一条线作为 Sprite 并具有自己的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456195/

相关文章:

python - Pygame Color - 使用字符串时参数无效

java - 如何在Android版Cocos2d中复制CCSprite?

html - 如何从 DIV 背景中的 CSS Sprites 中选择所需的图像并对其进行定位?

python - 尝试在 pygame 中制作一个切换类型按钮。有问题

c++ - DirectX:2 个 Sprite 多边形之间的小失真

python - while循环不会在递归二分查找中停止

python - 如何将二值图像表示为图形,轴为高度和宽度尺寸,数据为像素

python - Unicode解码错误: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when deploying to Heroku

python - 仅具有垂直滚动条和 WrapSizer 的 ScrolledPanel

python - 带有 NEAT 的 pygame 的 flappy bird 游戏中不显示管道