Im relatively new to Python and Pygame. I'm making a game where a circle appears at a random location on the window and you have to touch it to get a point. I've programmed it to teleport to another random location when you touch the circle. The problem is when I try to detect collision between the circle and the player it basically teleport the circle non-stop. I also made a print function which prints hi when it detects collision and it also prints non-stop. I usually try to figure out the problem, but I really don't see what Im doing wrong, I'd appreciate all help given.
enter code here
import pygame
import random
vel = 2
pygame.init()
pygame.font.init()
fps = 60
win = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
pygame.display.set_caption("Test")
#icon = pygame.image.load('icon.png')
#pygame.display.set_icon(icon)
background = pygame.image.load('Background.png')
ghost = pygame.image.load("ghost.png")
ghostrect = ghost.get_rect()
circle = pygame.image.load("circle.png")
circlerect = circle.get_rect()
def main():
a = random.randint(0, 900)
b = random.randint(0, 500)
clock = pygame.time.Clock()
run = True
while run:
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_d] and ghostrect.x + 55 <= 900:
ghostrect.x += vel
if keys_pressed[pygame.K_a] and ghostrect.x + 5 >= 0:
ghostrect.x -= vel
if keys_pressed[pygame.K_s] and ghostrect.y + 63 <= 500:
ghostrect.y += vel
if keys_pressed[pygame.K_w] and ghostrect.y >= 0:
ghostrect.y -= vel
if circlerect.colliderect(ghostrect):
a = random.randint(0, 900)
b = random.randint(0, 500)
win.blit(background, (0, 0))
win.blit(circle, (a, b))
win.blit(ghost, (ghostrect.x + 500, ghostrect.y + 210))
pygame.display.update()
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if __name__ == "__main__":
main()