python - Pygame Sprite 表 hitbox 损坏

标签 python pygame sprite collision

我做了一个简单的游戏,你必须跳过向你移动的石头。问题是,即使使用了 Sprite mask ,其中一个 Sprite 碰撞箱也会被破坏。只需将玩家 Sprite 表分为 4 列即可生成玩家 Sprite 。也许问题隐藏在那里? 节目图片: https://drive.google.com/drive/folders/1We6RDMs3Cwwprf1OY0ow9WCTHF9MHzne?usp=sharing

import pygame, os, random

pygame.init()

W, H = 800,600
HW, HH = W/2,H/2
AREA = W * H

WHITE = (255,255,255)
GREEN = (69,139,0)

FPS = 60

bg = pygame.image.load(os.path.join('Pildid', 'Taust3.png'))
bg = pygame.transform.scale(bg, (2000, 600))

DS = pygame.display.set_mode((W,H))
clock = pygame.time.Clock()


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y, py, player, veerg, rida):
        super(Player,self).__init__()
        '''Mangija huppamine'''
        clock.tick(5)
        self.x = x
        self.y = y

        self.jumping = False
        self.platform_y = py
        self.velocity_index = 0

        '''Sprite sheet'''
        self.player = pygame.image.load(os.path.join('Pildid', 'karakter.png')).convert_alpha()#pildi uleslaadimine
        #self.player = pygame.transform.scale(self.player,(200,100)) 
        self.rect = self.player.get_rect()

        '''Sprite sheeti piltide jaotamine pikslite jargi'''
        self.veerg = veerg
        self.rida = rida
        self.kokku = veerg * rida

        self.rect = self.player.get_rect()
        L = self.veergL = self.rect.width/veerg
        K = self.weegK = self.rect.height/rida
        PL,PK = self.veergKeskel = (L/2,K/2)

        self.veerg = list([(index % veerg * L, int(index/veerg) * K,L,K )for index in range(self.kokku)])
        self.handle = list([ #pildi paigutamise voimalikud positsioonid
            (0, 0), (-PL, 0), (-L, 0),
            (0, -PK), (-PL, -PK), (-L, -PK),
            (0, -L), (-PL, -K), (-L, -K),])

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

    def do_jumpt(self):
        '''Huppamine: kiirus, korgus, platvorm'''
        global velocity
        if self.jumping:
            self.y += velocity[self.velocity_index]
            self.velocity_index += 1
            if self.velocity_index >= len(velocity) - 1:
                self.velocity_index = len(velocity) - 1
            if self.y > self.platform_y:
                self.y = self.platform_y
                self.jumping = False
                self.velocity_index = 0



    def draw(self, DS,veergindex,x,y,handle=0):
        DS.blit(self.player,(self.x+self.handle[handle][0], self.y + self.handle[handle][1]),self.veerg[veergindex])

    def do(self):
        '''Funktsioonide kokkupanek'''
        self.do_jumpt()
        p.draw(DS,index%p.kokku,190, 359,4)

    def update(self):
        self.rect.center = self.x, self.y



def keys(player):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] or keys[pygame.K_UP] and player.jumping == False:
         player.jumping = True




class Obsticles(pygame.sprite.Sprite):
    '''Game obsticles: **'''
    #img = pygame.image.load(os.path.join('images', 'box.png'))
    def __init__(self, x, y):
        super(Obsticles,self).__init__()
        self.img = pygame.image.load(os.path.join('Pildid', 'kivi.png')).convert_alpha()
        self.img = pygame.transform.scale(self.img, (90,90))
        self.rect = self.img.get_rect(center=(x, y))
        self.x = x
        self.y = y
        self.mask = pygame.mask.from_surface(self.img)

    def draw(self, DS):
        '''Obsticle img blitting and hitbox'''
        DS.blit(self.img, (self.x, self.y))

    def update(self):
        if self.x < -64:  # Delete the obstacle.
            # `kill()` removes this obstacle sprite from all sprite groups.
            self.kill()

        self.x += speed  # Move the obstacle.
            # Update the rect because it's used to blit the
            # sprite and for the collision detection.
        self.rect.center = self.x, self.y

def redrawWindow():
    '''Obsticle loop'''
    for i in ob:
        i.draw(DS)

def text_objects(text, font):
    textSurface = font.render(text, True, WHITE)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',60)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((W/2),(H/4))
    DS.blit(TextSurf, TextRect)
    pygame.display.update()
    pygame.time.wait(3000)

def crash():
    message_display('Failed')


pygame.time.set_timer(pygame.USEREVENT+2, random.choice([2500, 3000, 1500]))

velocity = list([(i/ 1)-20 for i in range (0,60)])  #Huppe ulatus
index = 3

obsticles = Obsticles(832, 363)
p = Player(190, 359, 359, 'karakter.png', 4, 1)

all_sprites = pygame.sprite.Group(p, obsticles)
ob = pygame.sprite.Group(obsticles)

x = 0
x -= 1
speed = -5

running = True
while running:

    # ---Handle the events.---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.USEREVENT+2:
            r = random.randrange(0, 2)
            if r == 0:
                obsticles = Obsticles(832, 363)
                # Add the obstacle to both groups.
                ob.add(obsticles)
                all_sprites.add(obsticles)


    speed += -0.008
    # ---Game logic.---
    all_sprites.update()

    collided = pygame.sprite.spritecollide(p, ob, True, 
    pygame.sprite.collide_mask)

    if collided:
        crash()

    index += 1
    # Background movement.

    back_x = x % bg.get_rect().width
    x -= 2
    # ---Draw everything.---
    DS.blit(bg, (back_x - bg.get_rect().width, 0))
    if back_x < W:
        DS.blit(bg, (back_x, 0))


    keys(p)
    p.do()
    redrawWindow()


    pygame.display.update()
    clock.tick(60)


pygame.quit()
quit()

最佳答案

问题是您使用原始图像来创建Player.mask。生成的蒙版覆盖了角色的所有四种姿势,因此即使只有一个 Sprite 表帧可见,也会使用完整的原始图像进行碰撞检测。你的面具看起来像图中的绿色区域:

pygame mask

您还应该使用rect(即rect.topleft坐标)作为blit位置,因为它也用于碰撞检测(以找到掩模的位置)。

我有一个固定的、缩短的程序版本,其中我更改了一些内容,以向您展示如何解决问题。首先,我加载 Sprite 表并将其切割成几个子表面,然后将第一个表面/图像分配给 Playerself.image 属性。该图像可用于创建矩形掩码

update 方法中,我增加 frame_index 并将当前图像分配给 self.image 以便为 Sprite 设置动画。顺便说一句,为每个图像创建单独的蒙版并在动画过程中交换它们是一个好主意。 (我在这里只使用第一个蒙版,因为动画帧非常相似。)

请注意,如果您为 Sprite 提供了 imagerect 属性,则只需调用 all_sprites.update(DS) 即可进行 blit所有 Sprite 图像都位于其相应的矩形处。

import random
import pygame


pygame.init()
FPS = 60
DS = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
PLAYER_SHEET = pygame.image.load('karakter.png').convert_alpha()
# Cut the sprite sheet and append the subsurfaces to this list.
PLAYER_IMAGES = []
width = PLAYER_SHEET.get_width() / 4
height = PLAYER_SHEET.get_height()
for x in range(4):
    PLAYER_IMAGES.append(PLAYER_SHEET.subsurface(x*width, 0, width, height))


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y, py):
        super(Player,self).__init__()
        self.x = x
        self.y = y

        self.jumping = False
        self.platform_y = py
        self.velocity_index = 0
        self.velocity = list([(i/ 1)-20 for i in range (0,60)])

        self.frame_index = 0
        self.image = PLAYER_IMAGES[self.frame_index]
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)

    def do_jump(self):
        if self.jumping:
            self.y += self.velocity[self.velocity_index]
            self.velocity_index += 1
            if self.velocity_index >= len(self.velocity) - 1:
                self.velocity_index = len(self.velocity) - 1
            if self.y > self.platform_y:
                self.y = self.platform_y
                self.jumping = False
                self.velocity_index = 0

    def update(self):
        self.rect.center = self.x, self.y
        self.do_jump()
        # Update the animation frame.
        self.frame_index += 1
        self.frame_index %= len(PLAYER_IMAGES) * 7  # * 7 to slow it down.
        # Swap the image.
        self.image = PLAYER_IMAGES[self.frame_index//7]  # // 7 to slow it down.


def keys(player):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] or keys[pygame.K_UP] and player.jumping == False:
         player.jumping = True


class Obstacle(pygame.sprite.Sprite):
    """Game Obstacle."""

    def __init__(self, x, y):
        super(Obstacle,self).__init__()
        self.image = pygame.Surface((90, 90), pygame.SRCALPHA)
        self.image.fill((100, 150, 0))
        self.image = pygame.transform.scale(self.image, (90,90))
        self.rect = self.image.get_rect(center=(x, y))
        self.x = x
        self.y = y
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        if self.x < -64:
            self.kill()

        self.x += speed
        self.rect.center = self.x, self.y


pygame.time.set_timer(pygame.USEREVENT+2, random.choice([2500, 3000, 1500]))
index = 3

obstacle = Obstacle(832, 363)
player = Player(190, 359, 359)

all_sprites = pygame.sprite.Group(player, obstacle)
obstacles = pygame.sprite.Group(obstacle)
speed = -5

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.USEREVENT+2:
            r = random.randrange(0, 2)
            if r == 0:
                obstacle = Obstacle(832, 363)
                # Add the obstacle to both groups.
                obstacles.add(obstacle)
                all_sprites.add(obstacle)

    keys(player)

    # ---Game logic.---
    speed += -0.008

    all_sprites.update()

    collided = pygame.sprite.spritecollide(player, obstacles, True,
        pygame.sprite.collide_mask)

    if collided:
        print('crash')

    index += 1

    # ---Draw everything.---
    DS.fill((30, 30, 30))
    all_sprites.draw(DS)

    # Here I draw the outline (points) of the player's mask.
    px, py = player.rect.topleft
    for point in player.mask.outline(8):
        x, y = point[0] + px, point[1] + py
        pygame.draw.circle(DS, (250, 250, 0), (x, y), 2)

    pygame.draw.rect(DS, (0, 0, 250), player.rect, 1)
    # Draw the outlines of the obstacles.
    for obstac in obstacles:
        pygame.draw.rect(DS, (150, 250, 0), obstac.rect, 1)
        ox, oy = obstac.rect.topleft
        for point in obstac.mask.outline(6):
            pygame.draw.circle(DS, (250, 0, 0), (point[0]+ox, point[1]+oy), 2)

    pygame.display.update()
    clock.tick(60)


pygame.quit()

关于python - Pygame Sprite 表 hitbox 损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614592/

相关文章:

python - 在 python 中遍历字典中的项目

python - 管道不传递所有数据

Python Config Parser 找不到部分?

c++ - 添加 Sprite 时窗口关闭

读取注册表的 Python 代码

python - Numpy,将数组与标量相乘

eclipse - 如何让 Pygame 与 Pydev 一起工作?

python - 画圆不显示

javascript - JS : Load sprite when needed without a plugin

css - 中心图像和文本