Pygame 不是 blitting 图像,而是吐出类型错误

Pygame not blitting image,instead spitting out type error

所以我在网上观看了关于 pygame 的教程,因为我对 pygame 非常陌生。所以他所做的是创建一个 player() 函数,然后 blit 它。所以我也这样做了。对他来说,他得到了完美的 blitt,但对我来说,我得到了一个类型错误。他的代码和我的代码唯一不同的是 window 和播放器 class 的尺寸和颜色。我尝试在堆栈溢出中搜索它,但唯一相关的问题没有得到解答。不确定哪里出了问题。

全部错误:

/home/Command Blocks/Desktop/venv/bin/python "/home/CommandBlocks/Desktop/CLASS_/Studies/Computer/python/practice/space shooter/main.py"
pygame 2.0.2 (SDL 2.0.16, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/home/Command Blocks/Desktop/CLASS_/Studies/Computer/python/practice/space shooter/main.py", line 33, in <module>
    player()
  File "/home/Command Blocks/Desktop/CLASS_/Studies/Computer/python/practice/space shooter/main.py", line 20, in player
    screen.blit(player, (playerX, playerY))
TypeError: argument 1 must be pygame.Surface, not function

Process finished with exit code 1

完整代码:

import pygame

#Initialize pygame module
pygame.init()

#Create Screen
screen = pygame.display.set_mode((1000, 600))

#Title and Icon
pygame.display.set_caption("Jungle Invader")
icon = pygame.image.load('fox-sitting.png')
pygame.display.set_icon(icon)

# Player
player = pygame.image.load('cat.png')
playerX = 300
playerY = 500

def player():
    screen.blit(player, (playerX, playerY))


#Main loop
running = True
while running:
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((244, 232, 255))
    player()

你已经两次使用玩家这个名字。就是pygame.image.load('cat.png')返回的surface和blits返回到屏幕的函数。将 player 函数重命名为 drawPlayer