![](/img/trans.png)
[英]Error: descriptor 'blit' requires a 'pygame.Surface' object but received a 'NoneType'
[英]"descriptor 'blit' of 'pygame.Surface' object needs an argument"
您好,我正在尝试在 pygame 中创建一个 rpg 游戏但是,每次我尝试运行代码来测试它时,它都会告诉我“'pygame.Surface' object 的描述符'blit'需要一个参数”
我相信我已经给它一个论据。
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 库。 我不推荐这个; 首先学习更多的代码编写基础知识。 但我会尽力帮助您解决困惑:
但它所做的只是
正确的; 启动过程中出现错误,所以它还不能显示任何东西。
当我告诉它找到图片的位置以及它的 x 和 y 坐标时,我以为我给了它一个论据
关键在于单词descriptor
和object
。 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.
您需要创建 class 的实例,并使用其方法。 你已经有了这样一个实例:它叫做screen
,它是由pygame.display.set_mode
创建的。 所以现在我们可以,例如screen.blit(playerimg, (x, y))
。
我之所以把它放在这里是因为它告诉我在“def player(x, y):”中定义了“x”
发生这种情况是因为您将player
定义为接受x
和y
值,但在 function之外的代码中,您尝试使用 function外部的x
和y
调用它- 这不存在。 请仔细研究示例:
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
是一个 方法,第二个参数( dest
)是一个具有 2 个组件的 position 元组,所以它必须是:
screen.blit(source = playerimg, dest = (x, y))
这同样适用于pygame.Surface.fill
screen.fill(white)
注意,pygame.Surface object 上的操作pygame.Surface
.blit()
和.fill()
操作符。 如果您想fill
显示屏或在显示屏上对某些内容进行blit
,那么您必须使用与显示屏相关联的 Surface。 在您的情况下,这是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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.