为什么一个 .get_rect() 调用会引发错误而另一个不会?

时间:2021-07-18 08:47:51

标签: python pygame

所以,我刚刚开始使用 python 和 pygame,尽管之前的 get_rect() 方法没有问题,但我在 get_rect() 方法中遇到了属性错误。

import pygame
import sys
import os

#Current working directory.
print(os.getcwd())

#Does path exist?
print (os.path.exists("D:\Learning Python\Pygame Bootcamp\My Code\The Game\Images"))

filepath = "D:\Learning Python\Pygame Bootcamp\My Code\The Game\Images"

#Locations for the images.
grassDir = os.path.join(filepath,"grass.png")
ballDir = os.path.join(filepath,"ball.png")
characterBodyDir = os.path.join(filepath,"characterBody.png")
characterFootDir = os.path.join(filepath,"characterFoot.png")


#Initializes PyGame.
pygame.init()

#Defining the display dimensions.
width = 900
height = 700
screenDim = (width, height)

#Initializes a window or screen for display with defined dimensions.
#.set_mode needs to have a tuple.
screen = pygame.display.set_mode(screenDim)

#Sets window title.
pygame.display.set_caption("The Game")

#Loads the image.
#".convert()" loads the image into a pygame object called a surface.
#It makes there pixel values the same. Makes loading the image faster.
grassImage = pygame.image.load(grassDir).convert()
#Transforms the image. In this case, it scales the image to the screen dimensions.
grassImage = pygame.transform.scale(grassImage,(width, height))
#A rescale factor. Adjust to adjust scale of all images.
rescale = 3

#Player
player = pygame.image.load(characterBodyDir).convert_alpha()
#".get_rect()" Takes an attribute from the surface object.
playerWidth = player.get_rect().width
playerHeight = player.get_rect().height
#Rescaling
player = pygame.transform.scale(player,(playerWidth * rescale, playerHeight * rescale))
#pygame.transform.rotate() rotates COUNTER-CLOCKWISE.
player = pygame.transform.rotate(player,90)

#Foot
foot = pygame.image.load(characterFootDir).convert_alpha
footWidth = foot.get_rect().width
footHeight = foot.get_rect().height
foot = pygame.transform.scale(foot,(footWidth * rescale,footHeight * rescale))
foot = pygame.transform.rotate(foot,90)

#Draws image to screen, takes image and position as input.
screen.blit(grassImage,(0,0))
screen.blit(player,(0,0))
screen.blit(foot,(0,0))

#Tells if the game is finished.
finished = False


#Start of the game loop.
while finished == False:
    #Processing all the events:
    for event in pygame.event.get(): #Do things in this loop.
        #Quits the game.
        if event.type == pygame.QUIT:
                finished = True
                pygame.quit()
                sys.exit()

    pygame.display.flip() #Updates the display/load next frame.

错误信息是这样的:

D:\Learning Python
True
Traceback (most recent call last):
  File "d:\Learning Python\Pygame Bootcamp\My Code\The Game\main.py", line 56, in <module>
    footWidth = foot.get_rect().width
AttributeError: 'builtin_function_or_method' object has no attribute 'get_rect'

有人可以帮忙吗?据我所知,playerWidthfootWidth 应该没问题,但 playerWidth 是,而 footWidth 不是。

1 个答案:

答案 0 :(得分:0)

您没有第二次调用该函数:

player = pygame.image.load(characterBodyDir).convert_alpha()
# vs
foot = pygame.image.load(characterFootDir).convert_alpha # missing '()'