The examples in this tutorial can be found here.
import pygame as pg pg.init()
Here we import pygame library and name it pg. The reason for pg is preference for not typing out as many characters while also avoiding star imports. We initialize pygame with pg.init().
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.
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. 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.
pg.display.update()
Finally, we have our display update. This updates the entire screen.
import pygame as pg 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()
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.
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 here.
import os os.environ['SDL_VIDEO_WINDOW_POS'] = '{},{}'.format(100,200)
By adding this segment above in the example you can set the position at which the window first is created at. In this case the top left of the window will be at 100x200 pixels from the top left of the monitor. These values must be a single string and the values separated with a comma.
import os os.environ['SDL_VIDEO_CENTERED'] = "True"
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.
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()