Python pygame - 中心轴旋转线段

标签 python python-3.x pygame rotation radius

import sys, pygame
from math import sin, cos, radians, pi
from pygame.locals import QUIT, MOUSEBUTTONDOWN

pygame.init()
SURFACE = pygame.display.set_mode((780,920))
FPSCLOCK = pygame.time.Clock()

def Line():
    speed = 0

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        if pygame.mouse.get_pressed() [0]:
            speed += 26
            if speed > 60:
                speed = 60

        else:
            speed -= 15
            if speed < 0:
                speed = 0

        SURFACE.fill((255,0,0))

        radius = speed * 0.8
        
        pygame.draw.line(SURFACE,(5,80,255),(295,127),(sin(200-radius),cos(300-radius)),8)
        pygame.display.update()
        FPSCLOCK.tick(6)

if __name__ == '__main__' :
    Line()

我正在尝试编写一条绕中心轴逆时针旋转的线段。这很难理解。最终位置部分对我来说很难。

最佳答案

我建议使用pygame.math.Vector2 .

定义直线的中心点、半径、方向向量和角度

center_x, center_y = SURFACE.get_rect().center
radius = 100
line_vector = pygame.math.Vector2(1, 0)
angle = 0

由于要逆时针旋转,因此需要在按下鼠标按钮时减小角度:

if pygame.mouse.get_pressed()[0]:
    angle -= 1

使用pygame.math.Vector2.rotate()旋转方向向量并计算旋转后的线的起点和终点

rot_vector = line_vector.rotate(angle) * radius
start = round(center_x + rot_vector.x), round(center_y + rot_vector.y)
end = round(center_x - rot_vector.x), round(center_y - rot_vector.y)

使用startend来画线:

pygame.draw.line(SURFACE, (5,80,255), start, end, 8)

最小示例: repl.it/@Rabbid76/PyGame-VectorRotateLine

完整示例:

import sys, pygame
from math import sin, cos, radians, pi
from pygame.locals import QUIT, MOUSEBUTTONDOWN
pygame.init()
SURFACE = pygame.display.set_mode((780,920))
FPSCLOCK = pygame.time.Clock()

def Line():
    center_x, center_y = SURFACE.get_rect().center
    radius = 100
    line_vector = pygame.math.Vector2(1, 0)
    angle = 0
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        if pygame.mouse.get_pressed()[0]:
            angle -= 1
            print(angle)
        
        rot_vector = line_vector.rotate(angle) * radius
        start = round(center_x + rot_vector.x), round(center_y + rot_vector.y)
        end = round(center_x - rot_vector.x), round(center_y - rot_vector.y)
       
        SURFACE.fill((255,0,0))
        pygame.draw.line(SURFACE, (5,80,255), start, end, 8)
        pygame.display.update()
        FPSCLOCK.tick(60)

if __name__ == '__main__' :
    Line()

如果你想绕线的起点旋转,那么你不需要中心点。定义行的开头:

start_x, start_y = SURFACE.get_rect().center
length = 200

计算旋转的终点:

rot_vector = line_vector.rotate(angle) * length
start = start_x, start_y
end = round(start_x + rot_vector.x), round(start_y + rot_vector.y)

关于Python pygame - 中心轴旋转线段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64446045/

相关文章:

python - PyOpenGL + PyGame 上的奇怪 "X"

python - PIL 在终端中工作,但不能通过 IDLE

python - 在 python3 中安装第三方模块 - Ubuntu

python - 如何在 drf-yasg swagger_auto_schema request_body 上指定示例值?

python - spacy 3 更新后,NLP 更新无法与元组一起使用

python - 使用 Python 高效查找原根模 n?

string - 为什么当我在切片时使用大于 len(string) 的索引号时没有 IndexError?

regex - 如何对一个文件执行多个 re.sub()?

python - 我的 Sprite 在 Pygame 中无法正确显示

python - 如何重新触发之前的 'while' 循环