Python pygame размер окна

Pygame — Get screen size?

Hey,
First: Hello, i’m new here 😆
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?

  • 4 Contributors
  • 6 Replies
  • 16K Views
  • 9 Years Discussion Span
  • Latest Post 7 Years Ago Latest Post by diliupgabadamudalige
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.

All 6 Replies

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()

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()

Источник

Читайте также:  File content reading in java

How do I get the size (width x height) of my pygame window

Later on in the app I want to be able to ask that window its width and height so I can decide how to process the mouse position. How do I access the dimensions of that pygame window elsewhere in the program? (event processing loop)

8 Answers 8

You want to get the surface, then get the size from the surface object.

w, h = pygame.display.get_surface().get_size()

You can get the windows size by first getting the display surface. pygame.display.get_surface(), then calling get_width() / get_height() on the surface.

surface = pygame.display.get_surface() #get the surface of the current active display x,y = size = surface.get_width(), surface.get_height()#create an array of surface.width and surface.height 

Adding another answer since I haven't seen this one yet.

screen = pygame.display.set_mode() # Default to screen resolution. area = screen.get_rect() 

We can call get_rect() on screen because it is a surface object. area will be a Rect object, with (0, 0, width, height) information in it.

import pygame as pg def create_window(width, height): """create a width x height resizable window/frame""" win = pg.display.set_mode((width, height), pg.RESIZABLE) # optional fill bg red, default is black win.fill((255, 0, 0)) # optional title info sf = "size x=%s y=%s" % (width, height) pg.display.set_caption(sf) # any extra code here . # draw a white circle white = (255, 255, 255) # center can be fixed or calculated for resize center = (150, 150) radius = 100 pg.draw.circle(win, white, center, radius) pg.display.flip() return win pg.init() width = 300 height = 200 win = create_window(width, height) # event loop and exit conditions (windows titlebar x click) while True: for event in pg.event.get(): if event.type == pg.VIDEORESIZE: width, height = event.size # redraw win in new size win = create_window(width, height) print(win.get_size()) # test if event.type == pg.QUIT: raise SystemExit 

Unfortunately I'm using the opengl library, so I have discovered that adjusting the size messes up the opengl context somehow. Eventually I'll dig around to figure out how I should cope with that.

Источник

Оцените статью