首页 > 解决方案 > 它一直显示“TypeError:update() 需要 1 个位置参数,但给出了 2 个”

问题描述

我正在尝试使用 Python 编写游戏,但它一直显示错误“TypeError:update() 需要 1 个位置参数,但给出了 2 个”。我已经检查了多个论坛,包括 stackoverflow 多次,但他们都说当我们忘记“自我”参数时会发生错误。但是,这不是我的情况。

下面,你可以看到我的tiles.py文件。它显示了Tile类。

import pygame
class Tile(pygame.sprite.Sprite):
    def __init__(self,pos,size):
        super().__init__()
        self.image = pygame.Surface((size,size))
        self.image.fill('grey')
        self.rect = self.image.get_rect(topleft = pos)
    def update(self,x_shift):
        self.rect.x += x_shift

如您所见, the__init__()update()函数都有 self 参数。当我在以下位置运行更新部分时level.py

import pygame
from tiles import Tile
from settings import tile_size
from player import Player

class Level:
    def __init__(self,level_data,surface):
        self.display_surface = surface
        self.setup_level(level_data)
        self.world_shift = 0
    def setup_level(self,layout):
        self.tiles = pygame.sprite.Group()
        self.player = pygame.sprite.GroupSingle()
        for row_index,row in enumerate(layout):
            for col_index,cell in enumerate(row):
                x = col_index * tile_size
                y = row_index * tile_size
                if cell == 'X':
                    tile = Tile((x,y),tile_size)
                    self.tiles.add(tile)
                if cell == 'P':
                    player_sprite = Player((x,y))
                    self.tiles.add(player_sprite)
    def run(self):
        #level tiles
        self.tiles.update(self.world_shift)
        self.tiles.draw(self.display_surface)
        #player
        self.player.update()
        self.player.draw(self.display_surface)

如您所见,该run函数的中也有一个参数,即参数。但是,当我在文件中运行该函数时:self.tiles.update(self.world_shift)x_shiftrunmain.py

import pygame, sys
from settings import *
from level import Level

#Setup
pygame.init()
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
level = Level(level_map,screen)
pygame.display.set_caption('Pirates Run')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    screen.fill('black')
    level.run()

    pygame.display.update()
    clock.tick(60)

当我运行代码时,我得到的唯一输出是出现不到一秒钟然后关闭的黑屏,然后出现如下错误:

Traceback (most recent call last):
File "c:\Users\User\OneDrive\Desktop\Blazel\Pirates Run\code\main.py", line 19, in <module>
level.run()
File "c:\Users\User\OneDrive\Desktop\Blazel\Pirates Run\code\level.py", line 26, in run    
self.tiles.update(self.world_shift)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site- 
packages\pygame\sprite.py", line 531, in update
sprite.update(*args, **kwargs)
TypeError: update() takes 1 positional argument but 2 were given

请帮我解决这个问题!谢谢!

标签: pythonpygame

解决方案


首先是一些基础知识:

pygame.sprite.Group.draw()并且pygame.sprite.Group.update()是由 提供的方法pygame.sprite.Group

后者委托给update包含pygame.sprite.Sprite的 s的方法——您必须实现该方法。见pygame.sprite.Group.update()

调用update()组中所有 Sprite 的方法。[...]

前者使用包含的 s 的imagerect属性pygame.sprite.Sprite来绘制对象——您必须确保pygame.sprite.Sprites 具有所需的属性。见pygame.sprite.Group.draw()

将包含的 Sprite 绘制到 Surface 参数。这使用Sprite.image源表面的属性和Sprite.rect。[...]


您添加player_spriteself.tiles组。

self.tiles.add(player_sprite)

Tile对象有一个更新方法:

def update(self,x_shift):

Player对象有一个update带有不同参数列表的方法。这会导致错误。不要添加player_spritetiles,而是为玩家创建一个单独的来解决问题(例如pygame.sprite.GroupSingle)。


推荐阅读