1
votes

I have a Background Class, that looks like this:

class Background(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.left = Background_Line(10, WIN_HEIGHT, 175, 0)
        self.right = Background_Line(10, WIN_HEIGHT, 450, 0)
        self.base = Background_Line(275, 5, 175, WIN_HEIGHT - 5)
        background.add(self.left, self.right, self.base)

It is composed of three lines, that form a shape like this |_|

I am trying to implement a collision, such that when the player collides with one of these I am able to respond differently. e.g, if the left line is collided with, the player cannot move left, etc. This is my Background_Line class:

class Background_Line(pygame.sprite.Sprite):
    def __init__(self, width, height, posX, posY):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = posX
        self.rect.y = posY

Currently I am using sprite groups to detect collision between the player and the background. Each background line is stored as an individual sprite in a sprite group. However, my specific issue is that upon collision I do not know how to determine which of these lines have been collided with. I have looked through the Pycharm debugger but am finding it very confusing. A possible solution I have come up with is to just store them separately in individual sprite groups (e.g, left_background sprite group, right_background sprite group) but this doesn't seem very elegant, and would not be viable if I wanted to expand the size of the background.

1
Could you explain a bit more detailed why you need these composite background sprites? What are you actually trying to achieve in your game? - skrx
I realised I don't need composite background sprites at all. I did an OO class at university last semester so I need to remind myself not everything needs to be in a class, especially when programming in Python :). The game is basically a tetris variant, where pieces fall slowly from the top of the screen and can be moved left and tight by the player. - Perplexityy

1 Answers

2
votes

Your Background class isn't or shouldn't be a sprite, since it doesn't have an image and a rect. But it contains sprites, therefore a sprite group would be more fitting. You could either subclass pygame.sprite.Group if you need additional functionality or just use a normal group.

Regarding the lines, you could just give them a side attribute and check in your main loop for example if line.side == 'left': if a line collides with the player. Here's a complete example:

import sys
import pygame as pg
from pygame.math import Vector2


class Background_Line(pg.sprite.Sprite):

    def __init__(self, width, height, posX, posY, side):
        super().__init__()
        self.image = pg.Surface([width, height])
        self.image.fill((200, 30, 30))
        self.rect = self.image.get_rect(topleft=(posX, posY))
        self.side = side


class Player(pg.sprite.Sprite):

    def __init__(self, pos, *groups):
        super().__init__(*groups)
        self.image = pg.Surface((30, 50))
        self.image.fill(pg.Color('steelblue2'))
        self.rect = self.image.get_rect(center=pos)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()

    all_sprites = pg.sprite.Group()
    player = Player((100, 300), all_sprites)
    background = pg.sprite.Group(
        Background_Line(10, 300, 175, 50, 'left'),
        Background_Line(10, 300, 450, 50, 'right'),
        Background_Line(285, 5, 175, 350, 'base')
        )
    all_sprites.add(background)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEMOTION:
                player.rect.center = event.pos

        all_sprites.update()
        collided = pg.sprite.spritecollide(player, background, False)
        for line in collided:
            print(line.side)

        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()