python - 位置论据据说是缺失的,但给出了,有什么问题?
问题描述
我正在使用pygame
并创建了一个名为Entity
和 Subclass 的类Player
。Entity 类需要一个参数“pos”,它始终只是“pos”,因为它的值是在 init 中创建的。
class Entity(pygame.sprite.Sprite):
def __init__(self, color, stype, pos, *groups):
super().__init__(*groups)
self.image = stype
try:
self.image.fill(color)
except:
pass
self.image.convert()
self.rect = self.image.get_rect(topleft=pos)
def update(self, dt, events):
pass
class Player(Entity):
def __init__(self, platforms, pos, *groups):
super().__init__((pygame.image.load("lemon.png")), pos)
self.vel = pygame.Vector2((0, 0))
self.onGround = False
self.platforms = platforms
self.speed = 8
self.jump_strength = 10
但是,当我尝试运行代码时,它给了我这个错误:
TypeError:init()缺少1个必需的位置参数:'pos'
解决方案
为什么这里只有两个参数:
super().__init__((pygame.image.load("lemon.png")), pos)
但构造函数Entity
至少需要3个:
def __init__(self, color, stype, pos, *groups)
我不太了解您的问题,但这可能是一个解决方案:
super().__init__(color=None, stype=(pygame.image.load("lemon.png")), pos=pos)
推荐阅读
- javascript - 如何使用 Intersection Observer 为不同的目标调用不同的函数?
- python - 查找每列的负值索引
- sockets - 基于 Netty 的 http 服务器上处于 CLOSE_WAIT 状态的 tcp 连接过多
- javascript - 在浏览器中更改网址中的 URL,但不要去那里
- visual-studio - 编辑 Visual Studio .sln 文件
- python - 从 arff 文件读取时出现列表错误
- php - fgets 停止读取字符 less (<) 上的行,而不是行尾
- python - 使用 django 中间件可以解决哪些问题?
- android - 如何在第一个 Arraylist 中进行第二个 Arraylist 更新?
- python - Beautifulsoup 网页抓取。