My goal is for my program to be able to detect collision when the ball sprite hits any one of the 'g_ball' sprites. The code apperently detects collision, and i have the "print" statement to test it...but it's CONSTANTLY printing "progress" even though none of the sprites are touching. Here is the code:
while 1:
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_a:
m_x = -4
s+=1
elif event.key == K_d:
m_x = +4
s2+=1
if event.type == KEYUP:
if event.key == K_a:
m_x = 0
elif event.key == K_d:
m_x = 0
x+= m_x
y+= m_y
ball = pygame.sprite.Sprite()
ball.image = pygame.image.load('red_ball.png').convert()
ball.rect = ball.image.get_rect()
ball.image.set_colorkey((white))
screen.blit(ball.image,(x,y))
if x > 640:
x = 0
if x < 0:
x = 640
g_ball = pygame.sprite.Sprite()
g_ball.image = pygame.image.load('green_ball.png').convert()
g_ball.rect = g_ball.image.get_rect()
g_ball.image.set_colorkey(white)
screen.blit(g_ball.image,(50,t))
t+=5
if t > 521:
t = 0
collision = pygame.sprite.collide_rect(ball, g_ball)
if collision == True:
print ('PROGRESS!!!')