获取Pygame中的屏幕分辨率

huangapple go评论78阅读模式
英文:

Get the screen resolution in Pygame

问题

我有一个pygame程序,我想要它全屏显示,可以使用以下代码:

pygame.display.set_mode((width, height), pygame.FULLSCREEN)

我不知道计算机的宽度和高度(分辨率),所以我想要能够在macOS和Windows上获取它,可以使用类似以下的代码:

import platform
pf = platform.platform().lower()
if pf.startswith("windows"):
    # 在这里添加代码
else:
    # 在这里添加代码

我尝试了这个:

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480), pygame.FULLSCREEN)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()
英文:

I have a pygame program, and I want it to be fullscreen using
pygame.display.set_mode((width, height), pygame.FULLSCREEN).

I do not know the width and height (resolution) of the computer, so I want to be able to retrieve it on both macOS and Windows, using something like this for example:

import platform
pf = platform.platform().lower()
if pf.startswith("windows"):
    # code here
else:
    # code here

I tried this:

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480), pygame.FULLSCREEN)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

答案1

得分: 2

set_mode可以接受宽度和高度都为零以强制使用本机分辨率。所以你可以这样做:

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

要了解更多信息,请查看这里的文档:http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

英文:

The set_mode can take a width and height of zero to force native resolution. So you can just do:

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

Take a look at the docs here for more information: http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

huangapple
  • 本文由 发表于 2023年7月3日 09:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定