Download here: http://gg.gg/wb7f5/1
pygame module to control the display window and screen
Apr 07, 2020 The resizing thing I tested also on pygame 1.9.4 and 1.9.5 and it’s the same. What I wish is to draw on the window when it is resized to a larger size, but I can’t seem to do it. I also saw people doing this: if event.type pygame.VIDEORESIZE: window = pygame.display.setmode((event.w, event.h), pygame.RESIZABLE). 3.6: The QUIT Event and pygame.quit Function, 05, DISPLAYSURF = pygame.display.setmode(( 400, 300 )) Normally it doesn’t really matter since Python closes it when the program exits Could someone help me out with closing a pygame window? I’m following along with a book I’m reading and one of the examples involves the module pygame. Dec 21, 2020 pygame.init # Here init stands for initialization of the package. This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. Creating windows screen. I always use file explorer (used to be windows explorer). Anyhow, after opening the file explorer, it closes by itself after 30 minutes or so. Any solution of disabling it? I have Windows 10 Pro.pygame.display.init—initialize the display modulepygame.display.quit—uninitialize the display modulepygame.display.get_init—true if the display module is initializedpygame.display.set_mode—initialize a window or screen for displaypygame.display.get_surface—get a reference to the currently set display surfacepygame.display.flip—update the full display Surface to the screenpygame.display.update—update portions of the screen for software displayspygame.display.get_driver—get the name of the pygame display backendpygame.display.Info—Create a video display information objectpygame.display.get_wm_info—Get information about the current windowing systempygame.display.list_modes—get list of available fullscreen modespygame.display.mode_ok—pick the best color depth for a display modepygame.display.gl_get_attribute—get the value for an opengl flag for the current displaypygame.display.gl_set_attribute—request an opengl display attribute for the display modepygame.display.get_active—true when the display is active on the displaypygame.display.iconify—iconify the display surfacepygame.display.toggle_fullscreen—switch between fullscreen and windowed displayspygame.display.set_gamma—change the hardware gamma rampspygame.display.set_gamma_ramp—change the hardware gamma ramps with a custom lookuppygame.display.set_icon—change the system image for the display windowpygame.display.set_caption—set the current window captionpygame.display.get_caption—get the current window captionpygame.display.set_palette—set the display color palette for indexed displays
This module offers control over the pygame display. Pygame has a single displaySurface that is either contained in a window or runs full screen. Once youcreate the display you treat it as a regular Surface. Changes are notimmediately visible onscreen, you must choose one of the two flipping functionsto update the actual display.
The origin of the display, where x = 0, and y = 0 is the top left of thescreen. Both axis increase positively towards the botom right of the screen.
The pygame display can actually be initialized in one of several modes. Bydefault the display is a basic software driven framebuffer. You can requestspecial modules like hardware acceleration and OpenGL support. These arecontrolled by flags passed to pygame.display.set_mode().
Pygame can only have a single display active at any time. Creating a new onewith pygame.display.set_mode() will close the previous display. If precisecontrol is needed over the pixel format or display resolutions, use thefunctions pygame.display.mode_ok(), pygame.display.list_modes(), andpygame.display.Info() to query information about the display.
Once the display Surface is created, the functions from this module effect thesingle existing display. The Surface becomes invalid if the module isuninitialized. If a new display mode is set, the existing Surface willautomatically switch to operate on the new display.
Then the display mode is set, several events are placed on the pygame eventqueue. pygame.QUIT is sent when the user has requested the program toshutdown. The window will receive pygame.ACTIVEEVENT events as the displaygains and loses input focus. If the display is set with thepygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when theuser adjusts the window dimensions. Hardware displays that draw direct to thescreen will get pygame.VIDEOEXPOSE events when portions of the window mustbe redrawn.pygame.display.init()¶init() -> None
Initializes the pygame display module. The display module cannot do anythinguntil it is initialized. This is usually handled for you automatically whenyou call the higher level pygame.init().
Pygame will select from one of several internal display backends when it isinitialized. The display mode will be chosen depending on the platform andpermissions of current user. Before the display module is initialized theenvironment variable SDL_VIDEODRIVER can be set to control which backendis used. The systems with multiple choices are listed here.
On some platforms it is possible to embed the pygame display into an alreadyexisting window. To do this, the environment variable SDL_WINDOWID mustbe set to a string containing the window id or handle. The environmentvariable is checked when the pygame display is initialized. Be aware thatthere can be many strange side effects when running in an embedded display.
It is harmless to call this more than once, repeated calls have no effect.pygame.display.quit()¶quit() -> None
This will shut down the entire display module. This means any activedisplays will be closed. This will also be handled automatically when theprogram exits.
It is harmless to call this more than once, repeated calls have no effect.pygame.display.get_init()¶get_init() -> bool
Returns True if the module is currently initialized.pygame.display.set_mode()¶set_mode(resolution=(0,0), flags=0, depth=0) -> Surface
This function will create a display Surface. The arguments passed in arerequests for a display type. The actual created display will be the bestpossible match supported by the system.
The resolution argument is a pair of numbers representing the width andheight. The flags argument is a collection of additional options. The depthargument represents the number of bits to use for color.
The Surface that gets returned can be drawn to like a regular Surface butchanges will eventually be seen on the monitor.
If no resolution is passed or is set to (0, 0) and pygame uses SDLversion 1.2.10 or above, the created Surface will have the same size as thecurrent screen resolution. If only the width or height are set to 0, theSurface will have the same width or height as the screen resolution. Using aSDL version prior to 1.2.10 will raise an exception.
It is usually best to not pass the depth argument. It will default to thebest and fastest color depth for the system. If your game requires aspecific color format you can control the depth with this argument. Pygamewill emulate an unavailable color depth which can be slow.
When requesting fullscreen display modes, sometimes an exact match for therequested resolution cannot be made. In these situations pygame will selectthe closest compatable match. The returned surface will still always matchthe requested resolution.
The flags argument controls which type of display you want. There areseveral to choose from, and you can even combine multiple types using thebitwise or operator, (the pipe “|” character). If you pass 0 or no flagsargument it will default to a software driven window. Here are the displayflags you will want to choose from:pygame.display.get_surface()¶get a reference to the currently set display surface
Return a reference to the currently set display Surface. If no display modehas been set this will return None.pygame.display.flip()¶flip() -> None
This will update the contents of the entire display. If your display mode isusing the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this willwait for a vertical retrace and swap the surfaces. If you are using adifferent type of display mode, it will simply update the entire contents ofthe surface.
When using an pygame.OPENGL display mode this will perform a gl bufferswap.pygame.display.update()¶update portions of the screen for software displaysupdate(rectangle_list) -> None
This function is like an optimized version of pygame.display.flip() forsoftware displays. It allows only a portion of the screen to updated,instead of the entire area. If no argument is passed it updates the entireSurface area like pygame.display.flip().
You can pass the function a single rectangle, or a sequence of rectangles.It is more efficient to pass many rectangles at once than to call updatemultiple times with single or a partial list of rectangles. If passing asequence of rectangles it is safe to include None values in the list, whichwill be skipped.
This call cannot be used on pygame.OPENGL displays and will generate anexception.pygame.display.get_driver()¶get_driver() -> name
Pygame chooses one of many available display backends when it isinitialized. This returns the internal name used for the display backend.This can be used to provide limited information about what displaycapabilities might be accelerated. See the SDL_VIDEODRIVER flags inpygame.display.set_mode() to see some of the common options.pygame.display.Info()¶Info() -> VideoInfo
Creates a simple object containing several attributes to describe thecurrent graphics environment. If this is called beforepygame.display.set_mode() some platforms can provide information aboutthe default display mode. This can also be called after setting the displaymode to verify specific display options were satisfied. The VidInfo objecthas several attributes:pygame.display.get_wm_info()¶Get information about the current windowing system
Creates a dictionary filled with string keys. The strings and values arearbitrarily created by the system. Some systems may have no information andan empty dictionary will be returned. Most platforms will return a “window”key with the value set to the system id for the current display.
New with pygame 1.7.1pygame.display.list_modes()¶list_modes(depth=0, flags=pygame.FULLSCREEN) -> list
This function returns a list of possible dimensions for a specified colordepth. The return value will be an empty list if no display modes areavailable with the given arguments. A return value of -1 means that anyrequested resolution should work (this is likely the case for windowedmodes). Mode sizes are sorted from biggest to smallest.
If depth is 0, SDL will choose the current/best color depth for thedisplay. The flags defaults to pygame.FULLSCREEN, but you may need toadd additional flags for specific fullscreen modes.pygame.display.mode_ok()¶mode_ok(size, flags=0, depth=0) -> depth
This function uses the same arguments as pygame.display.set_mode(). Itis used to depermine if a requested display mode is available. It willreturn 0 if the display mode cannot be set. Otherwise it will return a pixeldepth that best matches the display asked for.
Usually the depth argument is not passed, but some platforms can supportmultiple display depths. If passed it will hint to which depth is a bettermatch.
The most useful flags to pass will be pygame.HWSURFACE,pygame.DOUBLEBUF, and maybe pygame.FULLSCREEN. The function willreturn 0 if these display flags cannot be set.pygame.display.gl_get_attribute()¶get the value for an opengl flag for the current display
After calling pygame.display.set_mode() with the pygame.OPENGL flag,it is a good idea to check the value of any requested OpenGL attributes. Seepygame.display.gl_set_attribute() for a list of valid flags.pygame.display.gl_set_attribute()¶request an opengl display attribute for the display mode
When calling pygame.display.set_mode() with the pygame.OPENGL flag,Pygame automatically handles setting the OpenGL attributes like color anddoublebuffering. OpenGL offers several other attributes you may want controlover. Pass one of these attributes as the flag, and its appropriate value.This must be called before pygame.display.set_mode()
The OPENGL flags are;pygame.display.get_active()¶get_active() -> bool
After pygame.display.set_mode() is called the display Surface will bevisible on the screen. Most windowed displays can be hidden by the user. Ifthe display Surface is hidden or iconified this will return False.pygame.display.iconify()¶iconify() -> bool
Request the window for the display surface be iconified or hidden. Not allsystems and displays support an iconified display. The function will returnTrue if successfull.
When the display is iconified pygame.display.get_active() will returnFalse. The event queue should receive a ACTIVEEVENT event when thewindow has been iconified.pygame.display.toggle_fullscreen()¶toggle_fullscreen() -> bool
Switches the display window between windowed and fullscreen modes. Thisfunction only works under the unix x11 video driver. For most situations itis better to call pygame.display.set_mode() with new display flags.pygame.display.set_gamma()¶set_gamma(red, green=None, blue=None) -> bool
Set the red, green, and blue gamma values on the display hardware. If thegreen and blue arguments are not passed, they will both be the same as red.Not all systems and hardware support gamma ramps, if the function succeedsit will return True.
A gamma value of 1.0 creates a linear color table. Lower values will darkenthe display and higher values will brighten.pygame.display.set_gamma_ramp()¶change the hardware gamma ramps with a custom lookup
Set the red, green, and blue gamma ramps with an explicit lookup table. Eachargument should be sequence of 256 integers. The integers should rangebetween 0 and 0xffff. Not all systems and hardware support gamma ramps, ifthe function succeeds it will return True.pygame.display.set_icon()¶set_icon(Surface) -> None
Sets the runtime icon the system will use to represent the display window.All windows default to a simple pygame logo for the window icon.
You can pass any surface, but most systems want a smaller image around32x32. The image can have colorkey transparency which will be passed to thesystem.
Some systems do not allow the window icon to change after it has been shown.This function can be called before pygame.display.set_mode() to createthe icon before the display mode is set.pygame.display.set_caption()¶set_caption(title, icontitle=None) -> None
If the display has a window title, this function will change the name on thewindow. Some systems support an alternate shorter title to be used forminimized displays.pygame.display.get_caption()¶get_caption() -> (title, icontitle)
Returns the title and icontitle for the display Surface. These will often bethe same value.pygame.display.set_palette()¶set the display color palette for indexed displays
This will change the video display color palette for 8bit displays. Thisdoes not change the palette for the actual display Surface, only the palettethat is used to display the Surface. If no palette argument is passed, thesystem default palette will be restored. The palette is a sequence ofRGB triplets.
In this first tutorial, we’ll cover the basics and write some boilerplate code for Pygame - code you will almost always write whenever you use Pygame. We will:
*Create a Pygame window
*Stop the window from immediately disappearing
*Close the window in response to a quit event
*Change the title and background colour of the window
Note for Mac users: If you get a blank screen when using Pygame, take a look here to find a version of pygame that works.The Pygame window
Since we are going to use the Pygame module, the first thing we need to do is import it.
We can now create a Pygame window object (which I’ve called ’screen’) using pygame.display.set_mode(). This object requires two values that define the width and height of the window. Rather than use constants, we’ll define two variables, width and height, which will make the program easier to change later on. Feel free to use any integers that suit you. In order to display the window, we use the flip() function:
If you now run this program, you’ll see a 300 x 200 pixel window appear and then promptly disappear. The problem is that once as the flip() function has been called, the end of the code is reached, so the program ends.
To keep the screen visible for as long as we want, we need to make sure the program doesn’t end. We could do this by adding an infinite loop.
The problem with an infinite loop is that, by definition, it never ends. The program won’t quit even if we want it to. If we try to close the window by clicking on the X, nothing happens. You have to use Ctrl + C in the command line to force the program to quit.Closing the windowPygame Window Closing Automatically Lock
We want our window to persist until the user chooses to closes it. To achieve this, we monitor user inputs (known as ’events’) using pygame.event.get(). This function returns a list of events which we can loop through and check to see whether any have the type QUIT. If we find such an event, we exit our loop, which is best done by changing a boolean variable (which I’ve called ’running’).
The window now persists whilst ’running’ is equal to True, which it will be until you close the window (by clicking the X). Note that if you use an IDE for Python programming, then it may interfere with Pygame. This isn’t normally a major problem but it can stop the Pygame window from closing properly. If so, adding pygame.quit() should solve the problem (Thanks to nf3 in the comments for mentioning this).Pygame Window Closing Automatically ChangeChanging the window’s properties
Now we have a usable window, we can change its properties. For example, we can change its title using the set_caption() function.
We can change the background colour by filling the screen object. Colours are defined using a 3-tuple of integers from 0 to 255, for the red, green and blue values respectively. For example, white is (255,255,255). Changes need to be made before the flip() function is called.The final program
The complete program, after a bit of rearrangement, should now look like this:
You can also find the complete code for this tutorial by clicking the Code on Github link at the top of the article.Pygame Window Keeps Closing
Running the program should create a window that looks like this (on Windows XP).
It’s not very exciting at the moment, but now we have a window that persists until we close it. In the next tutorial we’ll draw some shapes in our window and start our simulation by creating a Particle object.
Download here: http://gg.gg/wb7f5/1

https://diarynote.indered.space

コメント

最新の日記 一覧

<<  2025年7月  >>
293012345
6789101112
13141516171819
20212223242526
272829303112

お気に入り日記の更新

テーマ別日記一覧

まだテーマがありません

この日記について

日記内を検索