Brian
Brian

Reputation: 279

Pygame display.info giving wrong resolution size

I'm building a small game with pygame. I want the window of the game to be size of the monitors resolution. My computers screen's resolution is 1920x1080 and display.info says the window size is also 1920x1080 but when I run it, it creates a window roughly one and half times the size of my screen.

import pygame, sys

def main():
    #set up pygame, main clock
    pygame.init()
    clock = pygame.time.Clock()

    #creates an object with the computers display information
    #current_h, current_w gives the monitors height and width
    displayInfo = pygame.display.Info()

    #set up the window
    windowWidth = displayInfo.current_w
    windowHeight = displayInfo.current_h
    window = pygame.display.set_mode ((windowWidth, windowHeight), 0, 32)
    pygame.display.set_caption('game')

    #gameLoop
    while True:
        window.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        #draw the window onto the screen
        pygame.display.flip()
        clock.tick(60)

main()

Upvotes: 5

Views: 3860

Answers (1)

Flutterguy135
Flutterguy135

Reputation: 478

I've been having the same problem, and I managed to find the answer and posted it here. The answer I found is as follows:



I managed to find a commit on the Pygame BitBucket page here that explains the issue and gives an example on how to fix it.

What is happening is that some display environments can be configured to stretch windows so they don't look small on high PPI (Pixels Per Inch) displays. This stretching is what causes displays on larger resolutions to display larger than they actually are.

They provide an example code on the page I linked to showing how to fix this issue.

They fix the issue by importing ctypes and calling this:

ctypes.windll.user32.SetProcessDPIAware()

They also express that this is a Windows only solution and is available within base Python since Python 2.4. Before that it will need to be installed.

With that said, to make this work, put this bit of code anywhere before pygame.display.set_mode()

import ctypes
ctypes.windll.user32.SetProcessDPIAware()
#
# # # Anywhere Before
#
pygame.display.set_mode(resolution)


I hope this helps you and anyone else who finds they have this same issue.

Upvotes: 9

Related Questions