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. Mar 23, 2021 Once we make a change to a sprite’s position in the Pygame window, many people call the pygame.display.flip or the pygame.display.update function. The problem here is that you are refreshing the entire window for every change you make. This has a significant hit on performance. In short, you need to do two things. I'm guessing because pygame is hardware accelerated there are issues running the display on a remote desktop, also because tight vnc creates a separate desktop sessions it seems to me. I've tried initialized the pygame display with pygame.SWSURFACE but I still get the MIT-MAGIC-COOKIE-1 error.
Pygame is a multimedia library for Python for making gamesand multimedia applications.
It is a wrapper around the SDL (Simple DirectMedia Layer) library.In this section we indroduce the basics of pygame functions without defining classes and objects.
Import the module¶
To use the methods in the Pygame library, the module must first be imported:
The import statement writes the pygame version and a link to thePygame website to the console (as a side effect):
The Pygame import statement is always placed at the beginning of the program.It imports the pygame classes, methods and attributes into the current name space.Now this new methods can be called via pygame.method()
.
For exemple we can now initialize or quit pygame with the following command:
The function display.set_mode()
sets the screen size. It returnsa Surface
object wich we assign to the variable screen
.This variable will be one of the most used variables.It represents the window we see:
You can now run this program and test it. At this moment it does very little.It opens a window and closes it immediately.
Show the event loop¶
The most essential part of any interactive application is the event loop.Reacting to events allows the user to interact with the application.Events are the things that can happen in a program, such as a
- mouse click,
- mouse movement,
- keyboard press,
- joystick action.
The following is an infinite loop which prints all events to the console:
Try to move the mouse, click a mouse button, or type something on the keyboard.Every action you do produces an event which will be printed on the console.This will look something like this:
As we are in an infite loop, it is impossible to quit this program from within the application.In order to quit the program, make the console the active window and type ctrl-C
.This will write the following message to the console:
Quit the event loop properly¶
In order to quit the application properly, from within the application,by using the window close button (QUIT event), we modify the event loop.First we introduce the boolean variable running
and set itto True
. Within the event loop we check for the QUIT event.If it occurs, we set running
to False
:
Once the event loop, we call the pygame.quit()
function to end the applicationcorrectly.
Define colors¶
Colors are defined as tuples of the base colors red, green and blue.This is called the RGB model.Each base color is represented as a number between 0 (minimum) and 255 (maximum)which occupies 1 byte in memory. An RGB color is thus represented as a 3-byte value.Mixing two or more colors results in new colors.A total of 16 million different colors can be represented this way.
Let’s define the base colors as tuples of the tree base values.Since colors are constants, we will write them using capitals.The absence of all colors results in black.The maximum value for all three components results in white.Three identical intermediate values result in gray:
The tree base colors are defined as:
By mixing two base colors we obtained more colors:
At the end of the event loop, we add the following:
The method fill(color)
fills the whole screen with the specified color.At this point nothing will be displayed. In order to show anything, the functionpygame.display.update()
must be called.
Switch the background color¶
Pygame Window Closing Automatically Download
At the beginning of the program we add a new veriable background
and initialize it to gray:
Within the event loop we are looking now for KEYDOWN
events.If found, we check if the R or G keys have been pressed and change thebackground color to red (R) and green (G). This is the code added in the event loop:
In the drawing section we use now the variable background
representing thebackground color:
Test the program.Pressing the R and G keys allows you to switch the background color.
Import pygame.locals¶
The pygame.locals
module contains some 280 constants used and defined by pygme.Placing this statement at the beginning of your programm imports them all:
We find the key modifiers (alt, ctrl, cmd, etc.)
the number keys:
the special character keys:
the letter keys of the alphabet:
Instead of writing pygame.KEYDOWN
we can now just write KEYDOWN
.
Use a dictionary to decode keys¶
The easiest way to decode many keys, is to use a dictionary.Instead of defining many if-else cases, we just create a dictionary with the keyboard key entries.In this exemple we want to associate 8 different keys with 8 different background colors.At the beginning of the programm we define this key-color dictionary:
Printing the dictionary to the console gives this result:
The keys are presented here with their ASCII code. For exaple the ASCII code fork
is 107. Colors are represented as tuples. The color black is represented as (0, 0, 0).
Pygame Window Closing Automatically Lock
The event loop now becomes very simple.First we check if the event type is a KEYDOWN event.If yes, we check if the event key is in the dictionary.If yes, we look up the color which is associated with that keyand set the background color to it:
Try to press the 8 specified keys to change the background color.
Change the window caption¶
The fonction pygame.display.set_caption(title)
allows to change the caption (title)of the application window. We can add this to the event loop:
This will display the RGB value of the current background color in the window caption.
Explore a simple ball game¶
To show what Pygame can do, here is a simple programwhich demonstrates a bouncing ball animation.The program uses the Rect
class to represent a rectangular region.An instance is created from the ball image:
A Rect
object has 4 attributes:
A Rect
object can be moved with the move()
method:
After importing the pygame module, we define a few variablessuch as screen size and two colors:
Then we initialize pygame and create the screen
variable:
The ball position is represented with a Rect
object:
Inside the event loop we only check for the QUIT
event:
Then we move the rectangle and check the left/right and top/bottom borders:
Finaly we draw a green background, a red rectangle and the ball image:
This is what the ball and the Rect
outline looks:
Try to understand what the program does.Then try to modify it’s parameters.