pygame窗口无法保持全屏模式

5 投票
2 回答
2030 浏览
提问于 2025-04-18 06:47

我正在用pygame模块制作一个游戏,现在遇到了一个问题。程序本身运行得很好,但我想开启的全屏模式却不行。我做了一个测试程序,全屏模式运行得非常完美,但当我尝试把游戏设置为全屏时,显示效果就很奇怪了。
首先,程序启动时,你会看到它进入全屏模式,并显示一段文字:“加载中……”。然后,窗口消失了,又以原来的非全屏大小重新出现。屏幕底部的资源管理器栏显示了两次,然后第二个资源管理器栏又消失了。最后,游戏还是在非全屏模式下运行。
这是我使用的程序:

import pygame, sys, os
from pygame.locals import *

pygame.mixer.pre_init(44100, -16, 4, 2048)
pygame.init()

DISPLAYSURF = pygame.display.set_mode((476, 506), FULLSCREEN)

pygame.display.set_caption('Title of the game')

DISPLAYSURF.fill((128,128,128))
FONT = pygame.font.Font('freesansbold.ttf',20)
LoadingText = FONT.render('Loading...', True, (0,255,0))
LoadingRect = LoadingText.get_rect()
LoadingRect.center = (238,253)
DISPLAYSURF.blit(LoadingText, LoadingRect)
pygame.display.update()


# These files will be created when needed. They are now removed to prevent interference later.
try:
    os.remove('LOAD.txt')
except IOError:
    pass
try:
    os.remove('OPEN.txt')
except IOError:
    pass
try:
    os.remove('RUN.txt')
except IOError:
    pass
try:
    os.remove('TEMP.txt')
except IOError:
    pass

# All parts of the program are split into small programs that are callable with a main function
import ROIM
import ROIM_CreateNewGame
import ROIM_LevelMenu
import ROIM_Menu
import ROIM_SmallMenus
import ROIM_GameIntroduction
import SetupController


# RUN.txt is a file that says wich program to run
Run = 'Menu'
RUN = open('RUN.txt','w')
RUN.write('RUN\n')
RUN.write(Run)
RUN.close()

ChangeRun = False

FPS = 35
fpsClock = pygame.time.Clock()

while True: # MAIN GAME LOOP

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    Preferences = open('Preferences.txt')
    PreferencesLines = Preferences.read().split('\n')
    x = 1
    Volume = -1
    Brightness = -1
    for Item in PreferencesLines:
        if Item == 'BRIGHTNESS':
            Brightness = int(PreferencesLines[x])
        if Item == 'VOLUME':
            Volume = int(PreferencesLines[x])
        x += 1
    Preferences.close()
    assert Volume != -1
    assert Brightness != -1

    # The colors will be changed to the right brightness.
    GREEN = (0,255 * (Brightness / 100),0)
    YELLOW = (255 * (Brightness / 100),255 * (Brightness / 100),0)
    RED = (255 * (Brightness / 100),0,0)
    BLUE = (0,0,255 * (Brightness / 100))
    WHITE = (255 * (Brightness / 100),255 * (Brightness / 100),255 * (Brightness / 100))
    BLACK = (0,0,0)
    GREY = (128 * (Brightness / 100),128 * (Brightness / 100),128 * (Brightness / 100))


    # Every small program gets the main variables and constants as arguments 
    if Run == 'Menu':
        ROIM_Menu.RunMenu(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'NewGame':
        ROIM_CreateNewGame.RunNewGame(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'Game':
        ROIM.RunGame(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'SmallMenu':
        ROIM_SmallMenus.RunSmallMenu(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'LevelMenu':
        ROIM_LevelMenu.RunLevelMenu(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'Introduction':
        ROIM_GameIntroduction.RunIntro(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    elif Run == 'Setup':
        SetupController.Run_Controller_Setup(FONT, ChangeRun, GREEN, YELLOW, RED, BLUE, WHITE, BLACK, GREY, DISPLAYSURF, Volume, Brightness, fpsClock, FPS)
    else:
        assert False
    # Every program edits the RUN file before finishing
    ChangeRun = False
    RUN = open('RUN.txt')
    assert RUN.readline() == 'RUN\n'
    Run = RUN.readline().split('\n')[0]
    RUN.close()

游戏运行得很好,但就是不能全屏。DISPLAYSURF在程序中没有被修改。这意味着我没有调用 pygame.display.set_mode()。我用的是Windows 8和Python 3.4。
这会不会是因为我把窗口对象作为参数传递了?我完全不知道我哪里做错了。

2 个回答

2

你可能需要在显示表面的 .set_mode() 函数中传入一些额外的参数。下面的代码在我使用Windows 7时是有效的:

DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), FULLSCREEN | HWSURFACE | DOUBLEBUF)
3

我发现问题出在子程序上。在每个导入的程序里,你都需要导入pygame库,但我以为还需要再次初始化pygame,其实并不需要。我把每个子程序里的pygame.init()去掉了,现在一切都正常了。

撰写回答