ionicals
ionicals

Reputation: 23

Mouse not interacting with object correctly using collidepoint function

So I used the collidepoint function to test out whether or not my mouse is interacting or can interact with the images on the surface Surface but the variable mouse_pos does give out a position yet the mouse cannot ever collide with the object (see A is always false rather than true when the mouse hit the object). How do I solve this

Code:

import pygame
from sys import exit

pygame.init()

widthscreen = 1440 #middle 720
heightscreen = 790 #middle 395
w_surface = 800
h_surface = 500
midalignX_lg = (widthscreen-w_surface)/2
midalignY_lg = (heightscreen-h_surface)/2

#blue = player
#yellow = barrier

screen = pygame.display.set_mode((widthscreen,heightscreen))

pygame.display.set_caption("Collision Game")
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 45)


surface = pygame.Surface((w_surface,h_surface))
surface.fill('Light Yellow')

blue_b = pygame.image.load('images/blue.png').convert_alpha()
blue_b = pygame.transform.scale(blue_b,(35,35))

yellow_b = pygame.image.load('images/yellow.png').convert_alpha()
yellow_b = pygame.transform.scale(yellow_b,(35,35))

text_surface = test_font.render('Ball Option:', True, 'White')

barrier_1_x = 0
barrier_1_surf = pygame.image.load('images/yellow.png').convert_alpha()
barrier_1_surf = pygame.transform.scale(barrier_1_surf,(35,35))
barrier_1_rect =  barrier_1_surf.get_rect(center = (100, 350))

player_surf = pygame.image.load('images/blue.png').convert_alpha()
player_surf = pygame.transform.scale(player_surf,(35,35))
player_rect = player_surf.get_rect(center = (0,350))

while True:
    #elements & update

    #event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    screen.blit(surface, (midalignX_lg,midalignY_lg))
    screen.blit(blue_b,(150,250))
    screen.blit(yellow_b, (150,300))
    screen.blit(text_surface,(150, 200))

    #barrier_1_x += 3
    #if barrier_1_x > 800: barrier_1_x = 0

    #barrier_1_rect.x += 3
    #if barrier_1_rect.x > 800:  barrier_1_rect.x = 0 

    barrier_1_rect.x += 2
    if barrier_1_rect.right >= 820: barrier_1_rect.left = -10
    player_rect.x += 3
    if player_rect.right >= 820: player_rect.left = -10

    surface = pygame.Surface((w_surface,h_surface))
    surface.fill('Light Yellow')
    surface.blit(barrier_1_surf, barrier_1_rect)
    surface.blit(player_surf, player_rect)

    '''if player_rect.colliderect(barrier_1_rect):
        print('collision')'''

    A = False;
    mouse_pos = pygame.mouse.get_pos()
    
    if player_rect.collidepoint(mouse_pos):
        A = True

    print(A)
    pygame.display.update()

    clock.tick(60)

    

I am not sure what else to do. i think it may be something wrong with the layering of the surface?

Upvotes: 1

Views: 38

Answers (1)

Rabbid76
Rabbid76

Reputation: 210947

You are not drawing the objects on the screen, but on the surface. Therefore the coordinates of player_rect are relative to the surface and you also have to calculate the mouse position relative to the surface. The top left coordinate of the surface is (midalignX_lg, midalignY_lg):

while True:
    # [...]
  
    mouse_pos = pygame.mouse.get_pos()
    rel_x = mouse_pos[0] - midalignX_lg
    rel_y = mouse_pos[1] - midalignY_lg
    if player_rect.collidepoint(rel_x, rel_y):
        print("hit")

Upvotes: 1

Related Questions