gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:39 25 4
gpt4 key购买 nike

你好,我正在尝试在 pygame 中创建一个角色扮演游戏但是,每次我尝试运行代码来测试它时,它都会告诉我“‘pygame.Surface’对象的描述符‘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 坐标时,我以为我给了它一个参数

最佳答案

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

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

这同样适用于pygame.Surface.fill

screen.fill(white)

请注意,pygame.Surface 上的操作 .blit().fill() 运算符目的。如果您想填充显示器或blit显示器上的某些内容,那么您必须使用与显示器关联的Surface。在您的情况下,这是屏幕

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

或者你可以这样做:

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

分别

pygame.Surface.fill(screen, white)
<小时/>

此外,您还必须在清除显示之后和更新显示之前绘制玩家:

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

关于python - "descriptor ' blit ' of ' pygame.Surface ' object needs an argument",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58291712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com