python - 它一直显示“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_shift
run
main.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
请帮我解决这个问题!谢谢!
解决方案
首先是一些基础知识:
pygame.sprite.Group.draw()
并且pygame.sprite.Group.update()
是由 提供的方法pygame.sprite.Group
。
后者委托给update
包含pygame.sprite.Sprite
的 s的方法——您必须实现该方法。见pygame.sprite.Group.update()
:
调用
update()
组中所有 Sprite 的方法。[...]
前者使用包含的 s 的image
和rect
属性pygame.sprite.Sprite
来绘制对象——您必须确保pygame.sprite.Sprite
s 具有所需的属性。见pygame.sprite.Group.draw()
:
将包含的 Sprite 绘制到 Surface 参数。这使用
Sprite.image
源表面的属性和Sprite.rect
。[...]
您添加player_sprite
到self.tiles
组。
self.tiles.add(player_sprite)
该Tile
对象有一个更新方法:
def update(self,x_shift):
该Player
对象有一个update
带有不同参数列表的方法。这会导致错误。不要添加player_sprite
到tiles
,而是为玩家创建一个单独的组来解决问题(例如pygame.sprite.GroupSingle
)。
推荐阅读
- fft - 使用 FFT 为 Shamir Secret Share 加速 Berlekamp Welch 算法
- docker - 在 gitlab ci 作业中使用 kubectl 创建 ImagePullSecret 时出现奇怪的错误
- python - 期权价格路径的 Python 蒙特卡罗模拟
- java - HttpServletResponse 的 sendRedirect 调用无法执行
- c++ - 获取当前 HWND 的 IDCompositionDevice
- awk - AWK 中的逻辑运算符和日期
- reactjs - 当我发送发布请求时,Django Rest Framework 被禁止
- c# - C# 应用程序无法加载嵌入的文本资源
- excel - 在 Excel 中,如何通过匹配 d 列和 h 列中的名称(不按顺序)从 c 列返回不同名称的列表,并且不带空格列出?
- visual-studio-code - VScode 中的系统环境变量