gpt4 book ai didi

python - 如何在 Python 中制作游标锁?

转载 作者:行者123 更新时间:2023-12-04 08:21:48 25 4
gpt4 key购买 nike

我正在使用 Turtle 在 Python 中处理“3D”立方体。我正在尝试找到一种方法,以便我可以锁定光标,或者当您按住鼠标右键时,您可以从不同角度查看立方体。由于我不知道该怎么做,所以我有一个旋转功能。
我目前的代码:

import turtle

VERTEXES = [(-1, -1, -1), ( 1, -1, -1), ( 1, 1, -1), (-1, 1, -1),
(-1, -1, 1), ( 1, -1, 1), ( 1, 1, 1), (-1, 1, 1)]

TRIANGLES = [
(0, 1, 2), (2, 3, 0),
(0, 4, 5), (5, 1, 0),
(0, 4, 3), (4, 7, 3),
(5, 4, 7), (7, 6, 5),
(7, 6, 3), (6, 2, 3),
(5, 1, 2), (2, 6, 5)
]

FOV = 400

# Create turtle,
pointer = turtle.Turtle()

# Turn off move time, makes drawing instant,
turtle.tracer(0, 0)
pointer.up()

def rotate(x, y, r):
s, c = sin(r), cos(r)
return x * c - y * s, x * s + y * c

counter = 0
while True:
# Clear screen,
pointer.clear()

# Draw,
for triangle in TRIANGLES:
points = []
for vertex in triangle:
# Get the X, Y, Z coords out of the vertex iterator,
x, y, z = VERTEXES[vertex]

# Rotate,
x, z = rotate(x, z, counter)
y, z = rotate(y, z, counter)
x, y = rotate(x, y, counter)

# Perspective formula,
z += 5
f = FOV / z
sx, sy = x * f, y * f

# Add point,
points.append((sx, sy))

# Draw triangle,
pointer.goto(points[0][0], points[0][1])
pointer.down()

pointer.goto(points[1][0], points[1][1])
pointer.goto(points[2][0], points[2][1])
pointer.goto(points[0][0], points[0][1])
pointer.up()

# Update,
turtle.update()

counter += 0.01

最佳答案

首先,我只想说 Turtle 没有很多支持鼠标事件和其他框架具有的其他有趣的东西。
由于 turtle 在通过屏幕事件触发时缺乏对鼠标向上事件的支持,这将允许您右键单击而不是按住它,并且在将鼠标设置回原始位置之前它将继续获得鼠标移动的增量.完成拖动鼠标后,只需再次单击鼠标右键,它就会释放鼠标。
delta 会很低,通常在 1-3 之间。如果这太低,则添加 time.sleep(1/60) .这会将 fps 锁定为 60 帧,并允许鼠标​​在锁定回其原始位置之前移动更多。
我也想炫耀这个问题,因为它似乎有一种方法可以添加鼠标向上并将事件移动到 turtle ,但我会让你看看它来决定是否要走那条路。
Find the cursor's current position in Python turtle
祝你好运。

from ctypes import windll, Structure, c_long, byref
from turtle import Turtle, Screen, update


is_dragging = False
start_mouse_pos = (0, 0)


class POINT(Structure):
_fields_ = [("x", c_long), ("y", c_long)]
x: int
y: int


def getMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return (pt.x, pt.y)


def setMousePosition(pos: tuple):
windll.user32.SetCursorPos(int(pos[0]), int(pos[1]))


def onScreenClick(x, y):
global is_dragging, start_mouse_pos
is_dragging = not is_dragging
start_mouse_pos = getMousePosition()


turtle = Turtle()
screen = Screen()
screen.onscreenclick(onScreenClick, 3)

while True:
if is_dragging:
pos = getMousePosition()
delta_pos = (start_mouse_pos[0] - pos[0], start_mouse_pos[1] - pos[1])

# Do something with how much the mouse has moved then set the mouse position back

setMousePosition(start_mouse_pos)
update()

关于python - 如何在 Python 中制作游标锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com