AttributeError: 'pygame.Surface' object has no attribute 'get_rect'

Question:

I read an article about how a mouse cursor can detect a rect, and it includes the line ".get_rect()" but somehow it doesnt work

heres the articles code ->

import pygame
 
pygame.init()
width=350;
height=400
screen = pygame.display.set_mode( (width, height ) )
pygame.display.set_caption('clicked on image')
redSquare = pygame.image.load("images/red-square.png").convert()
 
x = 20; # x coordnate of image
y = 30; # y coordinate of image
screen.blit(redSquare ,  ( x,y)) # paint to screen
pygame.display.flip() # paint screen one time
 
running = True
while (running):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            # Set the x, y postions of the mouse click
            x, y = event.pos
            if redSquare.get_rect().collidepoint(x, y):
                print('clicked on image')
#loop over, quite pygame
pygame.quit()

heres my code ->

import pygame
import os
import sys
pygame.init()

width,height = (1100,800)
WIN = pygame.display.set_mode((width,height))

global bcard
bg_filename = os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
bg = pygame.image.load(bg_filename)
bg = pygame.transform.scale(bg, (width, height)).convert()
card_width=130
card_height=160
blue_card=pygame.image.load(os.path.join('C:\Users\USER\Desktop\Python\picture match','blue_card.png'))
red_card=pygame.image.load(os.path.join('C:\Users\USER\Desktop\Python\picture match','red_card.png'))
bcard=pygame.transform.scale(blue_card,(card_width,card_height)).convert()
rcard=pygame.transform.scale(red_card,(card_width,card_height)).convert()
text=pygame.image.load(os.path.join('C:\Users\USER\Desktop\Python\picture match','text.png'))

global clicking
clicking = False

def pictures():

    global card1
    
    card1=WIN.blit(bcard,(30,200))
    card2=WIN.blit(rcard,(200,200))
    card3=WIN.blit(bcard,(370,200))
    card4=WIN.blit(rcard,(550,200))
    card5=WIN.blit(bcard,(730,200))
    card6=WIN.blit(rcard,(900,200))

    card7=WIN.blit(rcard,(30,400))
    card8=WIN.blit(bcard,(200,400))
    card9=WIN.blit(rcard,(370,400))
    card10=WIN.blit(bcard,(550,400))
    card11=WIN.blit(rcard,(730,400))
    card12=WIN.blit(bcard,(900,400))

    card13=WIN.blit(bcard,(30,600))
    card14=WIN.blit(rcard,(200,600))
    card15=WIN.blit(bcard,(370,600))
    card16=WIN.blit(rcard,(550,600))
    card17=WIN.blit(bcard,(730,600))
    card18=WIN.blit(rcard,(900,600))

    card1_rect=pygame.Rect(30,200,130,160)
    card1_rect=pygame.Rect(200,200,130,160)
    card1_rect=pygame.Rect(370,200,130,160)
    card1_rect=pygame.Rect(550,200,130,160)
    card1_rect=pygame.Rect(730,200,130,160)
    card1_rect=pygame.Rect(900,200,130,160)

    
    WIN.blit(text,(25,0))
def draw():
    WIN.blit(bg,(0,0))
    pictures()
    
def main():
    global clicking
    global card1
    global bcard
    run = True
    mx , my = pygame.mouse.get_pos()
    
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if bcard.get_rect().collidepoint(mx, my):
                        print('clicked on image') 
        draw()
        pygame.display.flip()
main()

its suppose to be a picture match game btw, heres the error code "AttributeError: ‘pygame.Surface’ object has no attribute ‘get_rect’"

Asked By: captainchungus

||

Answers:

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft:

if bcard.get_rect().collidepoint(mx, my):

if bcard.get_rect(topleft = (30, 200)).collidepoint(mx, my):
    print('clicked on image') 
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.