python - AttributeError:我用来制作这个的教程视频中的一切都是正确的,但仍然存在错误
问题描述
我使用 youtube 视频制作了这款平台游戏。定义了所有属性和参数,但仍然出现错误。这是我的代码块:
def update(self):
# Game Loop Update
self.all_sprites.update()
# Check if player hits a platform only if falling
if self.player.vel.y > 0:
hits = pg.sprite.spritecollide(self.player, self.platforms, False)
if hits:
self.player.pos.y = hits[0].rect.top + 1
self.player.vel.y = 0
# If player reaches top 1/4 of screen
if self.player.rect.top <= HEIGHT / 4:
self.player.pos.y += abs(self.player.vel.y)
for plat in self.platforms:
plat.rect.y += abs(self.player.vel.y)
if plat.rect.top >= HEIGHT:
plat.kill()
这是玩家类的额外信息:
class Player(pg.sprite.Sprite):
def __init__(self, game):
pg.sprite.Sprite.__init__(self)
self.game = game
self.image = pg.Surface((30,40))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2, HEIGHT/2)
self.pos = vec(WIDTH/2, HEIGHT/2)
self.vel = vec(0,0)
self.acc = vec(0,0)
在代码中给出的第 6 行,控制台给出错误:
AttributeError: 'bool' object has no attribute 'vel'
有人可以帮忙吗?告诉我是否需要提供更多信息。
解决方案
根据错误消息,包含一个布尔值,而不是您期望self.player
的具有属性的对象。vel
问题不在您提供的代码中。我建议您查看周围的代码以确保正确处理此属性。
推荐阅读
- python - SQLAlchemy 没有将值代入查询
- ignite - 点燃:从缓存中驱逐数据并存储到数据库中
- java - Apache Quartz,如何从作业执行上下文中获取或计算先前计划的触发时间?
- ruby - 如何使用 gmail gem 从邮件中删除标签
- firebase - 如何在flutter firebase中解决此NoSuchMethodError
- ios - 在 TableView 中选择单元格后更新单元格的渐变颜色
- r - R中的子集给出了与以前相同的因子水平
- android - 如何在 ARCore 的 Sceneform 中的两个锚点之间画线?
- python - 使用 Cmake 将 ITK 和 VTK 包装到 Python 中
- r - 网络同质性的计算