This question has been asked before, but the code they answer doesn't apply to me.
I want to check when two rectangles collide in my game, and for the circle, I put a rectangle behind the circle(it's colored black).
However, when I run, I get an error saying the rectangle needs to have a rect attribute.
Here is my code:
clock = pygame.time.Clock()
pygame.init()
change = False
screen = pygame.display.set_mode((400, 300))
done = False
x = 100
y = 30
bound = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
color = (255, 255, 255)
pygame.draw.rect(screen, color, (110, 30, 60, 60))
bound = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_z:
change = not change
if bound:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
x += 5
elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
y -= 5
elif change:
color2 = (0, 128, 86)
else:
color2 = (0, 128, 10)
screen.fill((0, 0, 0))
pygame.draw.rect(screen, color2, (x, y, 60, 60))
#pygame.draw.rect(screen, (255, 255, 255), (150, 30, 10, 60))
pygame.draw.rect(screen, (255, 255, 255), (x+ 65, y + 24, 20, 10))
else:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
x += 5
elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
y -= 5
elif change:
color2 = (0, 128, 86)
else:
color2 = (0, 128, 10)
screen.fill((0, 0, 0))
player = pygame.draw.rect(screen, color2, (x, y, 60, 60))
back = pygame.draw.rect(screen, (0, 0, 0), (210, 210, 40, 40))
gun1 = pygame.draw.circle(screen, (0, 0, 255), (250, 250), 40, 1)
pygame.draw.rect(screen, (255, 255, 255), (240, 245, 20, 10))
clock.tick(60)
pygame.display.flip()
Can someone please give me code that assigns the rect attribute to my rectangles?
Thanks!
See Question&Answers more detail:
os