This program should print "True", only if green rect touches the red line, but it prints True every time green rect got into red lines "area"(touches the lines sprite). Is there a way to make lines sprite?
In this code green is not on line, but still prints true:
class Line(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((200, 200))
self.rect = self.image.get_rect()
self.rect.x = 50
self.rect.y = 0
def update(self):
pygame.draw.line(screen, (255, 0, 0), (self.rect.x, self.rect.y), (200, 200))
class Rectt(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50, 50))
self.rect = self.image.get_rect()
self.rect.x = 25
self.rect.y = 100
def update(self):
pygame.draw.rect(screen, (0, 255, 0), self.rect)
pygame.init()
screen = pygame.display.set_mode((300, 300))
screen.fill((0, 0, 0))
running = True
l = Line()
m = Rectt()
while running:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
running = False
if (pygame.sprite.collide_mask(m, l)):
print(True)
else:
print(False)
l.update()
m.update()
pygame.display.flip()
screen.fill((0, 0, 0))
pygame.quit()