Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pygame doesnt dedect collision between sprite and image

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()
Comment

PREVIOUS NEXT
Code Example
Python :: how to change the rate of speech in pyttsx3 
Python :: how to move the pointer on screen using python 
Python :: f string decimal places 
Python :: save dataframe as csv 
Python :: python print without space 
Python :: python check disk space 
Python :: pandas drop rows with empty list 
Python :: tf.contrib.layers.xavier_initializer() tf2 
Python :: pandas plot distribution 
Python :: calculate root mean square error python 
Python :: coronavirus tips 
Python :: create django user command line 
Python :: python csv add row 
Python :: change each line color as a rainbow python 
Python :: tkinter hover button 
Python :: opencv save image rgb 
Python :: python opencv open camera 
Python :: python how to copy a 2d array leaving out last column 
Python :: how to change the column order in pandas dataframe 
Python :: display result in same page using flask api 
Python :: how to split string with comma in python 
Python :: fill na with mode and mean python 
Python :: linux command on python 
Python :: how to increase size of graph in jupyter 
Python :: playsound 
Python :: pyspark correlation 
Python :: python timedelta 
Python :: run file as administrator python 
Python :: Python - Drop row if two columns are NaN 
Python :: python dictionary dot product 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =