Pygame 显示位置 [英] Pygame Display Position

查看:127
本文介绍了Pygame 显示位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建 pygame 窗口后立即需要窗口位置:

I need the window position right after I created a pygame window:

window = pygame.display.set_mode((width, height), 0, 32)
pygame.init()

默认情况下,窗口从 0,0 开始 - 但如果用户更改窗口位置,我也需要 x,y.有什么想法吗?

By default, the window starts at 0,0 - but I also need x,y if the user changes the window position. Any ideas?

推荐答案

我需要 pygame 窗口的 x,y 坐标 - 无论是在开始时还是在窗口移动时.最后一个很不错.

I need x,y coords of the pygame window - either at start or on window move. The last one is nice to have.

我想出了如何使 pygame 窗口在屏幕底部居中:

I figured out how to center the pygame window at the bottom of the screen:

    pos_x = screen_width / 2 - window_width / 2
    pos_y = screen_height - window_height
    os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y)
    os.environ['SDL_VIDEO_CENTERED'] = '0'

背景:我有与屏幕相关的 x,y 坐标,我必须将屏幕坐标转换为窗口本地坐标,以便我可以使用它们,例如在 pygame 窗口内显示坐标或丢弃 pygame 窗口外的坐标.

Background: I have x,y coords which are screen related and I must convert the screen coords into window-local coords so that I can use them e.g. to display coords inside the pygame window or to discard coords which are outside the pygame window.

通过我上面的方法,我知道初始位置.但是我只能使用一个 pygame 窗口(因为它总是在相同的位置)并且如果用户移动窗口就会出错.

With my approach above, I knwo the initial position. But I can only use a single pygame window (because it's always at the same position) and things go wrong if the user moves the window.

这篇关于Pygame 显示位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆