python - 与 pygame 中的归一化向量移动不一致?

标签 python pygame

我在 pygame 中有 2 个点,我想将一个 Sprite 从点 1 移动到点 2。我采用的方法是获取两个点的向量(目标 - 开始),对该向量进行标准化,然后应用它到 Sprite 并将向量乘以速度。这确实会移动 Sprite ,但它移动的角度会根据速度而变化。

这是代码:

import pygame, sys
from pygame.math import Vector2

class Icon(pygame.sprite.Sprite):
    def __init__(self,start,end):
        super().__init__()
        # setup
        self.image = pygame.Surface((40,40))
        self.image.fill('red')
        self.rect = self.image.get_rect(center = start)
        
        # positions
        self.direction = Vector2(end) - Vector2(start) 
        self.speed = 10 # changing this changes the direction the sprite moves
        self.moving = False

    def move(self):
        self.moving = True

    def update(self):
        if self.moving:
            self.rect.center += self.direction.normalize() * self.speed 

# drawing the points to show where things are supposed to be
def draw_path_points(p1,p2):
    pygame.draw.lines(screen,'grey',False,(p1,p2),4)
    pygame.draw.circle(screen,'white',p1,20)
    pygame.draw.circle(screen,'white',p2,20)


pygame.init()
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
font = pygame.font.Font(None,40)

point_1 = (100,100)
point_2 = (400,520)
icon = pygame.sprite.GroupSingle(Icon(point_1,point_2))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                icon.sprite.move()

    screen.fill((30,30,30))
    draw_path_points(point_1,point_2)
    icon.update()
    icon.draw(screen)


    pygame.display.update()
    clock.tick(60)

我对此感到非常困惑,我是否在某个地方犯了错误或者在某个地方误解了向量?最糟糕的是,无论速度如何, Sprite 都不会真正击中第二个点,并且总是会稍微错过它......

最佳答案

pygame.Rect假设代表屏幕上的一个区域,pygame.Rect 对象只能存储完整数据。

The coordinates for Rect objects are all integers. [...]

当将对象的新位置分配给 Rect 对象时,坐标的小数部分会丢失。如果每帧都这样做,位置误差将随着时间的推移而累积。

如果要以浮点精度存储对象位置,则必须将对象的位置存储在单独的变量属性中,并同步 pygame.Rect 对象。 round坐标并将其分配给矩形的位置:

class Icon(pygame.sprite.Sprite):
    def __init__(self,start,end):
        # [...]

        self.pos = Vector2(start)
        self.rect = self.image.get_rect(center = start)
        
        # [...]

    def update(self):
        if self.moving:
            self.pos += self.direction.normalize() * self.speed 
            self.rect.center = (round(self.pos.x), round(self.pos.y))

完整示例:

import pygame, sys
from pygame.math import Vector2

class Icon(pygame.sprite.Sprite):
    def __init__(self,start,end):
        super().__init__()
        # setup
        self.image = pygame.Surface((40,40))
        self.image.fill('red')
        self.pos = Vector2(start)
        self.rect = self.image.get_rect(center = start)
        
        # positions
        self.direction = Vector2(end) - Vector2(start) 
        self.speed = 10 # changing this changes the direction the sprite moves
        self.moving = False

    def move(self):
        self.moving = True

    def update(self):
        if self.moving:
            self.pos += self.direction.normalize() * self.speed 
            self.rect.center = (round(self.pos.x), round(self.pos.y))

# drawing the points to show where things are supposed to be
def draw_path_points(p1,p2):
    pygame.draw.lines(screen,'grey',False,(p1,p2),4)
    pygame.draw.circle(screen,'white',p1,20)
    pygame.draw.circle(screen,'white',p2,20)


pygame.init()
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
font = pygame.font.Font(None,40)

point_1 = (100,100)
point_2 = (400,520)
icon = pygame.sprite.GroupSingle(Icon(point_1,point_2))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                icon.sprite.move()

    screen.fill((30,30,30))
    draw_path_points(point_1,point_2)
    icon.update()
    icon.draw(screen)


    pygame.display.update()
    clock.tick(60)

关于python - 与 pygame 中的归一化向量移动不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68486375/

相关文章:

python - 从包含特殊字符的字符串日期时间获取时间

Python 模拟修补缺少参数的函数

python - 复合索引 SQLModel

python - 为什么我们需要在 surface.blit() 之后进行 display.update() 操作?

python - PyGame MOUSEBUTTONDOWN 事件未注册?

python - Pygame碰撞精度问题

python - 如何在所有操作系统中使用 pygame 播放声音?

python - 如何使用 LIKE 运算符作为 pyspark 中的 JOIN 条件作为列

Python 加速在范围字典中搜索值

pygame - 使 `coc-python` 和 `jedi` 自动完成模块安装在虚拟环境中