切换到 Pygame 全屏模式仅工作一次

发布于 2025-01-09 12:26:51 字数 1076 浏览 0 评论 0原文

我是 pygame.FULLSCREEN 模式的新手,我需要你的帮助。 我正在编写一个小游戏,其中玩家应该能够在正常模式和全屏模式之间切换。

当我运行程序时,窗口会打开,当我单击最大化按钮时,我会进入全屏模式。当我按退出键时,我回到正常模式。到目前为止一切正常。

但是,当我第二次单击最大化按钮时,窗口被最大化,但是,我没有进入全屏模式。此外,pygame 使用的窗口部分保持正常大小。

这是我的代码:

import pygame

pygame.init()

width = 500
height = 500

info = pygame.display.Info()
screen_width = info.current_w
screen_height = info.current_h

window = pygame.display.set_mode((width, height), pygame.RESIZABLE)

fullscreen = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.VIDEORESIZE:
            window = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)
            fullscreen = True

        keys = pygame.key.get_pressed()

        if keys[pygame.K_ESCAPE] and fullscreen:
            window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
            fullscreen = False

    window.fill((255, 255, 255))
    pygame.display.update()

提前致谢!

I'm new to the pygame.FULLSCREEN mode, and I need your help.
I'm writing a small game, in which the player is supposed to be able to switch between normal mode and full screen mode.

When I run my program is that the window opens, and when I click on the maximize button I get into full screen mode. When I press escape, I get back to normal mode. Everything's working fine so far.

But when I click the maximize button a second time the window is maximized, however, I do not get into full screen mode. Also, the part of the window that pygame uses remains normal size.

Here's my code:

import pygame

pygame.init()

width = 500
height = 500

info = pygame.display.Info()
screen_width = info.current_w
screen_height = info.current_h

window = pygame.display.set_mode((width, height), pygame.RESIZABLE)

fullscreen = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.VIDEORESIZE:
            window = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)
            fullscreen = True

        keys = pygame.key.get_pressed()

        if keys[pygame.K_ESCAPE] and fullscreen:
            window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
            fullscreen = False

    window.fill((255, 255, 255))
    pygame.display.update()

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-16 12:26:51

每次调整窗口大小时都会执行 pygame.VIDEORESIZE 。因此,即使窗口更改为较小的尺寸,也会发生pygame.VIDEORESIZE
通过 event.set 获取窗口的新大小,并根据当前状态创建新的 pygame.FULLSCREENpygame.FULLSCREEN 显示
全屏。但可以通过按键设置全屏,例如fpygame.event.post() 一个新的 pygame.event.Event () 与正确的论点:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        elif event.type == pygame.VIDEORESIZE:
            type = pygame.FULLSCREEN if fullscreen else pygame.RESIZABLE
            window = pygame.display.set_mode(event.size, type)

        elif event.type == pygame.KEYDOWN:
            if not fullscreen and event.key == pygame.K_f:
                fullscreen = True
                pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (screen_width, screen_height)))
            if fullscreen and event.key == pygame.K_ESCAPE:
                fullscreen = False
                pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (width, height)))

    window.fill((255, 255, 255))
    pygame.display.update()

The pygame.VIDEORESIZE is executed every time when the window is resized. Hence even if the window is changed to a smaller size, then pygame.VIDEORESIZE occurs.
Get the new size of the window by event.set and create a new pygame.FULLSCREEN or pygame.FULLSCREEN display dependent on the current state
of fullscreen. But set fullscreen by a key, for instance f. pygame.event.post() a new pygame.event.Event() with the proper arguments:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        elif event.type == pygame.VIDEORESIZE:
            type = pygame.FULLSCREEN if fullscreen else pygame.RESIZABLE
            window = pygame.display.set_mode(event.size, type)

        elif event.type == pygame.KEYDOWN:
            if not fullscreen and event.key == pygame.K_f:
                fullscreen = True
                pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (screen_width, screen_height)))
            if fullscreen and event.key == pygame.K_ESCAPE:
                fullscreen = False
                pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (width, height)))

    window.fill((255, 255, 255))
    pygame.display.update()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文