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