属性错误: 'pygame.Surface'对象没有属性'get_rect'



我读过一篇关于鼠标光标如何检测矩形的文章,其中包括一行"。get_rect(("但不知怎么的,不起作用

这里的文章代码->

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()

这是我的代码->

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()

它假设是一个图片匹配游戏btw,这是错误代码";AttributeError:"pygame.Surface"对象没有属性"get_rect">

pygame.Surface.get_rect.get_rect()返回一个与Surface对象大小相同的矩形,该矩形始终从(0,0(开始,因为Surfaceobject没有位置。曲面是屏幕上某个位置的blit。矩形的位置可以通过关键字参数指定。例如,矩形的左上角可以用关键字参数topleft:指定

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

if bcard.get_rect(topleft = (30, 200)).collidepoint(mx, my):
print('clicked on image') 

最新更新