02-02 - Create Window

01

 Keep the window up using a while True loop with the running variable. Import from pygame.locals import *. to get keyboard events to use https://www.pygame.org/docs/ref/locals.html

02

** Make this video faster

 While runing, loop through the pygame.event.get() method and get the event.type

 We can check and print out the type KEYDOWN and check event.key

Notice how the window doesn't close, but the ASCI numbers are being printed ** You will have to restart vs code if you do this

import pygame
from pygame.locals import *

if __name__ == '__main__':
    pygame.inti()
    surface = pygame.display.set_mode((900,300))
    pygame.display.flip()

    running = True
    while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event.key)
                    

03

To close the window check for the type QUIT and or key K_ESCAPE

Now we can click the x or hit the escape (ASCII 27) to close the window, remember to set running to False, and use surface.fill() to change background color. Do before flip()

import pygame 
from pygame.locals import *

if __name__ == "__main__":
    pygame.init()
    surface = pygame.display.set_mode((800,400))
    surface.fill((255,255,255))

    pygame.display.flip()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                print(event.key)
                if event.key == K_ESCAPE:
                    running = False
            if event.type == QUIT:
                running = False

                        
                    

04

Find the documentation for pygame.event's events and keys under the pygame.locals documentation from here or googling pygame locals