【问题标题】:How to rotate a Vector2 in pygame如何在pygame中旋转Vector2
【发布时间】:2022-11-07 23:59:23
【问题描述】:

我正在尝试制作类似时钟的东西,但是我正在旋转矢量。 据我所知,矢量相对于屏幕的点 (0, 0) 旋转,但我希望它相对于“中心”矢量旋转。

我遇到的另一个问题是,即使 fps 被锁定在 60,矢量似乎也在加速。

这是代码:

import pygame, sys
from pygame import Vector2

pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE, 100)

angle = 0
vector = Vector2(250, 100)
center = Vector2(250, 200)

while True:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == SCREEN_UPDATE:
            vector.rotate_ip(angle)
            angle += 1


    screen.fill('black')
    pygame.draw.line(screen, 'white', center, vector)

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

我期待矢量以恒定速度旋转并且相对于“中心”矢量。

【问题讨论】:

    标签: python vector pygame vector2


    【解决方案1】:

    `rotate ip` 旋转矢量本身,但旋转角度。您必须将向量旋转一个恒定角度而不是增加角度:

    vector.rotate_ip(1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2015-04-13
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多