Turtle Graphics - 调整要显示的形状
Turtle Graphics - Resize Shapes To Display
我正在使用 Python 2.7。我只能使用 turtle 模块来创建游戏。我的 1920 x 1080 显示器 完美缩放所有图像并且动画播放正常,但是 (例如) 在 1280 x 720 显示,图像未正确缩放。我正在使用 turtle.shape()
作为图像。我该如何解决这个问题?
我添加了一张图片
和 video 进一步解释问题。
如您所见,图标重叠,图像向后和第四弹跳,而不是直接前进。
这是我定义屏幕尺寸的方式:
screen_width = ctypes.windll.user32.GetSystemMetrics(0) - ctypes.windll.user32.GetSystemMetrics(0) / 4
screen_height = ctypes.windll.user32.GetSystemMetrics(1) - ctypes.windll.user32.GetSystemMetrics(1) / 4
- 它采用显示大小,减去自身除以 4 以获得 window 的最终显示。
如果没有您的代码,我无法在我的环境中对此进行全面测试。这个想法是使用虚拟坐标,以便在统一处理各个图形元素的级别上进行缩放:
import turtle
VIRTUAL_ASPECT_RATIO = 1.8
VIRTUAL_WIDTH = 2000
VIRTUAL_HEIGHT = VIRTUAL_WIDTH / VIRTUAL_ASPECT_RATIO
screen_width = 3 * ctypes.windll.user32.GetSystemMetrics(0) / 4
screen_height = 3 * ctypes.windll.user32.GetSystemMetrics(1) / 4
# attempt to adjust the real window to match the virtual aspect ratio
if screen_width / VIRTUAL_ASPECT_RATIO < screen_height:
screen_height = screen_width / VIRTUAL_ASPECT_RATIO
elif screen_height * VIRTUAL_ASPECT_RATIO < screen_width:
screen_width = screen_height * VIRTUAL_ASPECT_RATIO
else:
pass # probably need to do something where we adjust both
screen = turtle.Screen()
screen.setup(screen_width, screen_height) # size the real window
screen.setworldcoordinates(-VIRTUAL_WIDTH/2, -VIRTUAL_HEIGHT/2, VIRTUAL_WIDTH/2, VIRTUAL_HEIGHT/2) # virtual coordinates
yertle = turtle.Turtle()
yertle.circle(100)
turtle.mainloop()
告诉我们以上内容对您有何影响。
我正在使用 Python 2.7。我只能使用 turtle 模块来创建游戏。我的 1920 x 1080 显示器 完美缩放所有图像并且动画播放正常,但是 (例如) 在 1280 x 720 显示,图像未正确缩放。我正在使用 turtle.shape()
作为图像。我该如何解决这个问题?
我添加了一张图片
和 video 进一步解释问题。
如您所见,图标重叠,图像向后和第四弹跳,而不是直接前进。
这是我定义屏幕尺寸的方式:
screen_width = ctypes.windll.user32.GetSystemMetrics(0) - ctypes.windll.user32.GetSystemMetrics(0) / 4
screen_height = ctypes.windll.user32.GetSystemMetrics(1) - ctypes.windll.user32.GetSystemMetrics(1) / 4
- 它采用显示大小,减去自身除以 4 以获得 window 的最终显示。
如果没有您的代码,我无法在我的环境中对此进行全面测试。这个想法是使用虚拟坐标,以便在统一处理各个图形元素的级别上进行缩放:
import turtle
VIRTUAL_ASPECT_RATIO = 1.8
VIRTUAL_WIDTH = 2000
VIRTUAL_HEIGHT = VIRTUAL_WIDTH / VIRTUAL_ASPECT_RATIO
screen_width = 3 * ctypes.windll.user32.GetSystemMetrics(0) / 4
screen_height = 3 * ctypes.windll.user32.GetSystemMetrics(1) / 4
# attempt to adjust the real window to match the virtual aspect ratio
if screen_width / VIRTUAL_ASPECT_RATIO < screen_height:
screen_height = screen_width / VIRTUAL_ASPECT_RATIO
elif screen_height * VIRTUAL_ASPECT_RATIO < screen_width:
screen_width = screen_height * VIRTUAL_ASPECT_RATIO
else:
pass # probably need to do something where we adjust both
screen = turtle.Screen()
screen.setup(screen_width, screen_height) # size the real window
screen.setworldcoordinates(-VIRTUAL_WIDTH/2, -VIRTUAL_HEIGHT/2, VIRTUAL_WIDTH/2, VIRTUAL_HEIGHT/2) # virtual coordinates
yertle = turtle.Turtle()
yertle.circle(100)
turtle.mainloop()
告诉我们以上内容对您有何影响。