-1

For some reason my code is printing 1 even though it has not collided with any other ball or with the floor. I am using sys, pygame.locals and pygame as my main libraries and no error is appearing anywhere.

code:

pygame.init()

screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1

def mainPhysics():
    for circle_pos in circles:
        circle_pos[1] += gravity
        if circle_pos[1] > 495:
            boundaries = circle_pos[1] - 495
            circle_pos[1] -= boundaries
        if circle_pos[0] < 0:
            boundaries2 = circle_pos[0]
            circle_pos[0] += boundaries2
        if circle_pos[0] > 995:
            boundaries3 = circle_pos[0] - 995
            circle_pos[0] - boundaries3
        circle_rect = pygame.draw.circle(screen, green, circle_pos, 5)

        collide = circle_rect.collidepoint(circle_pos)
        print('1') if collide else print('0')



def input():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            circles.append(list(event.pos))
        if event.type == pygame.MOUSEBUTTONDOWN and pygame.MOUSEMOTION:
            circles.append(list(event.pos))

clock = pygame.time.Clock()
while True:
    screen.fill((15, 15, 15))

    input()
    mouse_pos = pygame.mouse.get_pos()

    mainPhysics()
    pygame.display.update()
    clock.tick(120)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jigsaw
  • 294
  • 1
  • 3
  • 14
  • 1
    Why do you repeat the question? [Why does my collision not work or act naturally at all?](https://stackoverflow.com/questions/67182115/pygame-rect-object-is-not-callable) – Rabbid76 Apr 20 '21 at 16:52
  • 1
    What does `circle_rect.collidepoint(circle_pos)` return? If it's anything other than `0` or `None`, `1` is printed. – AcK Apr 20 '21 at 16:55
  • 1
    Given the definition of `circle_rect`, how could you expect `circle_rect` not to be in collision with `circle_pos`? – khelwood Apr 20 '21 at 16:56

1 Answers1

1

For a collision detection you need 2 objects. Iterate through the circles in nested loops. To detect the collision between 2 circles see Pygame how to let balls collide, pygame Get the balls to bounce off each other or How do I detect collision in pygame?:

for i, circle_pos_1 in enumerate(circles):
    for circle_pos_2 in circles[i+1:]:
        dx = circle_pos_2[0] - circle_pos_1[0]  
        dy = circle_pos_2[1] - circle_pos_1[1]
        if dx*dx + dy*dy < 5*5:
            # [...]

Minimal example:

import pygame
pygame.init()

screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1

def input():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            circles.append(list(event.pos))

count = 0
clock = pygame.time.Clock()
while True:
    screen.fill((15, 15, 15))

    input()
    mouse_pos = pygame.mouse.get_pos()

    for circle_pos in circles:
        circle_pos[1] += gravity
        if circle_pos[1] > 495:
            boundaries = circle_pos[1] - 495
            circle_pos[1] -= boundaries
        if circle_pos[0] < 0:
            boundaries2 = circle_pos[0]
            circle_pos[0] += boundaries2
        if circle_pos[0] > 995:
            boundaries3 = circle_pos[0] - 995
            circle_pos[0] - boundaries3

    for i, circle_pos_1 in enumerate(circles):
        for circle_pos_2 in circles[i+1:]:
            dx = circle_pos_2[0] - circle_pos_1[0]  
            dy = circle_pos_2[1] - circle_pos_1[1]
            if dx*dx + dy*dy < 5*5:
                count += 1 
                print(f"hit: {count}")           

    for circle_pos in circles:
        pygame.draw.circle(screen, green, circle_pos, 5)

    pygame.display.update()
    clock.tick(120)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174