简体   繁体   English

“'pygame.Surface' object 的描述符 'blit' 需要一个参数”

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

Hello I am trying to create a rpg game in pygame However, everytime i try to run the code to test it it tells me "descriptor 'blit' of 'pygame.Surface' object needs an argument"您好,我正在尝试在 pygame 中创建一个 rpg 游戏但是,每次我尝试运行代码来测试它时,它都会告诉我“'pygame.Surface' object 的描述符'blit'需要一个参数”

I believe I have already givin it 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)

i expect it to open to a white screen.我希望它打开一个白屏。 maybe be able to see the pic.也许可以看到图片。 but all it does is tells me blit needs an argument.但它所做的只是告诉我 blit 需要一个参数。 which i thought i gave it an argument when i told it the place to find the picture and it x and y coords当我告诉它找到图片的位置以及它的 x 和 y 坐标时,我以为我给了它一个论据

It seems like you are trying to learn how to write code in Python and use the Pygame library at the same time.您似乎正在尝试学习如何在 Python 中编写代码并同时使用 Pygame 库。 I don't recommend this;我不推荐这个; learn some more code-writing fundamentals first.首先学习更多的代码编写基础知识。 But I will try to help with your confusion:但我会尽力帮助您解决困惑:

but all it does is但它所做的只是

Right;正确的; there is an error during the start-up, so it can't show you anything yet.启动过程中出现错误,所以它还不能显示任何东西。

which i thought i gave it an argument when i told it the place to find the picture and it x and y coords当我告诉它找到图片的位置以及它的 x 和 y 坐标时,我以为我给了它一个论据

The key is in the words descriptor and object .关键在于单词descriptorobject pygame.Surface is a class , and blit is a method of that class, but you are trying to use it as if it were a plain function. pygame.Surface is a class , and blit is a method of that class, but you are trying to use it as if it were a plain function.

You need to create an instance of the class, and use its methods.您需要创建 class 的实例,并使用其方法。 You already have such an instance: it is called screen , and it was created by pygame.display.set_mode .你已经有了这样一个实例:它叫做screen ,它是由pygame.display.set_mode创建的。 So now we can, for example, screen.blit(playerimg, (x, y)) .所以现在我们可以,例如screen.blit(playerimg, (x, y))

I only put it up here because it told me "x" wasent defined in "def player(x, y):"我之所以把它放在这里是因为它告诉我在“def player(x, y):”中定义了“x”

This happened because you defined player to accept x and y values, but in the code outside the function, you tried to call it using the x and y from outside the function - which didn't exist.发生这种情况是因为您将player定义为接受xy值,但在 function之外的代码中,您尝试使用 function外部xy调用它- 这不存在。 Please study the example carefully:请仔细研究示例:

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)

(Again; learn more code-writing fundamentals first; then it will be obvious to you when something like this happens and why, and you will be able to fix it properly, the first time, and understand why the proper fix is proper.) (再次强调一下;首先学习更多的代码编写基础知识;然后当发生这种情况以及发生这种情况的原因时,您会很清楚,并且您将能够第一次正确地修复它,并理解为什么正确的修复是正确的。)

pygame.Surface.blit is a Method and the 2nd parameter ( dest ) is a position tuple with 2 components, so it has to be: pygame.Surface.blit是一个 方法,第二个参数( dest )是一个具有 2 个组件的 position 元组,所以它必须是:

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

The same applies to pygame.Surface.fill这同样适用于pygame.Surface.fill

screen.fill(white)

Note, the operations .blit() and .fill() operator on an pygame.Surface object.注意,pygame.Surface object 上的操作pygame.Surface .blit().fill()操作符。 If you want to fill the display or to blit something on the display, then you've to use the Surface which is associated the to the display.如果您想fill显示屏或在显示屏上对某些内容进行blit ,那么您必须使用与显示屏相关联的 Surface。 In your case this is screen ;在您的情况下,这是screen

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

Alternatively you can do:或者,您可以这样做:

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

respectively分别

pygame.Surface.fill(screen, white)

Furthermore you've to draw player , after the display was cleared and before the display is updated:此外,您必须在显示被清除之后和显示更新之前绘制player

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM