Python 如何获得我的 pygame 窗口的大小(宽 x 高)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36653519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:10:37  来源:igfitidea点击:

How do I get the size (width x height) of my pygame window

pythonpygame

提问by Mutant Bob

I have created a window using

我使用创建了一个窗口

pygame.display.set_mode((width, height), 
                        pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)

Later on in the app I want to be able to ask that window its width and height so I can decide how to process the mouse position.

稍后在应用程序中,我希望能够询问该窗口的宽度和高度,以便我可以决定如何处理鼠标位置。

How do I access the dimensions of that pygame window elsewhere in the program? (event processing loop)

我如何访问程序中其他地方的 pygame 窗口的尺寸?(事件处理循环)

回答by Will

You want to get the surface, then get the size from the surface object.

您要获取曲面,然后从曲面对象获取大小。

Using tuple unpacking:

使用元组解包:

w, h = pygame.display.get_surface().get_size()

w, h = pygame.display.get_surface().get_size()

回答by Jacob Persi

You can get the windows size by first getting the display surface. pygame.display.get_surface(), then calling get_width() / get_height() on the surface.

您可以通过首先获取显示表面来获取窗口大小。pygame.display.get_surface(),然后在surface上调用get_width() / get_height()。

回答by TheLazyScripter

Like this!

像这样!

surface = pygame.display.get_surface() #get the surface of the current active display
x,y = size = surface.get_width(), surface.get_height()#create an array of surface.width and surface.height

回答by Akash Vartak

import pygame as pg
def create_window(width, height):
    """create a width x height resizable window/frame"""
    win = pg.display.set_mode((width, height), pg.RESIZABLE)
    # optional fill bg red, default is black
    win.fill((255, 0, 0))
    # optional title info
    sf = "size x=%s  y=%s" % (width, height)
    pg.display.set_caption(sf)

    # any extra code here ...
    # draw a white circle
    white = (255, 255, 255)
    # center can be fixed or calculated for resize
    center = (150, 150)
    radius = 100
    pg.draw.circle(win, white, center, radius)

    pg.display.flip()
    return win
pg.init()
width = 300
height = 200
win = create_window(width, height)
# event loop and exit conditions (windows titlebar x click)
while True:
    for event in pg.event.get():
        if event.type == pg.VIDEORESIZE:
            width, height = event.size
            # redraw win in new size
            win = create_window(width, height)
            print(win.get_size())  # test
        if event.type == pg.QUIT:
            raise SystemExit

Try this out.

试试这个。

Hope it helps.

希望能帮助到你。

回答by David Jay Brady

Adding another answer since I haven't seen this one yet.

添加另一个答案,因为我还没有看到这个答案。

screen = pygame.display.set_mode() # Default to screen resolution.

area = screen.get_rect()

We can call get_rect()on screen because it is a surface object. areawill be a Rect object, with (0, 0, width, height) information in it.

我们可以get_rect()在屏幕上调用,因为它是一个表面对象。area将是一个 Rect 对象,其中包含 (0, 0, width, height) 信息。

回答by Jonathan Scott James

changed = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
        if event.type == pygame.VIDEORESIZE:
            scrsize = event.size  # or event.w, event.h
            screen = pygame.display.set_mode(scrsize,RESIZABLE)
            changed = True

it's almost exactly like this in the comments for pygame.display.init doc page https://www.pygame.org/docs/ref/display.html#comment_pygame_display_updateedited only slightly from 2011-01-25T23:10:35 - Dave Burton thanks dave

在 pygame.display.init 文档页面https://www.pygame.org/docs/ref/display.html#comment_pygame_display_update的评论中几乎完全像这样, 仅从 2011-01-25T23:10:35 开始稍微编辑 - Dave伯顿感谢戴夫

回答by samet kaya

gameDisplay = pygame.display.set_mode((800,600))

gameDisplay = pygame.display.set_mode((800,600))

x=gameDisplay.get_width()

x=gameDisplay.get_width()

y=gameDisplay.get_height()

y=gameDisplay.get_height()