Pygame对角运动不正确的角度
我希望我的玩家以与水平或垂直移动时相同的速度以对角线移动。但是,当沿着正梯度移动时,在与负梯度的一条线上移动时确实可以工作,玩家并没有以完美的45度角移动。
这是我移动玩家的代码:
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.rect.x += self.direction.x * speed
self.rect.y += self.direction.y * speed
正如我之前说的,我想要的只是让玩家以与X或Y方向相同的速度对角移动。
这是完整的玩家课程,以防需要:
class Player:
def __init__(self, x, y):
self.image = pygame.Surface((30, 30))
self.image.fill((255, 255, 255))
self.rect = self.image.get_rect(center = (x, y))
self.direction = pygame.math.Vector2()
self.speed = 5
def input(self):
keys = pygame.key.get_pressed()
if keys[K_w]:
self.direction.y = -1
elif keys[K_s]:
self.direction.y = 1
else:
self.direction.y = 0
if keys[K_a]:
self.direction.x = -1
elif keys[K_d]:
self.direction.x = 1
else:
self.direction.x = 0
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.rect.x += self.direction.x * speed
self.rect.y += self.direction.y * speed
def update(self):
self.input()
self.move(self.speed)
def draw(self, screen):
screen.blit(self.image, self.rect.center)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
pygame.Rect
应该代表一个屏幕上的区域,pygame.Rect
对象只能存储积分数据。当对象的新位置分配给 rect 对象时,坐标部分会丢失。如果要使用浮点精度移动对象,则必须将对象的位置分别存储在单独的变量属性中,并同步
pygame.Rect.Rect
对象。round> rounh
坐标并将其分配给矩形的位置:另请参见Pygame doesn't let me use float for rect.move, but I need it and
请注意,
pygame.math.math.vector2。幅度
和 > MATH.VECTOR2.Normalize
性能很昂贵。尝试避免此操作。仅在沿两个轴移动时才需要归一化。由于在这种情况下,向量的绝对值为√2,因此可以通过除以√2代替归一化。Since
pygame.Rect
is supposed to represent an area on the screen, apygame.Rect
object can only store integral data.The fraction part of the coordinates gets lost when the new position of the object is assigned to the Rect object. If you want to move the object with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the
pygame.Rect
object.round
the coordinates and assign it to the location of the rectangle:See also Pygame doesn't let me use float for rect.move, but I need it and moving with a normalized vector in pygame inconsistent?.
Note, that
pygame.math.Vector2.magnitude
andmath.Vector2.normalize
are expensive for performance. Try to avoid this operations. Normalization of the vector is only required when moving along both axes. Since the absolute value of the vector is √2 in this case, the normalization can be replaced by dividing by √2.