Hey,
First: Hello, i'm new here :lol:
And here my question:
It's a very short question: Is it possible to get the screen size (resolution) with pygame and how do i get it?

Recommended Answers

All 6 Replies

Just an untested WAG ...

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640,480), FULLSCREEN)
# get the size of the fullscreen display
x, y = screen.get_size()

There is also a pygame.display.Info() call you can print.

How do you can the size of the window when you change it's size with the RESIZABLE flag? The size never updates to match the window. How can I get the window size by any other way?

#!/usr/bin/env python2.3
#
#  main.py
#
#Main python script for the game  
#
#  Created by Matthew Mitchell on 13/09/2009.
#  Copyright (c) 2009 Matthew Mitchell. All rights reserved.
#
#Import modules
import wx #Use wxpython for Window management
import sys
import os
import pygame
from pygame.locals import *
import threading
#wxpython window thread
class WxThread(threading.Thread):
	def run(self):
		app = wx.PySimpleApp()
		self.window = wx.Frame(None, wx.ID_ANY, "TimeSplitters Platinum")
		self.window.SetClientSize((400,400))
		self.window.Show(1)
		self.window.Bind(wx.EVT_IDLE, self.winid)
		app.MainLoop()
	def winid(self,event):
		if window_flag == False:
			winid = self.window.GetHandle()
			if sys.platform == "win32":
				os.environ['SDL_VIDEODRIVER'] = 'windib'
			os.environ['SDL_WINDOWID'] = str(winid)
			global window_flag
			window_flag = True
#Game loop
def game_loop():
	global screen,fs,bp
	for event in pygame.event.get():
			if event.type == QUIT:
				quit()
	keys = pygame.key.get_pressed() #Get the pressed keys
	if pygame.key.get_mods() == 1024 and (keys[K_q] or keys[K_w]):
		quit()
	if pygame.key.get_mods() == 1024 and keys[K_f]:
		if fs == False:
			screen = pygame.display.set_mode((0,0), FULLSCREEN)
			fs = True
		else:
			screen = pygame.display.set_mode((width,height - 120),RESIZABLE)
			fs = False
	if keys[K_UP] and (bp[1] > 0):
		bp[1] -= 3
	if keys[K_DOWN] and (bp[1] +25< game_size[1]):
		bp[1] += 3
	if keys[K_LEFT] and (bp[0] > 0):
		bp[0] -= 3
	if keys[K_RIGHT] and (bp[0] +25 < game_size[0]):
		bp[0] += 3
	#Scale game to screen resolution, keeping aspect ratio
	screen = pygame.display.get_surface() #Get updated screen - NOT WORKING :(
	ss = screen.get_size()
	gap = float(16)/float(9)
	sap = float(ss[0]) / float(ss[1])
	if gap > sap:
		#Game aspect ratio is greater than screen (wider) so scale width
		factor = float(game_size[0]) /float(ss[0])
		new_h = game_size[1]/factor #Divides the height by the factor which the width changes so the aspect ratio remians the same.
		screen.blit(pygame.transform.scale(game,(ss[0],new_h)),(0,(ss[1] - new_h)/2)) 
	elif gap < sap:
		#Game aspect ratio is less than the screens.
		factor = float(game_size[1]) /float(ss[1])
		new_w = game_size[0]/factor #Divides the width by the factor which the height changes so the aspect ratio remians the same.
		screen.blit(pygame.transform.scale(game,(new_w,ss[1])),((ss[0] - new_w)/2,0)) 
	else:
		screen.blit(pygame.transform.scale(game,(screen.get_size())),(0,0))
	game.fill((255,255,255)) #Refresh game with black
	game.blit(bottom,bp)
	pygame.display.flip()
	clock.tick(60)
#Run if being run directly and not as a module
if __name__ == '__main__':
	#Sets the window to be handled by wxpython
	window_flag = False
	WxThread().start()
	#Waits for thread to set window ID
	while(window_flag == False):
		pass
	#Initialises pygame
	pygame.init()
	screen_info = pygame.display.Info() #Required to set a good resolution for the game screen
	height,width = screen_info.current_h, screen_info.current_w
	screen = pygame.display.set_mode((width,height - 120),RESIZABLE) #Take 120 pixels from the height because the menu bar, window bar and dock takes space
	fs = False #Fullscreen false to start
	game_size = [1280,720]  #1280x720 HD resolution
	#window.SetSize(game_size)
	game = pygame.Surface(game_size) #The game resolution is fixed and will be scaled to the actual screen resoultion
	game = game.convert()
	bottom = pygame.Surface((25,25))
	bottom = bottom.convert()
	bottom.fill((0,80,150))
	bp = [0,0]
	clock = pygame.time.Clock()
	while 1:
		game_loop()

Integrating wxpython doesn't work.

Thank you. However, I figured it out myself eventually and made a library for it. The code keeps the aspect ratio constant:

#!/usr/bin/env python2.3
#
#  Automatic Game Scaling Library for pygame
#
#  Allows resize of a Window while scaling the game, keeping the aspect ratio.
#
#  Created by Matthew Mitchell on 13/09/2009.
#  Copyright (c) 2009 Matthew Mitchell. All rights reserved.
#
#Import modules
import sys
import pygame
from pygame.locals import *
def get_resolution(screen,ss,gs): 
	gap = float(gs[0]) / float(gs[1])
	sap = float(ss[0]) / float(ss[1])
	if gap > sap:
		#Game aspect ratio is greater than screen (wider) so scale width
		factor = float(gs[0]) /float(ss[0])
		new_h = gs[1]/factor #Divides the height by the factor which the width changes so the aspect ratio remians the same.
		game_scaled = (ss[0],new_h)
	elif gap < sap:
		#Game aspect ratio is less than the screens.
		factor = float(gs[1]) /float(ss[1])
		new_w = gs[0]/factor #Divides the width by the factor which the height changes so the aspect ratio remians the same.
		game_scaled = (new_w,ss[1])
	else:
		game_scaled = screen.get_size()
	return game_scaled		
class ScaledGame(pygame.Surface):
	game_size = None
	first_screen = None
	screen = None
	fs = False #Fullscreen false to start
	clock = None
	resize = True
	game_gap = None
	game_scaled = None
	title = None
	fps = False
	def __init__(self,title,game_size):
		pygame.init()
		self.title = title
		self.game_size = game_size
		screen_info = pygame.display.Info() #Required to set a good resolution for the game screen
		self.first_screen = (screen_info.current_w, screen_info.current_h - 120) #Take 120 pixels from the height because the menu bar, window bar and dock takes space
		self.screen = pygame.display.set_mode(self.first_screen,RESIZABLE) 
		pygame.display.set_caption(self.title)
		pygame.Surface.__init__(self,self.game_size) #Sets up the Surface for the game.
		self.clock = pygame.time.Clock()
		self.game_gap = (0,0)
	def update(self):
		#Updates screen properly
		win_size_done = False #Changes to True if the window size is got by the VIDEORESIZE event below
		for event in pygame.event.get():
			if event.type == QUIT:
				sys.exit()
			if event.type == VIDEORESIZE:
				ss = [event.w,event.h]
				self.resize = True
				win_size_done = True
		keys = pygame.key.get_pressed() #Get the pressed keys
		if pygame.key.get_mods() == 1024:
			if(keys[K_q] or keys[K_w]):
				sys.exit()
			if keys[K_f]:
				self.screen = pygame.display.set_mode(self.first_screen,RESIZABLE)
				if self.fs == False:
					self.game_scaled = get_resolution(self.screen,[self.screen.get_width(),self.screen.get_height()],self.game_size)
					self.game_gap = [(self.screen.get_width() - self.game_scaled[0])/2,(self.screen.get_height() - self.game_scaled[1])/2]
					self.screen = pygame.display.set_mode((0,0), FULLSCREEN | HWSURFACE  | DOUBLEBUF)
					self.fs = True
				else:
					self.fs = False
					self.resize = True
					self.game_gap = (0,0)
		#Scale game to screen resolution, keeping aspect ratio
		if self.resize == True:
			if(win_size_done == False): #Sizes not gotten by resize event
				ss = [self.screen.get_width(),self.screen.get_height()]
			self.game_scaled = get_resolution(self.screen,ss,self.game_size)
			self.screen = pygame.display.set_mode(self.game_scaled,RESIZABLE)
		self.resize = False #Next time do not scale unless resize or fullscreen events occur
		self.screen.blit(pygame.transform.scale(self,self.game_scaled),self.game_gap) #Add game to screen with the scaled size and gap required.
		pygame.display.flip()
		self.clock.tick(60)
		if self.fps == True:
			pygame.display.set_caption(self.title + " - " + str(int(self.clock.get_fps())) + "fps")

An example of the library in use (scalelib is the name of the library script obviously):

#!/usr/bin/env python2.3
#
#  main.py
#  
#
#  Created by Matthew Mitchell on 15/09/2009.
#  Copyright (c) 2009 Matthew Mitchell. All rights reserved.
#
from scalelib import *
if __name__ == '__main__': #Run if being run directly and not as a module
	game = ScaledGame("Test",[1280,720])  #1280x720 HD resolution game. Creates an object with the game Surface
	bottom = pygame.Surface((50,50))
	bottom.fill((0,80,180))
	bp = [0,0]
	while 1: #Game loop
		keys = pygame.key.get_pressed()
		if keys[K_UP] and (bp[1] > 0):
			bp[1] -= 3
		if keys[K_DOWN] and (bp[1] +50< game.get_height()):
			bp[1] += 3
		if keys[K_LEFT] and (bp[0] > 0):
			bp[0] -= 3
		if keys[K_RIGHT] and (bp[0] +50 < game.get_width()):
			bp[0] += 3
		game.fill((255,255,255)) #Refresh game with white
		game.blit(bottom,bp)
		game.fps = True #Shows framerate
		game.update() #Updates game
Member Avatar for diliupgabadamudalige

This code has been answered. But it has errors!
line 85, in update
self.screen = pygame.display.set_mode(self.game_scaled,RESIZABLE)
TypeError: integer argument expected, got float

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.