checking for collisions in a simple pygame code

Hey, I am new to these forums, first post!
Any who. I made a program that has 20 balls of random size and shape. the only problem is i cant figure out how to make the balls bounce off of each other. They bounce off the walls fine. But not each other. I understand that i need to do it by checking the radius of each ball. I'm not sure how to implement it

here is my code


import pygame
import random
import time
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)

pygame.init()

#creats ball class.
class Ball():
# x and y cordiates starting points before random.
x = 0
y = 0

change_x = 0
change_y = 0

size = 5

color= black
#function to move the ball on the x and y at the same time.
def move(self):
self.x = self.x + self.change_x
self.y = self.y + self.change_y
#when ball hits the wall it changes the direction of movement.
if self.x <= 0:
self.change_x = 1
if self.x == 691:
self.change_x = -1
if self.y <= 0:
self.change_y = 1
if self.y == 491:
self.change_y = -1



def draw(self, screen):
pygame.draw.circle(screen, self.color, [self.x, self.y], self.size )


# Set the width and height of the screen [width,height]
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Quintens bouncing balls")

# Used to manage how fast the screen updates
clock=pygame.time.Clock()
done=False

#Loop until the user clicks the close button.
# -------- Main Program Loop -----------
#ball1 = Ball()
ball = [Ball() for i in range(0,20)]
#for loop to creat all random atribures for balls.
for i in range (0, 20):
ball[i].x = random.randrange(0,686)
ball[i].y = random.randrange(0, 486)
ball[i].color = [random.randrange(0, 256), random.randrange(0,256),random.randrange(0,256)]
ball[i].size = random.randrange(5,20)
ball[i].change_x = random.randrange(-2, 2, 1)
ball[i].change_y = random.randrange(-2, 2, 1)
print( i, ball[i].x, ball[i].y)
time.sleep(0.003)


while done==False:

# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop

# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT


# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT

# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT


# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT

# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
#creates the screen and draws 20 balls.
screen.fill(white)
for i in range(20):
ball[i].move()
ball[i].draw(screen)


# Draw on the screen a green line from (0 ,0) to (100 ,100)
# 5 pixels wide .

# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT


# Go ahead and update the screen with what we've drawn.
pygame.display.flip()

# Limit to 20 frames per second
clock.tick(20)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories