Pygame: столкновение спрайта не имеет выхода - PullRequest
0 голосов
/ 17 мая 2018

Я создаю игру, в которой вы можете прыгать со спрайтом, и препятствия движутся к вам.Я сделал маску спрайта и попытался использовать функцию столкновения спрайтов, но ничего не произошло: для проверки, произошло ли столкновение, я сделал простое выражение для печати.Я попытался столкнуть игрока спрайт с самим собой, и столкновение это сработало.Так что я ломаю голову над тем, чего не хватает или что не так.

Фоновое изображение
Изображение игрока Изображение препятствия

import pygame, random

pygame.init()


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

bg = pygame.image.load('Linn.png')
bg = pygame.transform.scale(bg, (800, 600))

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



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

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

        '''Sprite sheet'''
        self.paat = pygame.image.load('STlaev.png').convert_alpha() #pildi uleslaadimine
        self.paat = pygame.transform.scale(self.paat,(300,200)) #muutmaks pilti vaiksemaks
        self.rect = self.paat.get_rect()

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

        W = self.veergL = self.rect.width/veerg
        H = self.weegK = self.rect.height/rida
        HW,HH = self.veergKeskel = (W/2,H/2)

        self.veerg = list([(index % veerg * W, int(index/veerg) * H,W,H )for index in range(self.kokku)])
        self.handle = list([ #pildi paigutamise voimalikud positsioonid
            (0, 0), (-HW, 0), (-W, 0),
            (0, -HH), (-HW, -HH), (-W, -HH),
            (0, -W), (-HW, -H), (-W, -H),])

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

    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.paat,(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,300,300,0)

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

p = Player(310, 200, 200, 'STlaev.png', 4, 1) #Mangija algkordinaadid, huppe korgus, pilt, sprite valik
velocity = list([(i/ 2.0)-14 for i in range (0,50)])  #Huppe ulatus
index = 3

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, width, height):
        super(Obsticles,self).__init__()

        self.img = pygame.image.load('box.png').convert_alpha()
        self.img = pygame.transform.scale(self.img, (64,64))
        self.rect = self.img.get_rect()

        self.x = x
        self.y = y
        self.width = width
        self.height = height

        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):
        self.rect.center = self.x, self.y

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


pygame.time.set_timer(pygame.USEREVENT+2, random.choice([3000,2000,1000,800]))
objects = []


'''Sprites'''
sprites = pygame.sprite.Group()

obsticles = Obsticles(832,300,64,64)
p = Player(310, 200, 200, 'STlaev.png', 4, 1)

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

x=0
while True:

    '''Game loop'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.USEREVENT+2:
            r = random.randrange(0,2)
            if r == 0:
                objects.append(Obsticles(832,300,64,64))


    '''Obsticle speed and deleting'''
    for i in objects:
        i.x -= 5 #the speed of the obsticle
        if i.x < -64: #deleting obsticle from the window
            objects.pop(objects.index(i))

    '''Background movement'''
    back_x = x % bg.get_rect().width
    DS.blit(bg, (back_x - bg.get_rect().width, 0))
    if back_x < W:
        DS.blit(bg, (back_x, 0))
    x -= 1

    '''Sprites'''
    all_sprites.update()

    collided = pygame.sprite.spritecollide(p,ob,True,pygame.sprite.collide_mask)
    for i in collided:
        print('Collision.')


    '''Funktsioonid'''
    keys(p)
    p.do()
    index+=1

    redrawWindow()

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

pygame.quit()
quit()

1 Ответ

0 голосов
/ 17 мая 2018

Проблема в том, что вы добавляете спрайты препятствий только в список objects, но не в группу all_sprites и ob. Строка all_sprites.update() вызывает методы update всех содержащихся в ней спрайтов, и, поскольку ваши препятствия не входят в эту группу, правила не будут обновляться. Вы также должны добавить их в группу ob, поскольку она используется для обнаружения столкновений.

Я предлагаю удалить список objects и просто добавить спрайты препятствий в две группы.

class Obsticles(pygame.sprite.Sprite):

    def __init__(self, x, y):  # Removed width and height parameters.
        super(Obsticles, self).__init__()
        # Note that it would be more efficient to load the image
        # once in the global scope instead of loading it from the
        # hard disk again and again.
        # Also call the attribute `self.image`, then you can draw all
        # sprites by calling `all_sprites.draw(DS)` in the main loop.
        self.image = pygame.image.load('box.png').convert_alpha()
        self.image = pygame.transform.scale(self.image, (64, 64))
        self.rect = self.image.get_rect(center=(x, y))
        self.x = x
        self.y = y
        self.speed = -5
        self.mask = pygame.mask.from_surface(self.image)

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

    def update(self):
        self.x += self.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

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


def redrawWindow():
    """Draw the obstacles."""
    for i in ob:  # Uses the ob group now.
        i.draw(DS)


obsticles = Obsticles(832, 300)
# Use comprehensible variable names.
p = Player(310, 200, 200, 'STlaev.png', 4, 1)

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

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

    keys(p)

    # ---Game logic.---
    all_sprites.update()

    collided = pygame.sprite.spritecollide(p, ob, True, pygame.sprite.collide_mask)
    for i in collided:
        print('Collision.')

    index += 1
    # Background movement.
    x -= 1
    back_x = x % bg.get_rect().width

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

    p.do()
    redrawWindow()

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


pygame.quit()
...