Python Forum
[PyGame] Creating a window (part 1)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Creating a window (part 1)
#1
This tutorial starts a series of tutorials. This specific one handles opening a window, and modifications to that window. Feel free to ask questions on the forum on any confusing parts of this series.

import pygame as pg
pg.init()
Here we import pygame library and name it pg. The reason for pg is my preference for not typing out as many characters while also avoiding star imports. You can replace pg with pygame if you want. We initialize pygame with pg.init(). This will initialize all the pygame modules for you instead of doing one by one.

screen = pg.display.set_mode((800,600))
Here is the line that we actually create our window and set it's size. The tuple (800,600) sets our window to be 800 pixels wide by 600 pixels tall. The documentation has more regarding specifics here

done = False
while not done:
We create a boolean variable "done" and set it to False. This is what we are going to use to close the game. We create the main game loop. While not False is a double negative and is basically just while True. When done is set to True, the loop will end, and the game will close. While not True.

for event in pg.event.get():
    if event.type == pg.QUIT:
        done = True
This is our event loop. It catches any key presses, or other events. In this case it is only catching the click of the X, when you close out the window. Without this you would have to force close the window as the little X to close the window would not do anything. We loop pygame.event.get() which is events from the queue. And if the event type is the window X button or (pg.QUIT), then set done to True. Which close the window.

The events queue is a list of events. More info can be found here. pygame.event.get() grabs the whole list of events and clears them. So each frame as its ran has a new set of events from the previous frame. If you want to grab do a check for keys it would look like the following




pg.display.update()
Finally, we have our display update. This updates the entire screen.

import pygame as pg
import sys
 
pg.init()
screen = pg.display.set_mode((800,600))
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
             
    pg.display.update()
pg.quit()
sys.exit()
If you run this, you should get a black window 800 by 600 pixels. It should be able to close out via the x button. And that is about it. Here i added import sys and pg.quit() and sys.exit() at the end. pg.QUIT() should uninitialize the what pg.init did...the opposite. However sys.exit() is only needed when you run the program frim the IDE, IDLE. As sometimes it will not close. However you can always keep it there if you wish. It is important to let the program flow to the bottom of the script like we have it here, intead of throwing a sys.exit() in the event condition (ie. repalacing done = True with sys.exit())

It is important to know that the topleft of the window is at position (0,0), while the bottom right position of the window is whatever size window you make, in our case it would be (800,600).

   

Window Modifications

screen = pg.display.set_mode((800,600), pg.RESIZABLE)
You can set argument flags to set_mode() that allow what type of display you want. By just adding pg.RESIZABLE flag you can allow the user to resize the window at their will. The different types of flags can be seen
Quote:pygame.FULLSCREEN create a fullscreen display
pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
pygame.OPENGL create an OpenGL renderable display
pygame.RESIZABLE display window should be sizeable
pygame.NOFRAME display window will have no border or controls

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = '{},{}'.format(100,200)
You can also set the window to be centered in the smack center of your monitor. This value must be a string of the bloolean True or False.

All os.environ values must be set BEFORE you create the window via set_mode. You can center the window to the screen via
os.environ['SDL_VIDEO_CENTERED'] = '1'

import pygame as pg
import os
 
pg.init()
 
os.environ['SDL_VIDEO_WINDOW_POS'] = '{},{}'.format(100,200)
screen = pg.display.set_mode((800,600), pg.RESIZABLE)
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
             
    pg.display.update()

Part 2
https://python-forum.io/Thread-PyGame-Lo...ets-part-2
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020