亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python pygame - 中心軸旋轉線段

Python pygame - 中心軸旋轉線段

小怪獸愛吃肉 2023-12-08 17:18:24
import sys, pygamefrom math import sin, cos, radians, pifrom pygame.locals import QUIT, MOUSEBUTTONDOWNpygame.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()我正在嘗試編寫一條繞中心軸逆時針旋轉的線段。這很難理解。最終位置部分對我來說很難。
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

我建議使用pygame.math.Vector2.

定義直線的中心點、半徑、方向向量和角度

center_x,?center_y?=?SURFACE.get_rect().center
radius?=?100line_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)


查看完整回答
反對 回復 2023-12-08
  • 1 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號