"descriptor 'blit' of 'pygame.Surface' object needs an argument"

"descriptor 'blit' of 'pygame.Surface' object needs an argument"

您好,我正在尝试在 pygame 中创建一个角色扮演游戏 但是,每次我尝试 运行 测试它的代码时,它都会告诉我 "descriptor 'blit' of 'pygame.Surface' object needs an argument"

我想我已经给它一个论据了。

import pygame
import os
from pygame.locals import*

screen_width = 1900
screen_height = 1080

black = (0, 0, 0)
white = (255, 255 ,255)
red = (255, 0 ,0)
lime = (0, 255, 0)
blue = (255, 255, 0)
aqua = (0, 255, 255)
magenta = (255, 0, 255)
silver = (192, 192, 192)
gray = (128, 128, 128)
maroon = (128, 0, 0)
olive = (128, 128, 0)
green = (0, 128, 0)
purple = (128, 0, 128)
teal = (0, 128, 128)
navy = (0, 0, 128)

pygame.display.init()
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('This is a rpg')
clock = pygame.time.Clock()

current_path = os.path.dirname('untitled')
resource_path = os.path.join(current_path, 'Characters')
character_path = os.path.join(resource_path, 'Knight')
image_path = os.path.join(character_path, 'Walk_Attack')

playerimg = pygame.image.load(os.path.join(character_path, 'knight.png'))

x = (screen_width * 0.45) 
# I only put it up here because it told me "x" wasent defined in "def player(x, y):"

y = (screen_height * 0.8)

def player(x, y):
    x = (screen_width * 0.45)
    y = (screen_height * 0.8)
    pygame.Surface.blit(source = playerimg, x = x, y = y)

dead = False

while not dead:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True
    player(x, y)
    pygame.Surface.fill(white)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()
quit()

screen.fill(white)

我希望它打开时出现白屏。也许能够看到图片。但它所做的只是告诉我 blit 需要一个参数。 当我告诉它找到图片的位置以及它的 x 和 y 坐标时,我以为我给了它一个论点

看来您正在尝试学习如何在 Python 中编写代码并同时使用 Pygame 库。我不推荐这个;首先学习更多的代码编写基础知识。但我会尽力解决你的困惑:

but all it does is

对;启动过程中出现错误,因此还无法显示任何内容。

which i thought i gave it an argument when i told it the place to find the picture and it x and y coords

关键在于 descriptorobject 这两个词。 pygame.Surface is a class, and blit 是 class 的 的 方法,但您正试图将其用作普通函数。

您需要创建 class 的实例并使用其方法。您已经有这样一个实例:它被称为 screen,它是由 pygame.display.set_mode 创建的。所以现在我们可以,例如,screen.blit(playerimg, (x, y)).

I only put it up here because it told me "x" wasent defined in "def player(x, y):"

发生这种情况是因为您将 player 定义为接受 xy 值,但是在代码 外部 函数中,您尝试使用 xy 外部 函数调用它 - 这不存在。请仔细研究例子:

def player(x, y):
    # We don't need to define x and y inside, because they will be given to us
    # if we do assign some values, it replaces what we were passed in.
    # This is bad, because the point is to let the outside code decide.
    print(f"I was passed in: x = {x}, y = {y}")

x = 1
y = 2
# this requires the above; it has nothing to do with x and y inside the function.
player(x, y)

# but we can use different names:
a = 1
b = 2
player(a, b)

# or no names at all:
player(1, 2)

(再一次;先学习更多的代码编写基础知识;然后当这样的事情发生时以及为什么发生时,您将很明显,并且您将能够在第一时间正确修复它,并理解为什么正确修复是正确的。)

pygame.Surface.blit is a Method 和第二个参数 (dest) 是一个包含 2 个组件的位置元组,因此它必须是:

screen.blit(source = playerimg, dest = (x, y))

同样适用于pygame.Surface.fill

screen.fill(white)

请注意,pygame.Surface 对象上的操作 .blit().fill() 运算符。如果你想 fill 显示器或 blit 显示器上的东西,那么你必须使用与显示器相关联的表面。在你的情况下,这是 screen;

screen = pygame.display.set_mode([screen_width, screen_height])

或者你可以这样做:

pygame.Surface.blit(screen, playerimg, (x, y))

分别

pygame.Surface.fill(screen, white)

此外,您还需要绘制 player,在清除显示之后和更新显示之前:

screen.fill(white)
player(x, y)
pygame.display.flip()