I have created a class to create rectangles and put them in a list . I don't want them to collide so I use collidelist but it isn't working.rectangles are still colliding .
I also want rectangles to move down and change x position when hit a specific point ,
I can do that but I am not sure if it is preventing collidelist from working
Look the code below for more clarification.
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
x = random.randrange(10,900)
y = random.randrange(10,900)
sy = 10
w = random.randrange(40,90)
h = random.randrange(40,90)
rectangles =[]
class Rectangle:
def __init__(self ,color ,x,y,w,h):
self.c = color
self.x = x
self.y = y
self.w = w
self.h = h
self.rect =pygame.Rect(self.x ,self.y ,self.w ,self.h)
def draw(self):
pygame.draw.rect(display , self.c ,(self.x ,self.y ,self.w,self.h))
self.y += sy
if self.y > height:
self.y = -25
self.x = random.randint(10,900)
return self.rect
return self.rect
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c ,r_x,r_y,r_w,r_h)
rectangles.append(rectangle)
while not run:
display.fill(a)
p =pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles:
if rectangle.rect.collidelist(rectangles) > -1:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()