我在我的 Pygame 程序中遇到了 AttributeError。错误消息说模块
'pygame.math' has no attribute 'Matrix44'
。我正在使用 pyrr 库中的 Matrix44 类,我使用 pip 安装了该类。我不确定是什么原因导致此错误或如何解决它。
import pygame
from pyrr.matrix44 import Matrix44
from pygame.math import Vector3
class Camera:
def __init__(self, position, fov=90):
self.position = Vector3(*position)
self.rotation = Vector3(0, 0, 0)
self.fov = fov
self.aspect_ratio = 1.0
self.near_clip = 0.1
self.far_clip = 100.0
def get_projection_matrix(self):
return pygame.math.Matrix44.perspective(
self.fov, self.aspect_ratio, self.near_clip, self.far_clip)
def get_view_matrix(self):
rotation = pygame.math.Matrix44.from_euler(self.rotation)
translation = pygame.math.Matrix44.from_translation(-self.position)
return rotation @ translation
def move(self, direction):
if direction == 'right':
self.position.x += 1
elif direction == 'left':
self.position.x -= 1
elif direction == 'up':
self.position.y += 1
elif direction == 'down':
self.position.y -= 1
elif direction == 'forward':
self.position.z -= 1
elif direction == 'backward':
self.position.z += 1
def rotate(self, axis, angle):
setattr(self.rotation, axis, getattr(self.rotation, axis) + angle)
我尝试解决错误的步骤:
检查我们已经使用 pip 安装了 pyrr 库。
这是我得到的完整详细错误:
Traceback (most recent call last):
File "/home/pi/Desktop/Project/loader.py", line 188, in <module>
main()
File "/home/pi/Desktop/Project/loader.py", line 144, in main
projection_matrix = camera.get_projection_matrix()
File "/home/pi/Desktop/Project/utilities.py", line 15, in get_projection_matrix
return pygame.math.Matrix44.perspective(
AttributeError: module 'pygame.math' has no attribute 'Matrix44'
尽管尝试了所有这些步骤,我们仍然在 Pygame 程序中遇到相同的 AttributeError。我们不确定还有什么可以尝试修复错误。任何帮助将不胜感激。
Pygame 没有
Matrix44
。 Matirx44
是从pyrr.matrix44
模块导入的:
class Camera:
# [...]
def get_projection_matrix(self):
return Matrix44.perspective(
self.fov, self.aspect_ratio, self.near_clip, self.far_clip)
def get_view_matrix(self):
rotation = Matrix44.from_euler(self.rotation)
translation = Matrix44.from_translation(-self.position)
return rotation @ translation