簡體   English   中英

在 Pygame 中用自己的碰撞將一條線作為精靈

[英]Make a line as a sprite with its own collision in Pygame

我正在制作一個連接兩個玩家之間的線的游戲。 我想做到這一點,這樣我就可以知道一個對象(也是一個精靈)何時與這條線發生碰撞。

我想這樣做的方式是創建一條充當精靈的線。 這條線將能夠根據玩家所在的位置改變長度。

我對 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)

注意與此類似的帖子已經存在,但是,該帖子中的人所問的內容與該帖子中的不同。 大體思路可能是一樣的,但我想知道一個更簡單的方法。

我建議用位掩碼測試碰撞。 請參閱如何制作碰撞遮罩? pygame.sprite.collide_mask()的文檔:

兩個精靈之間的碰撞檢測,使用遮罩。

 collide_mask(SpriteLeft, SpriteRight) -> point

通過測試它們的位掩碼是否重疊來測試兩個精靈之間的碰撞 如果精靈具有“掩碼”屬性,則將其用作掩碼,否則將根據精靈圖像創建掩碼。 旨在作為碰撞回調函數傳遞給 *collide 函數。 精靈必須有一個“rect”和一個可選的“mask”屬性。

您所要做的就是向Sprites類添加一個mask屬性。 使用pygame.mask.from_surface從給定的Surface創建一個Mask

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 個SpritesMask之間的碰撞:

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 遮罩


最小的例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM