pygame doesnt dedect collision between sprite and image

Solutions on MaxInterview for pygame doesnt dedect collision between sprite and image by the best coders in the world

showing results for - "pygame doesnt dedect collision between sprite and image"
Monica
17 Apr 2018
1import sys
2import pygame as pg
3from pygame.math import Vector2
4
5
6class Background_Line(pg.sprite.Sprite):
7
8    def __init__(self, width, height, posX, posY, side):
9        super().__init__()
10        self.image = pg.Surface([width, height])
11        self.image.fill((200, 30, 30))
12        self.rect = self.image.get_rect(topleft=(posX, posY))
13        self.side = side
14
15
16class Player(pg.sprite.Sprite):
17
18    def __init__(self, pos, *groups):
19        super().__init__(*groups)
20        self.image = pg.Surface((30, 50))
21        self.image.fill(pg.Color('steelblue2'))
22        self.rect = self.image.get_rect(center=pos)
23
24
25def main():
26    screen = pg.display.set_mode((640, 480))
27    clock = pg.time.Clock()
28
29    all_sprites = pg.sprite.Group()
30    player = Player((100, 300), all_sprites)
31    background = pg.sprite.Group(
32        Background_Line(10, 300, 175, 50, 'left'),
33        Background_Line(10, 300, 450, 50, 'right'),
34        Background_Line(285, 5, 175, 350, 'base')
35        )
36    all_sprites.add(background)
37
38    done = False
39
40    while not done:
41        for event in pg.event.get():
42            if event.type == pg.QUIT:
43                done = True
44            elif event.type == pg.MOUSEMOTION:
45                player.rect.center = event.pos
46
47        all_sprites.update()
48        #Answer
49        collided = pg.sprite.spritecollide(player, background, False)
50        for line in collided:
51            print(line.side)
52
53        screen.fill((30, 30, 30))
54        all_sprites.draw(screen)
55
56        pg.display.flip()
57        clock.tick(30)
58
59
60if __name__ == '__main__':
61    pg.init()
62    main()
63    pg.quit()
64    sys.exit()
65