0

So, I've looked all over the internet and I've seen a few things that state

window_width, window_height = pygame.display.get_surface().get_size()

is possible but when I do it I get an error message (window_width = pygame.display.get_surface().get_size() AttributeError: 'NoneType' object has no attribute 'get_size') I've seen it's possible here and well as other sites so why won't it work for me?

import pygame 
import random
window_width, window_height = pygame.display.get_surface().get_size()
black = (0,0,0)
close_program = False
fullscreen = False
white = (255,255,255)
random_position_x = random.randint(0, window_width)
random_position_y = random.randint(0, window_height)

1 Answer 1

0

You can use pygame.display.Info():

The docs say:

current_h, current_w: Height and width of the current video mode, or of the desktop mode if called before the display.set_mode is called. (current_h, current_w are available since SDL 1.2.10, and pygame 1.8.0) They are -1 on error, or if an old SDL is being used.1.8.0)

pygame.display.Info() creates an Info Object with the attributes current_h and current_w. Create the Info Object before you call display.set_mode and then call display.set_mode with current_h and current_w from the object.

Example 1:

infoObject = pygame.display.Info()
pygame.display.set_mode((infoObject.current_w, infoObject.current_h))

Example 2:

width, height = pygame.display.Info().current_w, pygame.display.Info().current_h
1
  • it works which is great but I can divide it but I'll look around and see what i can come up with. Just when I'm done i can exit out
    – user12477652
    Jul 28, 2021 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy