0

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()
  • You should draw the circle with `win.blit(circle, (circlerect.x, circlerect.y))` – 001 Sep 10 '21 at 16:35
  • Also, why offset the `ghostrect` when you render it? `win.blit(ghost, (ghostrect.x, ghostrect.y))` – 001 Sep 10 '21 at 16:36
  • When you get a collision, update the circle rect: `circlerect.x, circlerect.y = a, b` – 001 Sep 10 '21 at 16:38

1 Answers1

1

You haven't set ghostrect's value. By default, pygame.Rect objects position values are 0. You also have not set circlerect's value, so its position is also at (0, 0).

Therefore, right now if circlerect.colliderect(ghostrect): will always return true. All you need to do is to give them position values to fix the problem.

Give ghostrect some position. I have chosen (100, 100), that's completely up yo you.

 ghostrect = ghost.get_rect()
 #give the rect some position value
 ghostrect.x = 100
 ghostrect.y = 100

Give circlerect the random position instead of assigning it to a and b.

def main():
    #remove them
    #a = random.randint(0, 900) #this 
    #b = random.randint(0, 500) #and this
    circlerect.x = random.randint(0, 900)#add this
    circlerect.y = random.randint(0, 500)#and add this
    #...

    while run:
        #...
        if circlerect.colliderect(ghostrect):
            #remove this
            #a = random.randint(0, 900) #this 
            #b = random.randint(0, 500) #and this
    
            circlerect.x = random.randint(0, 900)#add this
            circlerect.y = random.randint(0, 500)#and add this

Then draw the circle at this position instead of a and b.

win.blit(circle, (circlerect.x, circlerect.y))